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
- 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
- MIN_RESERVED_ERROR =
3000
- MAX_RESERVED_ERROR =
4999
- PACK_FORMATS =
{2 => 'n', 8 => 'Q>'}
ConfigurationError, MAX_LENGTH, ProtocolError, STATES
Instance Attribute Summary
#protocol, #ready_state
Class Method Summary
collapse
Instance Method Summary
collapse
#set_header, #start, #state
#add_listener, #emit, #listener_count, #listeners, #on, #remove_all_listeners, #remove_listener
Constructor Details
#initialize(socket, options = {}) ⇒ Hybi
Returns a new instance of Hybi.
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/websocket/driver/hybi.rb', line 55
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 = {}
return unless @socket.respond_to?(:env)
sec_key = @socket.env['HTTP_SEC_WEBSOCKET_KEY']
protos = @socket.env['HTTP_SEC_WEBSOCKET_PROTOCOL']
@headers['Upgrade'] = 'websocket'
@headers['Connection'] = 'Upgrade'
@headers['Sec-WebSocket-Accept'] = Hybi.generate_accept(sec_key)
if protos = @socket.env['HTTP_SEC_WEBSOCKET_PROTOCOL']
protos = protos.split(/ *, */) if String === protos
@protocol = protos.find { |p| @protocols.include?(p) }
@headers['Sec-WebSocket-Protocol'] = @protocol if @protocol
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
86
87
88
89
|
# File 'lib/websocket/driver/hybi.rb', line 86
def add_extension(extension)
@extensions.add(extension)
true
end
|
#binary(message) ⇒ Object
133
134
135
|
# File 'lib/websocket/driver/hybi.rb', line 133
def binary(message)
frame(message, :binary)
end
|
#close(reason = nil, code = nil) ⇒ Object
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/websocket/driver/hybi.rb', line 146
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
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
191
192
193
194
195
196
197
|
# File 'lib/websocket/driver/hybi.rb', line 163
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
is_text = String === buffer
message.rsv1 = message.rsv2 = message.rsv3 = false
message.opcode = OPCODES[type || (is_text ? :text : :binary)]
payload = is_text ? buffer.bytes.to_a : buffer
payload = [code].pack(PACK_FORMATS[2]).bytes.to_a + payload if code
message.data = payload.pack('C*')
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
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
126
127
|
# File 'lib/websocket/driver/hybi.rb', line 91
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
137
138
139
140
|
# File 'lib/websocket/driver/hybi.rb', line 137
def ping(message = '', &callback)
@ping_callbacks[message] = callback if callback
frame(message, :ping)
end
|
#pong(message = '') ⇒ Object
142
143
144
|
# File 'lib/websocket/driver/hybi.rb', line 142
def pong(message = '')
frame(message, :pong)
end
|
#text(message) ⇒ Object
129
130
131
|
# File 'lib/websocket/driver/hybi.rb', line 129
def text(message)
frame(message, :text)
end
|
#version ⇒ Object
82
83
84
|
# File 'lib/websocket/driver/hybi.rb', line 82
def version
"hybi-#{@socket.env['HTTP_SEC_WEBSOCKET_VERSION']}"
end
|