Class: Isomorfeus::Transport::WebsocketClient
- Inherits:
-
Object
- Object
- Isomorfeus::Transport::WebsocketClient
- Defined in:
- lib/isomorfeus/transport/websocket_client.rb
Defined Under Namespace
Classes: SendError
Constant Summary collapse
- CONNECTING =
0
- OPEN =
1
- CLOSING =
2
- CLOSED =
3
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(url, protocols = nil) ⇒ WebsocketClient
constructor
A new instance of WebsocketClient.
- #on_close(&block) ⇒ Object
- #on_error(&block) ⇒ Object
- #on_message(&block) ⇒ Object
- #on_open(&block) ⇒ Object
- #protocol ⇒ Object
- #send(data) ⇒ Object (also: #write)
Constructor Details
#initialize(url, protocols = nil) ⇒ WebsocketClient
Returns a new instance of WebsocketClient.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/isomorfeus/transport/websocket_client.rb', line 14 def initialize(url, protocols = nil, headers = nil) @url = url @native_websocket = if protocols && headers `new Opal.global.WebSocket(url, protocols, {headers: #{headers.to_n}})` elsif protocols `new Opal.global.WebSocket(url, protocols)` elsif headers `new Opal.global.WebSocket(url, {headers: #{headers.to_n}})` else `new Opal.global.WebSocket(url)` end end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
4 5 6 |
# File 'lib/isomorfeus/transport/websocket_client.rb', line 4 def url @url end |
Instance Method Details
#close ⇒ Object
27 28 29 30 |
# File 'lib/isomorfeus/transport/websocket_client.rb', line 27 def close @native_websocket.JS.close nil end |
#on_close(&block) ⇒ Object
32 33 34 |
# File 'lib/isomorfeus/transport/websocket_client.rb', line 32 def on_close(&block) @native_websocket.JS[:onclose] = `function(event) { block.$call(event); }` end |
#on_error(&block) ⇒ Object
36 37 38 |
# File 'lib/isomorfeus/transport/websocket_client.rb', line 36 def on_error(&block) @native_websocket.JS[:onerror] = `function(event) { block.$call(event); }` end |
#on_message(&block) ⇒ Object
40 41 42 |
# File 'lib/isomorfeus/transport/websocket_client.rb', line 40 def (&block) @native_websocket.JS[:onmessage] = `function(event) { block.$call(event); }` end |
#on_open(&block) ⇒ Object
44 45 46 |
# File 'lib/isomorfeus/transport/websocket_client.rb', line 44 def on_open(&block) @native_websocket.JS[:onopen] = `function(event) { block.$call(event); }` end |
#protocol ⇒ Object
48 49 50 |
# File 'lib/isomorfeus/transport/websocket_client.rb', line 48 def protocol @native_websocket.JS[:protocol] end |
#send(data) ⇒ Object Also known as: write
52 53 54 55 56 57 58 59 |
# File 'lib/isomorfeus/transport/websocket_client.rb', line 52 def send(data) case ready_state when OPEN then @native_websocket.JS.send(data) when CONNECTING then after(50) { send(data) } when CLOSING then Isomorfeus.raise_error(error_class: SendError, message: 'Cant send, connection is closing!') when CLOSED then Isomorfeus.raise_error(error_class: SendError, message: 'Cant send, connection is closed!') end end |