Class: WSocketIO::Client
- Inherits:
-
Object
- Object
- WSocketIO::Client
- Defined in:
- lib/wsocket_io.rb
Overview
─── Client ─────────────────────────────────────────────────
Instance Attribute Summary collapse
-
#pubsub ⇒ Object
readonly
Returns the value of attribute pubsub.
Instance Method Summary collapse
- #channel(name) ⇒ Object
- #configure_push(base_url:, token:, app_id:) ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
-
#initialize(url, api_key, options = Options.new) ⇒ Client
constructor
A new instance of Client.
- #on_connect(&block) ⇒ Object
- #on_disconnect(&block) ⇒ Object
- #on_error(&block) ⇒ Object
Constructor Details
#initialize(url, api_key, options = Options.new) ⇒ Client
Returns a new instance of Client.
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/wsocket_io.rb', line 268 def initialize(url, api_key, = Options.new) @url = url @api_key = api_key @options = @channels = {} @subscribed_channels = {} @last_message_ts = 0 @reconnect_attempts = 0 @connected = false @ws = nil @on_connect_cbs = [] @on_disconnect_cbs = [] @on_error_cbs = [] @pubsub = PubSubNamespace.new(self) end |
Instance Attribute Details
#pubsub ⇒ Object (readonly)
Returns the value of attribute pubsub.
266 267 268 |
# File 'lib/wsocket_io.rb', line 266 def pubsub @pubsub end |
Instance Method Details
#channel(name) ⇒ Object
324 325 326 |
# File 'lib/wsocket_io.rb', line 324 def channel(name) @channels[name] ||= Channel.new(name, method(:send_msg)) end |
#configure_push(base_url:, token:, app_id:) ⇒ Object
328 329 330 |
# File 'lib/wsocket_io.rb', line 328 def configure_push(base_url:, token:, app_id:) PushClient.new(base_url: base_url, token: token, app_id: app_id) end |
#connect ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'lib/wsocket_io.rb', line 291 def connect ws_url = @url.dup ws_url += @url.include?('?') ? '&' : '?' ws_url += "key=#{@api_key}" ws_url += "&token=#{@options.token}" if @options.token client = self @ws = WebSocket::Client::Simple.connect(ws_url) do |ws| ws.on :open do client.send(:handle_open) end ws.on :message do |msg| client.send(:handle_raw_message, msg.data) end ws.on :close do |e| client.send(:handle_close, e) end ws.on :error do |e| client.send(:handle_error, e) end end self end |
#connected? ⇒ Boolean
289 |
# File 'lib/wsocket_io.rb', line 289 def connected? = @connected |
#disconnect ⇒ Object
319 320 321 322 |
# File 'lib/wsocket_io.rb', line 319 def disconnect @connected = false @ws&.close end |
#on_connect(&block) ⇒ Object
286 |
# File 'lib/wsocket_io.rb', line 286 def on_connect(&block) = (@on_connect_cbs << block; self) |
#on_disconnect(&block) ⇒ Object
287 |
# File 'lib/wsocket_io.rb', line 287 def on_disconnect(&block) = (@on_disconnect_cbs << block; self) |
#on_error(&block) ⇒ Object
288 |
# File 'lib/wsocket_io.rb', line 288 def on_error(&block) = (@on_error_cbs << block; self) |