Skip to content

Reconnect on lost network #15

@dtonon

Description

@dtonon

Hi, which is the correct way to manage a reconnect when the connection is lost?
I tried this code:

def relay_connect(relay_host)

  ws = WebSocket::Client::Simple.connect(relay_host)

  ws.on :open do |event|
    # ....
  end

  ws.on :message do |msg|
    # ....
  end

  ws.on :error do |event|
    
    puts "Error, reconnecting..."
    sleep(5)
    ws = nil
    relay_connect(relay_host)
  end

  ws.on :close do |event|
    # p [:close, event.code, event.reason]
    puts "Reconnecting..."
    sleep(5)
    ws = nil
    relay_connect(relay_host)
  end

end

relay_connect(relay_host)

loop do
  STDIN.gets.strip
end

But it does not work.
It doesn't give any output when I block the network, while the CPU spikes up at 100%.
Any help :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions