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,
lib/websocket/driver/hybi/stream_reader.rb
Direct Known Subclasses
Client
Defined Under Namespace
Classes: Frame, Message, 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
}
- 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
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.
54
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 54
def initialize(socket, options = {})
super
@extensions = ::WebSocket::Extensions.new
@reader = StreamReader.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
11
12
13
|
# File 'lib/websocket/driver/hybi.rb', line 11
def self.generate_accept(key)
Base64.encode64(Digest::SHA1.digest(key + GUID)).strip
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
132
133
134
|
# File 'lib/websocket/driver/hybi.rb', line 132
def binary(message)
frame(message, :binary)
end
|
#close(reason = nil, code = nil) ⇒ Object
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
# File 'lib/websocket/driver/hybi.rb', line 141
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(data, type = nil, code = nil) ⇒ Object
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
191
|
# File 'lib/websocket/driver/hybi.rb', line 158
def frame(data, type = nil, code = nil)
return queue([data, type, code]) if @ready_state <= 0
return false unless @ready_state == 1
message = Message.new
frame = Frame.new
is_text = String === data
message.rsv1 = message.rsv2 = message.rsv3 = false
message.opcode = OPCODES[type || (is_text ? :text : :binary)]
payload = is_text ? data.bytes.to_a : data
if code
payload = [(code >> 8) & BYTE, code & BYTE, *payload]
end
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
end
|
#parse(data) ⇒ 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
|
# File 'lib/websocket/driver/hybi.rb', line 91
def parse(data)
@reader.put(data)
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
@frame.masking_key = buffer
@stage = 4
end
when 4 then
buffer = @reader.read(@frame.length)
if buffer
emit_frame(buffer)
@stage = 0
end
else
buffer = nil
end
end
end
|
#ping(message = '', &callback) ⇒ Object
136
137
138
139
|
# File 'lib/websocket/driver/hybi.rb', line 136
def ping(message = '', &callback)
@ping_callbacks[message] = callback if callback
frame(message, :ping)
end
|
#text(message) ⇒ Object
128
129
130
|
# File 'lib/websocket/driver/hybi.rb', line 128
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
|