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
- 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.
18
19
20
|
# File 'lib/faye/websocket/api.rb', line 18
def buffered_amount
@buffered_amount
end
|
#ready_state ⇒ Object
Returns the value of attribute ready_state.
18
19
20
|
# File 'lib/faye/websocket/api.rb', line 18
def ready_state
@ready_state
end
|
#url ⇒ Object
Returns the value of attribute url.
18
19
20
|
# File 'lib/faye/websocket/api.rb', line 18
def url
@url
end
|
Instance Method Details
#close ⇒ Object
72
73
74
75
|
# File 'lib/faye/websocket/api.rb', line 72
def close
@ready_state = CLOSING unless @ready_state == CLOSED
@driver.close
end
|
#initialize(options = {}) ⇒ Object
20
21
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
|
# File 'lib/faye/websocket/api.rb', line 20
def initialize(options = {})
super()
::WebSocket::Driver.validate_options(options, [:headers, :extensions, :max_length, :ping, :proxy, :tls])
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
@ready_state = CONNECTING
@buffered_amount = 0
@driver.on(:open) { |e| open }
@driver.on(:message) { |e| receive_message(e.data) }
@driver.on(:close) { |e| begin_close(e.reason, e.code) }
@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
67
68
69
70
|
# File 'lib/faye/websocket/api.rb', line 67
def ping(message = '', &callback)
return false if @ready_state > OPEN
@driver.ping(message, &callback)
end
|
#protocol ⇒ Object
77
78
79
|
# File 'lib/faye/websocket/api.rb', line 77
def protocol
@driver.protocol || ''
end
|
#send(message) ⇒ Object
57
58
59
60
61
62
63
64
65
|
# File 'lib/faye/websocket/api.rb', line 57
def send(message)
return false if @ready_state > OPEN
case message
when Numeric then @driver.text(message.to_s)
when String then @driver.text(message)
when Array then @driver.binary(message)
else false
end
end
|
#write(data) ⇒ Object
53
54
55
|
# File 'lib/faye/websocket/api.rb', line 53
def write(data)
@stream.write(data)
end
|