Class: WebSocket::Driver
- Inherits:
-
Object
show all
- Includes:
- EventEmitter
- Defined in:
- lib/websocket/driver.rb,
lib/websocket/driver/hybi.rb,
lib/websocket/driver/client.rb,
lib/websocket/driver/server.rb,
lib/websocket/driver/draft75.rb,
lib/websocket/driver/draft76.rb,
lib/websocket/driver/headers.rb,
lib/websocket/driver/event_emitter.rb,
lib/websocket/driver/hybi/stream_reader.rb
Defined Under Namespace
Modules: EventEmitter
Classes: Client, CloseEvent, ConnectEvent, Draft75, Draft76, Headers, Hybi, MessageEvent, OpenEvent, ProtocolError, Server
Constant Summary
collapse
- STATES =
[:connecting, :open, :closing, :closed]
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#add_listener, #emit, #listener_count, #listeners, #on, #remove_all_listeners, #remove_listener
Constructor Details
#initialize(socket, options = {}) ⇒ Driver
Returns a new instance of Driver.
58
59
60
61
62
63
64
65
66
|
# File 'lib/websocket/driver.rb', line 58
def initialize(socket, options = {})
super()
@socket = socket
@options = options
@headers = Headers.new
@queue = []
@ready_state = 0
end
|
Instance Attribute Details
#protocol ⇒ Object
Returns the value of attribute protocol.
56
57
58
|
# File 'lib/websocket/driver.rb', line 56
def protocol
@protocol
end
|
#ready_state ⇒ Object
Returns the value of attribute ready_state.
56
57
58
|
# File 'lib/websocket/driver.rb', line 56
def ready_state
@ready_state
end
|
Class Method Details
.client(socket, options = {}) ⇒ Object
119
120
121
|
# File 'lib/websocket/driver.rb', line 119
def self.client(socket, options = {})
Client.new(socket, options.merge(:masking => true))
end
|
.encode(string, encoding = nil) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/websocket/driver.rb', line 138
def self.encode(string, encoding = nil)
if Array === string
string = string.pack('C*')
encoding ||= :binary
else
encoding ||= :utf8
end
case encoding
when :binary
string.force_encoding('ASCII-8BIT') if string.respond_to?(:force_encoding)
when :utf8
string.force_encoding('UTF-8') if string.respond_to?(:force_encoding)
return nil unless valid_utf8?(string)
end
string
end
|
.rack(socket, options = {}) ⇒ Object
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/websocket/driver.rb', line 127
def self.rack(socket, options = {})
env = socket.env
if env['HTTP_SEC_WEBSOCKET_VERSION']
Hybi.new(socket, options.merge(:require_masking => true))
elsif env['HTTP_SEC_WEBSOCKET_KEY1']
Draft76.new(socket, options)
else
Draft75.new(socket, options)
end
end
|
.server(socket, options = {}) ⇒ Object
123
124
125
|
# File 'lib/websocket/driver.rb', line 123
def self.server(socket, options = {})
Server.new(socket, options.merge(:require_masking => true))
end
|
.utf8_string(string) ⇒ Object
155
156
157
158
159
160
|
# File 'lib/websocket/driver.rb', line 155
def self.utf8_string(string)
string = string.pack('C*') if Array === string
string.respond_to?(:force_encoding) ?
string.force_encoding('UTF-8') :
string
end
|
.valid_utf8?(string) ⇒ Boolean
162
163
164
165
166
167
168
|
# File 'lib/websocket/driver.rb', line 162
def self.valid_utf8?(string)
if defined?(UTF8_MATCH)
UTF8_MATCH =~ string ? true : false
else
string.valid_encoding?
end
end
|
.websocket?(env) ⇒ Boolean
170
171
172
173
174
175
176
177
178
|
# File 'lib/websocket/driver.rb', line 170
def self.websocket?(env)
return false unless env['REQUEST_METHOD'] == 'GET'
connection = env['HTTP_CONNECTION'] || ''
upgrade = env['HTTP_UPGRADE'] || ''
env['REQUEST_METHOD'] == 'GET' and
connection.downcase.split(/\s*,\s*/).include?('upgrade') and
upgrade.downcase == 'websocket'
end
|
Instance Method Details
#binary(message) ⇒ Object
90
91
92
|
# File 'lib/websocket/driver.rb', line 90
def binary(message)
false
end
|
#close(reason = nil, code = nil) ⇒ Object
98
99
100
101
102
103
|
# File 'lib/websocket/driver.rb', line 98
def close(reason = nil, code = nil)
return false unless @ready_state == 1
@ready_state = 3
emit(:close, CloseEvent.new(nil, nil))
true
end
|
#ping(*args) ⇒ Object
94
95
96
|
# File 'lib/websocket/driver.rb', line 94
def ping(*args)
false
end
|
73
74
75
76
77
|
# File 'lib/websocket/driver.rb', line 73
def (name, value)
return false unless @ready_state <= 0
@headers[name] = value
true
end
|
#start ⇒ Object
79
80
81
82
83
84
|
# File 'lib/websocket/driver.rb', line 79
def start
return false unless @ready_state == 0
@socket.write(Driver.encode(handshake_response, :binary))
open unless @stage == -1
true
end
|
#state ⇒ Object
68
69
70
71
|
# File 'lib/websocket/driver.rb', line 68
def state
return nil unless @ready_state >= 0
STATES[@ready_state]
end
|
#text(message) ⇒ Object
86
87
88
|
# File 'lib/websocket/driver.rb', line 86
def text(message)
frame(message)
end
|