Class: DRb::WebSocket::WSClient
- Inherits:
-
Object
- Object
- DRb::WebSocket::WSClient
- Defined in:
- lib/drb/websocket/ws_client.rb
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(uri) ⇒ WSClient
constructor
A new instance of WSClient.
- #on(event, &block) ⇒ Object
- #send(data) ⇒ Object
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
#uri ⇒ Object (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
.start ⇒ Object
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 |
.stop ⇒ Object
56 57 58 59 60 |
# File 'lib/drb/websocket/ws_client.rb', line 56 def self.stop EM.stop @thread.join @thread = nil end |
.thread ⇒ Object
52 53 54 |
# File 'lib/drb/websocket/ws_client.rb', line 52 def self.thread @thread end |
Instance Method Details
#close ⇒ Object
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 |