action_cable_client icon indicating copy to clipboard operation
action_cable_client copied to clipboard

reconnect! method not calling the connected callback?

Open damuz91 opened this issue 5 years ago • 1 comments

Hi, Just saw the PR that implemented the reconnect! method. I am actually using it but i noticed it doesn't calls back the connected callback. What i am doing to trigger the disconnect the callback is just shutting down the ActionCable server in a different process, then turning it back on immediately.

Here is my sample code:

require 'action_cable_client'

EventMachine.run do

  @connected = false
  device_id = 1
  uri = 'ws://localhost:3001/cable'
  params = {
    channel: 'DevicesChannel'
  }
  headers = {
    :Origin => 'ws://localhost:3001/cable',
    :device_id => device_id
  }

  client = ActionCableClient.new(uri, params, true, headers)

  client.connected do
    @connected = true
    puts 'successfully connected.'
  end

  client.received do |message|
    p message
  end

  client.subscribed do
    puts "-> Subscribed!"
  end

  client.disconnected do
    @connected = false
    while !@connected
      puts "-> Disconnected, trying to reconnect in 5 seconds"
      sleep 5
      client.reconnect!
      puts "-> Client inspect: #{client.inspect}"
    end
  end

  client.errored do |e|
    @connected = false
    puts "-> Errored #{e}."
  end

end

I expected the reconnect method to start over all callbacks again. Maybe i need to subscribe all callbacks again? I think that would cause infinite loops.

Here is my console output:

Davids-MacBook-Air:keypad-firmware damuz91$ ruby app/keypad_socket.rb 
successfully connected.
-> Subscribed!
-> Disconnected, trying to reconnect in 15 seconds
-> Client inspect: #<ActionCableClient:0x00007fd6be9c8a28 @_uri="ws://localhost:3001/cable", @message_queue=[], @_subscribed=false, @_message_factory=#<ActionCableClient::MessageFactory:0x00007fd6be9c8988 @channel={:channel=>"KeypadsChannel"}, @identifier={:channel=>"KeypadsChannel"}>, @_websocket_client=#<WebSocket::EventMachine::Client:0x00007fd6be9c85a0 @signature=3, @args={:uri=>"ws://localhost:3001/cable", :headers=>{:Origin=>"ws://localhost:3001/cable", :keypad_id=>1}, :tls=>{}}, @state=:connecting, @handshake=<WebSocket::Handshake::Client:0x3feb5f83fcb8 @uri="ws://localhost:3001/cable", @headers={:Origin=>"ws://localhost:3001/cable", :device_id=>1}, @tls={}, @state=:new, @handler=#<WebSocket::Handshake::Handler::Client11:0x00007fd6bf07ef48 @handshake=<WebSocket::Handshake::Client:0x3feb5f83fcb8 @uri="ws://localhost:3001/cable", @headers={:Origin=>"ws://localhost:3001/cable", :device_id=>1}, @tls={}, @state=:new, @handler=#<WebSocket::Handshake::Handler::Client11:0x00007fd6bf07ef48 ...>, @data="", @protocols=[], @secure=false, @host="localhost", @port=3001, @path="/cable", @query=nil, @version=13>>, @data="", @protocols=[], @secure=false, @host="localhost", @port=3001, @path="/cable", @query=nil, @version=13>, @onclose=#<Proc:0x00007fd6bf840498@/Users/damuz91/.rvm/gems/ruby-2.6.0/gems/action_cable_client-3.1.0/lib/action_cable_client.rb:64>, @onmessage=#<Proc:0x00007fd6bf84b140@/Users/damuz91/.rvm/gems/ruby-2.6.0/gems/action_cable_client-3.1.0/lib/action_cable_client.rb:92>, @onerror=#<Proc:0x00007fd6bf84a600@app/keypad_socket.rb:52>, @frame=<WebSocket::Frame::Incoming::Client:0x3feb6040d6a8 @decoded=false, @code=nil, @data="", @version=13, @handler=#<WebSocket::Frame::Handler::Handler07:0x00007fd6bf064b70 @frame=<WebSocket::Frame::Incoming::Client:0x3feb6040d6a8 @decoded=false, @code=nil, @data="", @version=13, @handler=#<WebSocket::Frame::Handler::Handler07:0x00007fd6bf064b70 ...>>>>>, @_connected_callback=#<Proc:0x00007fd6bf84b5a0@/Users/damuz91/.rvm/gems/ruby-2.6.0/gems/action_cable_client-3.1.0/lib/action_cable_client.rb:107>, @_subscribed_callback=#<Proc:0x00007fd6bf84afb0@app/keypad_socket.rb:37>, @_disconnected_callback=#<Proc:0x00007fd6bf84aad8@/Users/damuz91/.rvm/gems/ruby-2.6.0/gems/action_cable_client-3.1.0/lib/action_cable_client.rb:143>>
-> Disconnected, trying to reconnect in 5 seconds

damuz91 avatar Feb 08 '20 15:02 damuz91

Would you be willing to submit a PR? :D

NullVoxPopuli avatar Jul 02 '20 00:07 NullVoxPopuli