Class: DRb::WebSocket::WSClient

Inherits:
Object
  • Object
show all
Defined in:
lib/drb/websocket/ws_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ WSClient

Returns a new instance of WSClient.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/drb/websocket/ws_client.rb', line 7

def initialize(uri)
  @uri = uri
  WSClient.start

  @handlers = { open: [], message: [] }
  @ws = Faye::WebSocket::Client.new(uri)

  EM.defer do
    @ws.on :open do |event|
      @handlers[:open].each do |proc|
        proc.call event
      end
    end

    @ws.on :message do |event|
      @handlers[:message].each do |proc|
        proc.call event
      end
    end
  end
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



5
6
7
# File 'lib/drb/websocket/ws_client.rb', line 5

def uri
  @uri
end

Class Method Details

.startObject



42
43
44
45
46
47
48
49
50
# File 'lib/drb/websocket/ws_client.rb', line 42

def self.start
  if @thread
    return
  end

  @thread = Thread.new do
    EM.run
  end
end

.stopObject



56
57
58
59
60
# File 'lib/drb/websocket/ws_client.rb', line 56

def self.stop
  EM.stop
  @thread.join
  @thread = nil
end

.threadObject



52
53
54
# File 'lib/drb/websocket/ws_client.rb', line 52

def self.thread
  @thread
end

Instance Method Details

#closeObject



37
38
39
40
# File 'lib/drb/websocket/ws_client.rb', line 37

def close
  @ws.close
  @ws = nil
end

#on(event, &block) ⇒ Object



29
30
31
# File 'lib/drb/websocket/ws_client.rb', line 29

def on(event, &block)
  @handlers[event] << block
end

#send(data) ⇒ Object



33
34
35
# File 'lib/drb/websocket/ws_client.rb', line 33

def send(data)
  @ws.send(data)
end