Class: Vox::Gateway::WebSocket
- Inherits:
-
Object
- Object
- Vox::Gateway::WebSocket
- Includes:
- EventEmitter
- Defined in:
- lib/vox/gateway/websocket.rb
Overview
Websocket that handles data interchange for Client.
Constant Summary collapse
- ZLIB_SUFFIX =
Zlib boundary used for separating messages split into multiple frames.
"\x00\x00\xFF\xFF".b.freeze
Instance Attribute Summary collapse
-
#driver ⇒ Object
readonly
Returns the value of attribute driver.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#close(reason = nil, code = 1000) ⇒ true, false
Close the websocket connection.
-
#connect ⇒ Thread
Connect to the websocket server.
-
#initialize(url, port: nil, compression: true) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #on(key, data) ⇒ Object
-
#send(message) ⇒ true, false
Send a ‘text` message.
-
#send_binary(data) ⇒ true, false
Send a ‘binary` frame.
-
#send_json(hash) ⇒ true, false
Serialize a hash to send as a ‘text` message.
Constructor Details
#initialize(url, port: nil, compression: true) ⇒ WebSocket
Returns a new instance of WebSocket.
24 25 26 27 28 29 |
# File 'lib/vox/gateway/websocket.rb', line 24 def initialize(url, port: nil, compression: true) @url = url @uri = URI.parse(url) @port = port || @uri.scheme == 'wss' ? 443 : 80 @inflate = Zlib::Inflate.new if compression end |
Instance Attribute Details
#driver ⇒ Object (readonly)
Returns the value of attribute driver.
22 23 24 |
# File 'lib/vox/gateway/websocket.rb', line 22 def driver @driver end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
22 23 24 |
# File 'lib/vox/gateway/websocket.rb', line 22 def thread @thread end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
22 23 24 |
# File 'lib/vox/gateway/websocket.rb', line 22 def url @url end |
Instance Method Details
#close(reason = nil, code = 1000) ⇒ true, false
Close the websocket connection.
99 100 101 |
# File 'lib/vox/gateway/websocket.rb', line 99 def close(reason = nil, code = 1000) @driver.close(reason, code) end |
#connect ⇒ Thread
Connect to the websocket server.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/vox/gateway/websocket.rb', line 44 def connect # Flush the zlib buffer @inflate&.reset # Create a socket connection to the URL @socket = create_socket # Initialize the websocket driver setup_driver # Read until our websocket closes. @thread = Thread.new do read_loop end end |
#on('open', &block) ⇒ Object #on('message') {|data| ... } ⇒ Object #on('close') {|code, reason| ... } ⇒ Object
|
# File 'lib/vox/gateway/websocket.rb', line 31
|
#send(message) ⇒ true, false
Send a ‘text` message.
63 64 65 66 |
# File 'lib/vox/gateway/websocket.rb', line 63 def send() LOGGER.debug { "[OUT] #{} " } @driver.text() end |
#send_binary(data) ⇒ true, false
Send a ‘binary` frame.
79 80 81 |
# File 'lib/vox/gateway/websocket.rb', line 79 def send_binary(data) @driver.binary(data) end |
#send_json(hash) ⇒ true, false
Serialize a hash to send as a ‘text` message.
71 72 73 74 |
# File 'lib/vox/gateway/websocket.rb', line 71 def send_json(hash) data = MultiJson.dump(hash) send(data) end |