Class: EventMachine::WebSocketClient
- Inherits:
-
Object
- Object
- EventMachine::WebSocketClient
- Includes:
- EventMachine::WebSocketCodec::Protocol
- Defined in:
- lib/em-ws-client/client.rb
Overview
Public: A fully functional WebSocket client implementation.
Examples
ws = WebSocketClient.new "ws://localhost/chat"
ws. do |msg|
puts msg
end
ws.onopen do
ws. "Hello!"
end
Defined Under Namespace
Classes: WebSocketConnection, WebSocketError
Constant Summary collapse
- Version =
"0.2.0"
Constants included from EventMachine::WebSocketCodec::Protocol
EventMachine::WebSocketCodec::Protocol::BINARY_FRAME, EventMachine::WebSocketCodec::Protocol::CLOSE, EventMachine::WebSocketCodec::Protocol::CONTINUATION, EventMachine::WebSocketCodec::Protocol::PING, EventMachine::WebSocketCodec::Protocol::PONG, EventMachine::WebSocketCodec::Protocol::TEXT_FRAME
Instance Attribute Summary collapse
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #close(code = 1000, msg = nil) ⇒ Object
-
#initialize(uri, origin = "em-ws-client") ⇒ WebSocketClient
constructor
Public: Initialize a WebSocket client.
-
#onclose(&block) ⇒ Object
Bind a callback to the close event.
-
#onerror(&block) ⇒ Object
Bind a callback to the error event.
-
#onmessage(&block) ⇒ Object
Bind a callback to the message event.
-
#onopen(&block) ⇒ Object
Bind a callback to the open event.
-
#onping(&block) ⇒ Object
Bind a callback to the ping event.
-
#onpong(&block) ⇒ Object
Bind a callback to the pong event.
-
#receive_data(data) ⇒ Object
Internal: called by eventmachine when data is received.
-
#send_message(data, binary = false) ⇒ Object
Send a message to the remote host.
-
#unbind ⇒ Object
Public: Close the connection.
Constructor Details
#initialize(uri, origin = "em-ws-client") ⇒ WebSocketClient
Public: Initialize a WebSocket client
uri - The endpoint host url origin - The origin you wish to claim
Examples
WebSocketClient.new "ws://localhost:9000/chat"
WebSocketClient.new "ws://ws.site.com/chat", "http://www.site.com/chat"
Returns the client
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/em-ws-client/client.rb', line 58 def initialize uri, origin="em-ws-client" super(); @uri = URI.parse(uri) @origin = origin @buffer = "" @encoder = WebSocketCodec::Encoder.new @decoder = WebSocketCodec::Decoder.new @handshake = WebSocketCodec::Handshake.new @uri, @origin @callbacks = {} @closing = false connect end |
Instance Attribute Details
#socket ⇒ Object
Returns the value of attribute socket.
45 46 47 |
# File 'lib/em-ws-client/client.rb', line 45 def socket @socket end |
Instance Method Details
#close(code = 1000, msg = nil) ⇒ Object
219 220 221 222 223 |
# File 'lib/em-ws-client/client.rb', line 219 def close code=1000, msg=nil @closing = true @socket.send_data @encoder.close(code, msg) @socket.close_connection_after_writing end |
#onclose(&block) ⇒ Object
Bind a callback to the close event
block - A block which is called when the connection to the remote host is closed. Your block receives 2 arguments, with the second potentially being nil.
Examples
ws.onclose do |code, explain|
end
Returns nothing
115 116 117 |
# File 'lib/em-ws-client/client.rb', line 115 def onclose &block @callbacks[:close] = block end |
#onerror(&block) ⇒ Object
Bind a callback to the error event
block - A block which is called when an error occurs. The connection is dropped immediately per spec. The first argument is the close code, and the second is the error.
Examples
ws.onerror do |close_code, error|
end
Returns nothing
149 150 151 |
# File 'lib/em-ws-client/client.rb', line 149 def onerror &block @callbacks[:error] = block end |
#onmessage(&block) ⇒ Object
Bind a callback to the message event
block - A block which is called when a message is received. The first argument for the block is the message, and the second argument is a binary flag.
Examples
ws. do |, binary|
end
Returns nothing
132 133 134 |
# File 'lib/em-ws-client/client.rb', line 132 def &block @callbacks[:frame] = block end |
#onopen(&block) ⇒ Object
Bind a callback to the open event
block - A block which is called when the connection to the remote host is established
Examples
ws.onopen do
end
Returns nothing
98 99 100 |
# File 'lib/em-ws-client/client.rb', line 98 def onopen &block @callbacks[:open] = block end |
#onping(&block) ⇒ Object
Bind a callback to the ping event
block - A block which is called when the remote host sends a ping. A single argument is sent, which contains the ping data sent from the remote host. A pong is automatically sent.
Examples
ws.onping do |data|
end
Returns nothing
167 168 169 |
# File 'lib/em-ws-client/client.rb', line 167 def onping &block @callbacks[:ping] = block end |
#onpong(&block) ⇒ Object
Bind a callback to the pong event
block - A block which is called when the remote host sends a pong in response to your ping. It’s possible to get unwarrented pongs.
Examples
ws.onpong do |data|
end
Returns nothing
184 185 186 |
# File 'lib/em-ws-client/client.rb', line 184 def onpong &block @callbacks[:pong] = block end |
#receive_data(data) ⇒ Object
Internal: called by eventmachine when data is received
190 191 192 193 194 195 196 |
# File 'lib/em-ws-client/client.rb', line 190 def receive_data(data) if @handshake.complete? data else receive_handshake_data data end end |
#send_message(data, binary = false) ⇒ Object
Send a message to the remote host
data - The string contents of your message
Examples
ws.onping do |data|
end
Returns nothing
209 210 211 212 213 214 215 216 217 |
# File 'lib/em-ws-client/client.rb', line 209 def data, binary=false if established? unless @closing @socket.send_data(@encoder.encode(data.to_s, binary ? BINARY_FRAME : TEXT_FRAME)) end else raise WebSocketError.new "can't send on a closed channel" end end |
#unbind ⇒ Object
Public: Close the connection
Examples
ws.unbind
# => ?
Returns
83 84 85 |
# File 'lib/em-ws-client/client.rb', line 83 def unbind emit :close end |