Class: Faye::Transport::WebSocket
Constant Summary
collapse
- WEBSOCKET_TIMEOUT =
1
- UNCONNECTED =
1
- CONNECTING =
2
- CONNECTED =
3
Constants included
from Logging
Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS
Instance Attribute Summary
#cookies, #headers
Attributes included from Logging
#log_level
Class Method Summary
collapse
Instance Method Summary
collapse
#connection_type, #flush, get, #initialize, #receive, register, #retry_block, #send, supported_connection_types
#add_timeout, #remove_timeout
Methods included from Publisher
#bind, #count_listeners, #trigger, #unbind
Methods included from Logging
#log
Class Method Details
.usable?(endpoint, &callback) ⇒ Boolean
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/faye/transport/web_socket.rb', line 12
def self.usable?(endpoint, &callback)
connected = false
called = false
socket_url = endpoint.gsub(/^http(s?):/, 'ws\1:')
socket = Faye::WebSocket::Client.new(socket_url)
socket.onopen = lambda do |event|
connected = true
socket.close
callback.call(true)
called = true
socket = nil
end
notconnected = lambda do |*args|
callback.call(false) unless called or connected
called = true
end
socket.onclose = socket.onerror = notconnected
EventMachine.add_timer(WEBSOCKET_TIMEOUT, ¬connected)
end
|
Instance Method Details
#batching? ⇒ Boolean
35
36
37
|
# File 'lib/faye/transport/web_socket.rb', line 35
def batching?
false
end
|
#close ⇒ Object
51
52
53
54
55
|
# File 'lib/faye/transport/web_socket.rb', line 51
def close
return if @closed
@closed = true
@socket.close if @socket
end
|
#connect ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/faye/transport/web_socket.rb', line 57
def connect
return if @closed
@state ||= UNCONNECTED
return unless @state == UNCONNECTED
@state = CONNECTING
@socket = Faye::WebSocket::Client.new(@endpoint.gsub(/^http(s?):/, 'ws\1:'))
@socket.onopen = lambda do |*args|
@state = CONNECTED
set_deferred_status(:succeeded, @socket)
trigger(:up)
end
@socket.onmessage = lambda do |event|
messages = [Yajl::Parser.parse(event.data)].flatten
messages.each { |message| @messages.delete(message['id']) }
receive(messages)
end
@socket.onclose = lambda do |*args|
was_connected = (@state == CONNECTED)
set_deferred_status(:deferred)
@state = UNCONNECTED
@socket = nil
next resend if was_connected
EventMachine.add_timer(@client.retry) { connect }
trigger(:down)
end
end
|
#request(messages, timeout = nil) ⇒ Object
39
40
41
42
43
44
|
# File 'lib/faye/transport/web_socket.rb', line 39
def request(messages, timeout = nil)
return if messages.empty?
@messages ||= {}
messages.each { |message| @messages[message['id']] = message }
with_socket { |socket| socket.send(Faye.to_json(messages)) }
end
|
#resend ⇒ Object
92
93
94
|
# File 'lib/faye/transport/web_socket.rb', line 92
def resend
request(@messages.values)
end
|
#with_socket(&resume) ⇒ Object
46
47
48
49
|
# File 'lib/faye/transport/web_socket.rb', line 46
def with_socket(&resume)
callback(&resume)
connect
end
|