Class: DripDrop::WebSocketHandler
- Inherits:
-
BaseHandler
- Object
- BaseHandler
- DripDrop::WebSocketHandler
- Defined in:
- lib/dripdrop/handlers/websockets.rb
Defined Under Namespace
Classes: Connection, SocketError
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#ws ⇒ Object
readonly
Returns the value of attribute ws.
Instance Method Summary collapse
-
#initialize(address, opts = {}) ⇒ WebSocketHandler
constructor
A new instance of WebSocketHandler.
- #on_close(&block) ⇒ Object
- #on_open(&block) ⇒ Object
- #on_recv(&block) ⇒ Object
- #on_recv_raw(&block) ⇒ Object
Methods inherited from BaseHandler
#handle_error, #on_error, #print_exception
Constructor Details
#initialize(address, opts = {}) ⇒ WebSocketHandler
Returns a new instance of WebSocketHandler.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dripdrop/handlers/websockets.rb', line 10 def initialize(address,opts={}) @opts = opts @raw = false #Deal in strings or ZMQ::Message objects host, port = address.host, address.port.to_i @message_class = @opts[:message_class] || DripDrop. @debug = @opts[:debug] || false EventMachine::WebSocket.start(:host => host,:port => port,:debug => @debug) do |ws| #A WebSocketHandler:Connection gets passed to all callbacks dd_conn = Connection.new(ws) ws.onopen { @onopen_handler.call(dd_conn) if @onopen_handler } ws.onclose { @onclose_handler.call(dd_conn) if @onclose_handler } ws.onerror {|reason| e = SocketError.new e.reason = reason e.connection = dd_conn handle_error(e) } ws. { || if @onmessage_handler begin = @message_class.decode() unless @raw @onmessage_handler.call(,dd_conn) rescue StandardError => e handle_error(e,dd_conn) end end } end end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
8 9 10 |
# File 'lib/dripdrop/handlers/websockets.rb', line 8 def address @address end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
8 9 10 |
# File 'lib/dripdrop/handlers/websockets.rb', line 8 def thread @thread end |
#ws ⇒ Object (readonly)
Returns the value of attribute ws.
8 9 10 |
# File 'lib/dripdrop/handlers/websockets.rb', line 8 def ws @ws end |
Instance Method Details
#on_close(&block) ⇒ Object
60 61 62 63 |
# File 'lib/dripdrop/handlers/websockets.rb', line 60 def on_close(&block) @onclose_handler = block self end |
#on_open(&block) ⇒ Object
55 56 57 58 |
# File 'lib/dripdrop/handlers/websockets.rb', line 55 def on_open(&block) @onopen_handler = block self end |
#on_recv(&block) ⇒ Object
43 44 45 46 47 |
# File 'lib/dripdrop/handlers/websockets.rb', line 43 def on_recv(&block) @raw = false @onmessage_handler = block self end |
#on_recv_raw(&block) ⇒ Object
49 50 51 52 53 |
# File 'lib/dripdrop/handlers/websockets.rb', line 49 def on_recv_raw(&block) @raw = true @onmessage_handler = block self end |