Class: WebSocket::Driver::Hybi

Inherits:
WebSocket::Driver show all
Defined in:
lib/websocket/driver/hybi.rb,
lib/websocket/driver/hybi/frame.rb,
lib/websocket/driver/hybi/message.rb

Direct Known Subclasses

Client

Defined Under Namespace

Classes: Frame, Message

Constant Summary collapse

VERSION =
'13'
GUID =
'258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
BYTE =
0b11111111
FIN =
MASK = 0b10000000
RSV1 =
0b01000000
RSV2 =
0b00100000
RSV3 =
0b00010000
OPCODE =
0b00001111
LENGTH =
0b01111111
OPCODES =
{
  :continuation => 0,
  :text         => 1,
  :binary       => 2,
  :close        => 8,
  :ping         => 9,
  :pong         => 10
}
OPCODE_CODES =
OPCODES.values
MESSAGE_OPCODES =
OPCODES.values_at(:continuation, :text, :binary)
OPENING_OPCODES =
OPCODES.values_at(:text, :binary)
ERRORS =
{
  :normal_closure       => 1000,
  :going_away           => 1001,
  :protocol_error       => 1002,
  :unacceptable         => 1003,
  :encoding_error       => 1007,
  :policy_violation     => 1008,
  :too_large            => 1009,
  :extension_error      => 1010,
  :unexpected_condition => 1011
}
ERROR_CODES =
ERRORS.values
DEFAULT_ERROR_CODE =
1000
MIN_RESERVED_ERROR =
3000
MAX_RESERVED_ERROR =
4999
PACK_FORMATS =
{ 2 => 'S>', 8 => 'Q>' }

Constants inherited from WebSocket::Driver

ConfigurationError, MAX_LENGTH, ProtocolError, STATES, URIError

Instance Attribute Summary

Attributes inherited from WebSocket::Driver

#protocol, #ready_state

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from WebSocket::Driver

#set_header, #start, #state, #text

Methods included from EventEmitter

#add_listener, #emit, #listener_count, #listeners, #on, #remove_all_listeners, #remove_listener

Constructor Details

#initialize(socket, options = {}) ⇒ Hybi

Returns a new instance of Hybi.


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/websocket/driver/hybi.rb', line 57

def initialize(socket, options = {})
  super

  @extensions      = ::WebSocket::Extensions.new
  @stage           = 0
  @masking         = options[:masking]
  @protocols       = options[:protocols] || []
  @protocols       = @protocols.strip.split(/ *, */) if String === @protocols
  @require_masking = options[:require_masking]
  @ping_callbacks  = {}

  @frame = @message = nil

  return unless @socket.respond_to?(:env)

  if protos = @socket.env['HTTP_SEC_WEBSOCKET_PROTOCOL']
    protos = protos.split(/ *, */) if String === protos
    @protocol = protos.find { |p| @protocols.include?(p) }
  else
    @protocol = nil
  end
end

Class Method Details

.generate_accept(key) ⇒ Object


10
11
12
# File 'lib/websocket/driver/hybi.rb', line 10

def self.generate_accept(key)
  Base64.strict_encode64(Digest::SHA1.digest(key + GUID))
end

Instance Method Details

#add_extension(extension) ⇒ Object


84
85
86
87
# File 'lib/websocket/driver/hybi.rb', line 84

def add_extension(extension)
  @extensions.add(extension)
  true
end

#binary(message) ⇒ Object


127
128
129
# File 'lib/websocket/driver/hybi.rb', line 127

def binary(message)
  frame(message, :binary)
end

#close(reason = nil, code = nil) ⇒ Object


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/websocket/driver/hybi.rb', line 140

def close(reason = nil, code = nil)
  reason ||= ''
  code   ||= ERRORS[:normal_closure]

  if @ready_state <= 0
    @ready_state = 3
    emit(:close, CloseEvent.new(code, reason))
    true
  elsif @ready_state == 1
    frame(reason, :close, code)
    @ready_state = 2
    true
  else
    false
  end
end

#frame(buffer, type = nil, code = nil) ⇒ Object


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/websocket/driver/hybi.rb', line 157

def frame(buffer, type = nil, code = nil)
  return queue([buffer, type, code]) if @ready_state <= 0
  return false unless @ready_state == 1

  message = Message.new
  frame   = Frame.new

  message.rsv1   = message.rsv2 = message.rsv3 = false
  message.opcode = OPCODES[type || (String === buffer ? :text : :binary)]

  payload = Driver.encode(buffer)
  payload = [code, payload].pack('S>a*') if code
  message.data = payload

  if MESSAGE_OPCODES.include?(message.opcode)
    message = @extensions.process_outgoing_message(message)
  end

  frame.final       = true
  frame.rsv1        = message.rsv1
  frame.rsv2        = message.rsv2
  frame.rsv3        = message.rsv3
  frame.opcode      = message.opcode
  frame.masked      = !!@masking
  frame.masking_key = SecureRandom.random_bytes(4) if frame.masked
  frame.length      = message.data.bytesize
  frame.payload     = message.data

  send_frame(frame)
  true

rescue ::WebSocket::Extensions::ExtensionError => error
  fail(:extension_error, error.message)
end

#parse(chunk) ⇒ Object


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/websocket/driver/hybi.rb', line 89

def parse(chunk)
  @reader.put(chunk)
  buffer = true
  while buffer
    case @stage
      when 0 then
        buffer = @reader.read(1)
        parse_opcode(buffer.getbyte(0)) if buffer

      when 1 then
        buffer = @reader.read(1)
        parse_length(buffer.getbyte(0)) if buffer

      when 2 then
        buffer = @reader.read(@frame.length_bytes)
        parse_extended_length(buffer) if buffer

      when 3 then
        buffer = @reader.read(4)
        if buffer
          @stage = 4
          @frame.masking_key = buffer
        end

      when 4 then
        buffer = @reader.read(@frame.length)

        if buffer
          @stage = 0
          emit_frame(buffer)
        end

      else
        buffer = nil
    end
  end
end

#ping(message = '', &callback) ⇒ Object


131
132
133
134
# File 'lib/websocket/driver/hybi.rb', line 131

def ping(message = '', &callback)
  @ping_callbacks[message] = callback if callback
  frame(message, :ping)
end

#pong(message = '') ⇒ Object


136
137
138
# File 'lib/websocket/driver/hybi.rb', line 136

def pong(message = '')
  frame(message, :pong)
end

#versionObject


80
81
82
# File 'lib/websocket/driver/hybi.rb', line 80

def version
  "hybi-#{ VERSION }"
end