Module: Jellyfish::WebSocket
- Defined in:
- lib/jellyfish/websocket.rb
Instance Attribute Summary collapse
-
#parser ⇒ Object
readonly
Returns the value of attribute parser.
-
#sock ⇒ Object
readonly
Returns the value of attribute sock.
Instance Method Summary collapse
- #switch_protocol(&block) ⇒ Object
- #ws_close ⇒ Object
- #ws_read(bytes = 8192) ⇒ Object
- #ws_start ⇒ Object
- #ws_write(msg) ⇒ Object
Instance Attribute Details
#parser ⇒ Object (readonly)
Returns the value of attribute parser.
12 13 14 |
# File 'lib/jellyfish/websocket.rb', line 12 def parser @parser end |
#sock ⇒ Object (readonly)
Returns the value of attribute sock.
12 13 14 |
# File 'lib/jellyfish/websocket.rb', line 12 def sock @sock end |
Instance Method Details
#switch_protocol(&block) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/jellyfish/websocket.rb', line 14 def switch_protocol &block key = env['HTTP_SEC_WEBSOCKET_KEY'] accept = [Digest::SHA1.digest("#{key}#{GUID}")].pack('m0') @sock = env['rack.hijack'].call sock.binmode sock.write(<<-HTTP) HTTP/1.1 101 Switching Protocols\r Upgrade: websocket\r Connection: Upgrade\r Sec-WebSocket-Accept: #{accept}\r \r HTTP @parser = Parser.new parser.(&block) end |
#ws_close ⇒ Object
46 47 48 49 |
# File 'lib/jellyfish/websocket.rb', line 46 def ws_close sock << Message.close.to_data sock.close end |
#ws_read(bytes = 8192) ⇒ Object
36 37 38 39 40 |
# File 'lib/jellyfish/websocket.rb', line 36 def ws_read bytes=8192 parser << sock.readpartial(bytes) rescue EOFError sock.close end |
#ws_start ⇒ Object
30 31 32 33 34 |
# File 'lib/jellyfish/websocket.rb', line 30 def ws_start while !sock.closed? && IO.select([sock]) do ws_read end end |
#ws_write(msg) ⇒ Object
42 43 44 |
# File 'lib/jellyfish/websocket.rb', line 42 def ws_write msg sock << Message.new(msg).to_data end |