Class: WebSocket::Parser

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



30
31
32
33
# File 'lib/websocket/parser.rb', line 30

def initialize
  @data =  ''.force_encoding("ASCII-8BIT")
  @state = :header
end

Instance Attribute Details

#on_close(&callback) ⇒ Object



43
44
45
# File 'lib/websocket/parser.rb', line 43

def on_close(&callback)
  @on_close = callback
end

#on_error(&callback) ⇒ Object



39
40
41
# File 'lib/websocket/parser.rb', line 39

def on_error(&callback)
  @on_error = callback
end

#on_message(&callback) ⇒ Object



35
36
37
# File 'lib/websocket/parser.rb', line 35

def on_message(&callback)
  @on_message = callback
end

#on_ping(&callback) ⇒ Object



47
48
49
# File 'lib/websocket/parser.rb', line 47

def on_ping(&callback)
  @on_ping = callback
end

#on_pong(&callback) ⇒ Object



51
52
53
# File 'lib/websocket/parser.rb', line 51

def on_pong(&callback)
  @on_pong = callback
end

Instance Method Details

#append(data) ⇒ Object

Stores the data in a buffer for later parsing



56
57
58
# File 'lib/websocket/parser.rb', line 56

def append(data)
  @data << data
end

#next_messageObject

Parse next message in buffer



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/websocket/parser.rb', line 77

def next_message
  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.message)
  else
    raise ex
  end
end

#next_messagesObject

Parse all messages in buffer



68
69
70
71
72
73
74
# File 'lib/websocket/parser.rb', line 68

def next_messages
  Array.new.tap do |messages|
    while msg = next_message do
       messages << msg
     end
  end
end

#receive(data) ⇒ Object Also known as: <<

Receive data and parse it return an array of parsed messages



61
62
63
# File 'lib/websocket/parser.rb', line 61

def receive(data)
  append(data) && next_messages
end