Class: SocketIO::Client
- Inherits:
-
Object
- Object
- SocketIO::Client
- Defined in:
- lib/SocketIO.rb
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Method Summary collapse
- #after_start(&block) ⇒ Object
- #before_start(&block) ⇒ Object
- #connect_transport ⇒ Object
- #disconnect ⇒ Object
- #disconnected ⇒ Object
-
#initialize(uri, session_id, heartbeat_timeout, connection_timeout, supported_transports, options = {}) ⇒ Client
constructor
The state of the Socket.IO socket can be disconnected, disconnecting, connected and connecting.
- #join ⇒ Object
- #on_ack(&block) ⇒ Object
- #on_connect(&block) ⇒ Object
- #on_disconnect(&block) ⇒ Object
- #on_error(&block) ⇒ Object
- #on_event(name, &block) ⇒ Object
- #on_heartbeat(&block) ⇒ Object
- #on_json_message(&block) ⇒ Object
- #on_message(&block) ⇒ Object
- #on_noop(&block) ⇒ Object
- #send_event(name, hash) ⇒ Object (also: #emit)
- #send_heartbeat ⇒ Object
- #send_json_message(hash) ⇒ Object
- #send_message(string) ⇒ Object (also: #send)
- #start ⇒ Object
- #start_recieve_loop ⇒ Object
Constructor Details
#initialize(uri, session_id, heartbeat_timeout, connection_timeout, supported_transports, options = {}) ⇒ Client
The state of the Socket.IO socket can be disconnected, disconnecting, connected and connecting. The transport connection can be closed, closing, open, and opening.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/SocketIO.rb', line 34 def initialize(uri, session_id, heartbeat_timeout, connection_timeout, supported_transports, = {}) @uri = uri namespace = uri.path.sub(%r~^/~, '') @namespace = namespace.empty? ? "socket.io" : namespace @session_id = session_id @hb_timeout = heartbeat_timeout @connect_timeout = connection_timeout @supported_transports = supported_transports = @reconnect = [:reconnect] @on_event = {} end |
Instance Method Details
#after_start(&block) ⇒ Object
133 134 135 |
# File 'lib/SocketIO.rb', line 133 def after_start(&block) @after_start = block end |
#before_start(&block) ⇒ Object
129 130 131 |
# File 'lib/SocketIO.rb', line 129 def before_start(&block) @before_start = block end |
#connect_transport ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/SocketIO.rb', line 56 def connect_transport if @supported_transports.include? "websocket" scheme = @uri.scheme == "https" ? "wss" : "ws" @transport = WebSocket.new("#{scheme}://#{@uri.host}:#{@uri.port}/socket.io/1/websocket/#{@session_id}", origin: @uri.to_s) else raise "We only support WebSockets.. and this server doesnt like web sockets.. O NO!!" end end |
#disconnect ⇒ Object
96 97 98 |
# File 'lib/SocketIO.rb', line 96 def disconnect @transport.send("0::") end |
#disconnected ⇒ Object
100 101 102 103 104 105 |
# File 'lib/SocketIO.rb', line 100 def disconnected if @reconnect connect_transport start_recieve_loop end end |
#join ⇒ Object
107 108 109 |
# File 'lib/SocketIO.rb', line 107 def join @thread.join end |
#on_ack(&block) ⇒ Object
161 162 163 |
# File 'lib/SocketIO.rb', line 161 def on_ack(&block) @on_ack = block end |
#on_connect(&block) ⇒ Object
141 142 143 |
# File 'lib/SocketIO.rb', line 141 def on_connect(&block) @on_connect = block end |
#on_disconnect(&block) ⇒ Object
137 138 139 |
# File 'lib/SocketIO.rb', line 137 def on_disconnect(&block) @on_disconnect = block end |
#on_error(&block) ⇒ Object
165 166 167 |
# File 'lib/SocketIO.rb', line 165 def on_error(&block) @on_error = block end |
#on_event(name, &block) ⇒ Object
157 158 159 |
# File 'lib/SocketIO.rb', line 157 def on_event(name, &block) @on_event[name] = block end |
#on_heartbeat(&block) ⇒ Object
145 146 147 |
# File 'lib/SocketIO.rb', line 145 def on_heartbeat(&block) @on_heartbeat = block end |
#on_json_message(&block) ⇒ Object
153 154 155 |
# File 'lib/SocketIO.rb', line 153 def (&block) = block end |
#on_message(&block) ⇒ Object
149 150 151 |
# File 'lib/SocketIO.rb', line 149 def (&block) = block end |
#on_noop(&block) ⇒ Object
169 170 171 |
# File 'lib/SocketIO.rb', line 169 def on_noop(&block) @on_noop = block end |
#send_event(name, hash) ⇒ Object Also known as: emit
124 125 126 |
# File 'lib/SocketIO.rb', line 124 def send_event(name, hash) @transport.send("5:::#{{name: name, args: [hash]}.to_json}") # rescue false end |
#send_heartbeat ⇒ Object
111 112 113 |
# File 'lib/SocketIO.rb', line 111 def send_heartbeat @transport.send("2::") #rescue false end |
#send_json_message(hash) ⇒ Object
120 121 122 |
# File 'lib/SocketIO.rb', line 120 def (hash) @transport.send("4:::#{hash.to_json}") # rescue false end |
#send_message(string) ⇒ Object Also known as: send
115 116 117 |
# File 'lib/SocketIO.rb', line 115 def (string) @transport.send("3:::#{string}") #rescue false end |
#start ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/SocketIO.rb', line 47 def start self.instance_eval(&@before_start) if @before_start connect_transport start_recieve_loop self.instance_eval(&@after_start) if @after_start @thread.join unless [:sync] self end |
#start_recieve_loop ⇒ Object
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 91 92 93 94 |
# File 'lib/SocketIO.rb', line 65 def start_recieve_loop @thread = Thread.new() do while data = @transport.receive() decoded = Parser.decode(data) case decoded[:type] when '0' @on_disconnect.call if @on_disconnect when '1' @on_connect.call if @on_connect when '2' send_heartbeat @on_heartbeat.call if @on_heartbeat when '3' .call decoded[:data] if when '4' .call decoded[:data] if when '5' = JSON.parse(decoded[:data]) @on_event[['name']].call ['args'] if @on_event[['name']] when '6' @on_ack.call if @on_ack when '7' @on_error.call decoded[:data] if @on_error when '8' @on_noop.call if @on_noop end end end @thread end |