Class: WebSocket::Parser
- Inherits:
-
Object
- Object
- WebSocket::Parser
- Defined in:
- lib/websocket/parser.rb
Overview
This class parses WebSocket messages and frames.
Each message is divied in frames as described in RFC 6455.
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-------+-+-------------+-------------------------------+
|F|R|R|R| opcode|M| Payload len | Extended payload length |
|I|S|S|S| (4) |A| (7) | (16/64) |
|N|V|V|V| |S| | (if payload len==126/127) |
| |1|2|3| |K| | |
+-+-+-+-+-------+-+-------------+ - - - - - - - - - - - - - - - +
| Extended payload length continued, if payload len == 127 |
+ - - - - - - - - - - - - - - - +-------------------------------+
| |Masking-key, if MASK set to 1 |
+-------------------------------+-------------------------------+
| Masking-key (continued) | Payload Data |
+-------------------------------- - - - - - - - - - - - - - - - +
: Payload Data continued ... :
+ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +
| Payload Data continued ... |
+---------------------------------------------------------------+
for more info on the frame format see: tools.ietf.org/html/rfc6455#section-5
Instance Attribute Summary collapse
- #on_close(&callback) ⇒ Object
- #on_error(&callback) ⇒ Object
- #on_message(&callback) ⇒ Object
- #on_ping(&callback) ⇒ Object
- #on_pong(&callback) ⇒ Object
Instance Method Summary collapse
-
#append(data) ⇒ Object
Stores the data in a buffer for later parsing.
-
#initialize ⇒ Parser
constructor
A new instance of Parser.
-
#next_message ⇒ Object
Parse next message in buffer.
-
#next_messages ⇒ Object
Parse all messages in buffer.
-
#receive(data) ⇒ Object
(also: #<<)
Receive data and parse it return an array of parsed messages.
Constructor Details
#initialize ⇒ Parser
Returns a new instance of Parser.
30 31 32 33 34 |
# File 'lib/websocket/parser.rb', line 30 def initialize @data = ''.force_encoding("ASCII-8BIT") @state = :header @current_message = nil end |
Instance Attribute Details
#on_close(&callback) ⇒ Object
44 45 46 |
# File 'lib/websocket/parser.rb', line 44 def on_close(&callback) @on_close = callback end |
#on_error(&callback) ⇒ Object
40 41 42 |
# File 'lib/websocket/parser.rb', line 40 def on_error(&callback) @on_error = callback end |
#on_message(&callback) ⇒ Object
36 37 38 |
# File 'lib/websocket/parser.rb', line 36 def (&callback) @on_message = callback end |
#on_ping(&callback) ⇒ Object
48 49 50 |
# File 'lib/websocket/parser.rb', line 48 def on_ping(&callback) @on_ping = callback end |
#on_pong(&callback) ⇒ Object
52 53 54 |
# File 'lib/websocket/parser.rb', line 52 def on_pong(&callback) @on_pong = callback end |
Instance Method Details
#append(data) ⇒ Object
Stores the data in a buffer for later parsing
57 58 59 60 |
# File 'lib/websocket/parser.rb', line 57 def append(data) @data << data @data.force_encoding("ASCII-8BIT") end |
#next_message ⇒ Object
Parse next message in buffer
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/websocket/parser.rb', line 79 def read_header if @state == :header read_payload_length if @state == :payload_length read_mask_key if @state == :mask read_payload if @state == :payload @state == :complete ? process_frame! : nil rescue StandardError => ex if @on_error @on_error.call(ex.) else raise ex end end |
#next_messages ⇒ Object
Parse all messages in buffer
70 71 72 73 74 75 76 |
# File 'lib/websocket/parser.rb', line 70 def Array.new.tap do || while msg = do << msg end end end |
#receive(data) ⇒ Object Also known as: <<
Receive data and parse it return an array of parsed messages
63 64 65 |
# File 'lib/websocket/parser.rb', line 63 def receive(data) append(data) && end |