Class: EventMachine::WebsocketClient
- Inherits:
-
HttpClient
- Object
- HttpClient
- EventMachine::WebsocketClient
- Defined in:
- lib/em-ws-request/client.rb
Constant Summary collapse
- PROTOCOL_VERSION =
'13'
Instance Method Summary collapse
- #build_request ⇒ Object
-
#disconnect(&blk) ⇒ Object
DISCONNECT ####.
-
#initialize(conn, options) ⇒ WebsocketClient
constructor
A new instance of WebsocketClient.
- #parse_response_header(header, version, status) ⇒ Object
- #receive(data) ⇒ Object
- #send(data, frame_type = :text) ⇒ Object
- #send_data(data) ⇒ Object
- #unbind(reason = nil) ⇒ Object
- #websocket? ⇒ Boolean
Constructor Details
#initialize(conn, options) ⇒ WebsocketClient
Returns a new instance of WebsocketClient.
38 39 40 41 |
# File 'lib/em-ws-request/client.rb', line 38 def initialize(conn, ) super(conn, ) @wswrapper = WebSocketWrapper.new(self, PROTOCOL_VERSION) end |
Instance Method Details
#build_request ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/em-ws-request/client.rb', line 63 def build_request head = super if websocket? @sec_websocket_key = @wswrapper.generate_key head.delete_if { |k, v| !%w(host).include?(k) } head['upgrade'] = 'websocket' head['connection'] = 'Upgrade' head['origin'] = @req.uri.host head['Sec-WebSocket-Key'] = @sec_websocket_key head['Sec-WebSocket-Version'] = PROTOCOL_VERSION end head end |
#disconnect(&blk) ⇒ Object
DISCONNECT ####
95 96 97 |
# File 'lib/em-ws-request/client.rb', line 95 def disconnect(&blk) @disconnect = blk end |
#parse_response_header(header, version, status) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/em-ws-request/client.rb', line 77 def parse_response_header(header, version, status) super if websocket? # p [:parse_response_header, :WEBSOCKET] if @response_header.status != 101 fail "websocket handshake failed (not status 101)" elsif @wswrapper.security_digest(@sec_websocket_key) != @response_header['SEC_WEBSOCKET_ACCEPT'] fail "websocket handshake failed (mismatched key)" else @state = :upgrade end end end |
#receive(data) ⇒ Object
54 55 56 57 |
# File 'lib/em-ws-request/client.rb', line 54 def receive(data) out = @wswrapper.receive(data) @stream.call(out) if @stream end |
#send(data, frame_type = :text) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/em-ws-request/client.rb', line 43 def send(data, frame_type = :text) @connection = @conn if state == :stream @wswrapper.send_frame(frame_type, data) end end |
#send_data(data) ⇒ Object
50 51 52 |
# File 'lib/em-ws-request/client.rb', line 50 def send_data(data) @conn.send_data(data) end |
#unbind(reason = nil) ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'lib/em-ws-request/client.rb', line 99 def unbind(reason = nil) if state == :stream @disconnect.call(self) if @disconnect on_error(reason) # TODO: Should we really barf on unbind? else super(reason) end end |
#websocket? ⇒ Boolean
59 60 61 |
# File 'lib/em-ws-request/client.rb', line 59 def websocket? @req.uri.scheme == 'ws' || @req.uri.scheme == 'wss' end |