Class: WebSocket::Protocol::Hybi

Inherits:
WebSocket::Protocol show all
Defined in:
lib/websocket/protocol/hybi.rb,
lib/websocket/protocol/hybi/stream_reader.rb

Direct Known Subclasses

Client

Defined Under Namespace

Classes: StreamReader

Constant Summary collapse

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
}
FRAGMENTED_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

Constants inherited from WebSocket::Protocol

STATES

Instance Attribute Summary

Attributes inherited from WebSocket::Protocol

#protocol, #ready_state

Instance Method Summary collapse

Methods inherited from WebSocket::Protocol

jruby?, #onclose, #onerror, #onmessage, #onopen, rbx?, #start, #state

Constructor Details

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

Returns a new instance of Hybi.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/websocket/protocol/hybi.rb', line 44

def initialize(socket, options = {})
  super
  reset

  @reader    = StreamReader.new
  @stage     = 0
  @masking   = options[:masking]
  @protocols = options[:protocols]
  @protocols = @protocols.strip.split(/\s*,\s*/) if String === @protocols

  @require_masking = options[:require_masking]
  @ping_callbacks  = {}
end

Instance Method Details

#binary(message) ⇒ Object



155
156
157
# File 'lib/websocket/protocol/hybi.rb', line 155

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

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



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/websocket/protocol/hybi.rb', line 164

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

  case @ready_state
    when 0 then
      @ready_state = 3
      dispatch(:onclose, CloseEvent.new(code, reason))
      true
    when 1 then
      frame(reason, :close, code)
      @ready_state = 2
      true
    else
      false
  end
end

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



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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/websocket/protocol/hybi.rb', line 98

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

  data = data.to_s unless Array === data
  data = Protocol.encode(data) if String === data

  is_text = (String === data)
  opcode  = OPCODES[type || (is_text ? :text : :binary)]
  buffer  = data.respond_to?(:bytes) ? data.bytes.to_a : data
  insert  = code ? 2 : 0
  length  = buffer.size + insert
  header  = (length <= 125) ? 2 : (length <= 65535 ? 4 : 10)
  offset  = header + (@masking ? 4 : 0)
  masked  = @masking ? MASK : 0
  frame   = Array.new(offset)

  frame[0] = FIN | opcode

  if length <= 125
    frame[1] = masked | length
  elsif length <= 65535
    frame[1] = masked | 126
    frame[2] = (length >> 8) & BYTE
    frame[3] = length & BYTE
  else
    frame[1] = masked | 127
    frame[2] = (length >> 56) & BYTE
    frame[3] = (length >> 48) & BYTE
    frame[4] = (length >> 40) & BYTE
    frame[5] = (length >> 32) & BYTE
    frame[6] = (length >> 24) & BYTE
    frame[7] = (length >> 16) & BYTE
    frame[8] = (length >> 8)  & BYTE
    frame[9] = length & BYTE
  end

  if code
    buffer = [(code >> 8) & BYTE, code & BYTE] + buffer
  end

  if @masking
    mask = [rand(256), rand(256), rand(256), rand(256)]
    frame[header...offset] = mask
    buffer = Mask.mask(buffer, mask)
  end

  frame.concat(buffer)

  @socket.write(Protocol.encode(frame))
  true
end

#parse(data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/websocket/protocol/hybi.rb', line 62

def parse(data)
  data = data.bytes.to_a if data.respond_to?(:bytes)
  @reader.put(data)
  buffer = true
  while buffer
    case @stage
      when 0 then
        buffer = @reader.read(1)
        parse_opcode(buffer[0]) if buffer

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

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

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

      when 4 then
        buffer = @reader.read(@length)
        if buffer
          @payload = buffer
          emit_frame
          @stage = 0
        end
    end
  end
end

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



159
160
161
162
# File 'lib/websocket/protocol/hybi.rb', line 159

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

#text(message) ⇒ Object



151
152
153
# File 'lib/websocket/protocol/hybi.rb', line 151

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

#versionObject



58
59
60
# File 'lib/websocket/protocol/hybi.rb', line 58

def version
  "hybi-#{@socket.env['HTTP_SEC_WEBSOCKET_VERSION']}"
end