Skip to content

Proper EM reconnect support #14

@MmKolodziej

Description

@MmKolodziej

Hey,
I've been having trouble while trying to setup proper reconnecting using your gem (using it through action_cable_client, but I believe that's irrelevant). Anyway, event machine supports reconnecting, but the method is not exposed in WebSocket::EventMachine::Client and due to this: eventmachine/eventmachine#218 implementing it is not straight forward.

This is what I've had to do in order to get stable reconnects:

require 'websocket-eventmachine-client'

EventMachine.run do
  ws = WebSocket::EventMachine::Client.connect(uri: 'ws://localhost:3000/websocket')

  ws.onclose do |code, reason|
    puts "Disconnected with status code: #{code}"
    EM.add_timer(2) do
      EventMachine.reconnect "localhost", 3000, ws
      ws.post_init
    end
  end
end

It should be pretty easy to remember the host and port and implement a simple reconnect method that will take care of firing post_init callback for the user. Let me know if this is something you'd like to see in your gem, I'll prepare a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions