Module: Faye::WebSocket::API
- Extended by:
- Forwardable
- Includes:
- EventTarget
- Included in:
- Faye::WebSocket, Client
- Defined in:
- lib/faye/websocket/api.rb,
lib/faye/websocket/api/event.rb,
lib/faye/websocket/api/event_target.rb
Defined Under Namespace
Modules: EventTarget
Classes: CloseEvent, ErrorEvent, Event, MessageEvent, OpenEvent
Constant Summary
collapse
- CONNECTING =
0
- OPEN =
1
- CLOSING =
2
- CLOSED =
3
- CLOSE_TIMEOUT =
30
- TYPES =
{
'open' => OpenEvent,
'message' => MessageEvent,
'close' => CloseEvent,
'error' => ErrorEvent
}
Instance Attribute Summary collapse
Instance Method Summary
collapse
#add_event_listener, #add_listener, #dispatch_event, #remove_event_listener
Instance Attribute Details
#buffered_amount ⇒ Object
Returns the value of attribute buffered_amount.
20
21
22
|
# File 'lib/faye/websocket/api.rb', line 20
def buffered_amount
@buffered_amount
end
|
#ready_state ⇒ Object
Returns the value of attribute ready_state.
20
21
22
|
# File 'lib/faye/websocket/api.rb', line 20
def ready_state
@ready_state
end
|
#url ⇒ Object
Returns the value of attribute url.
20
21
22
|
# File 'lib/faye/websocket/api.rb', line 20
def url
@url
end
|
Instance Method Details
#close(code = nil, reason = nil) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/faye/websocket/api.rb', line 90
def close(code = nil, reason = nil)
code ||= 1000
reason ||= ''
unless code == 1000 or (code >= 3000 and code <= 4999)
raise ArgumentError, "Failed to execute 'close' on WebSocket: " +
"The code must be either 1000, or between 3000 and 4999. " +
"#{ code } is neither."
end
if @ready_state < CLOSING
@close_timer = EventMachine.add_timer(CLOSE_TIMEOUT) { begin_close('', 1006) }
end
@ready_state = CLOSING unless @ready_state == CLOSED
@driver.close(reason, code)
end
|
#initialize(options = {}) ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/faye/websocket/api.rb', line 22
def initialize(options = {})
@ready_state = CONNECTING
super()
::WebSocket::Driver.validate_options(options, [
:headers, :extensions, :max_length, :ping, :proxy, :tls, :binary_data_format
])
@driver = yield
if = options[:headers]
.each { |name, value| @driver.(name, value) }
end
[*options[:extensions]].each do |extension|
@driver.add_extension(extension)
end
@ping = options[:ping]
@ping_id = 0
@buffered_amount = 0
@close_params = @close_timer = @ping_timer = @proxy = @stream = nil
@onopen = @onmessage = @onclose = @onerror = nil
@driver.on(:open) { |e| open }
@driver.on(:message) { |e| receive_message(e.data) }
@driver.on(:close) { |e| begin_close(e.reason, e.code, :wait_for_write => true) }
@driver.on(:error) do |error|
emit_error(error.message)
end
if @ping
@ping_timer = EventMachine.add_periodic_timer(@ping) do
@ping_id += 1
ping(@ping_id.to_s)
end
end
end
|
#ping(message = '', &callback) ⇒ Object
85
86
87
88
|
# File 'lib/faye/websocket/api.rb', line 85
def ping(message = '', &callback)
return false if @ready_state > OPEN
@driver.ping(message, &callback)
end
|
#protocol ⇒ Object
109
110
111
|
# File 'lib/faye/websocket/api.rb', line 109
def protocol
@driver.protocol || ''
end
|
#send(message) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/faye/websocket/api.rb', line 66
def send(message)
return false if @ready_state > OPEN
case message
when Numeric then
@driver.text(message.to_s)
when String then
if message.encoding == Encoding::BINARY
@driver.binary(message)
else
@driver.text(message)
end
when Array then
@driver.binary(message)
else
false
end
end
|
#write(data) ⇒ Object
62
63
64
|
# File 'lib/faye/websocket/api.rb', line 62
def write(data)
@stream.write(data)
end
|