Question

I have a super simple script that has pretty much what's on the Faye WebSocket GitHub page for handling closed connections:

 ws = Faye::WebSocket::Client.new(url, nil, :headers => headers)

  ws.on :open do |event|
    p [:open]
    # send ping command
    # send test command
    #ws.send({command: 'test'}.to_json)
  end 

  ws.on :message do |event|
    # here is the entry point for data coming from the server.
    p JSON.parse(event.data)
  end 

  ws.on :close do |event|
    # connection has been closed callback.
    p [:close, event.code, event.reason]
    ws = nil 
  end 

Once the client is idle for 2 hours, the server closes the connection. I can't seem to find a way to reconnect to the server once ws.on :close is triggered. Is there an easy way of going about this? I just want it to trigger ws.on :open after :close goes off.

Was it helpful?

Solution

Looking for the Faye Websocket Client implementation, there is a ping option which sends some data to the server periodically, which prevents the connection to go idle.

# Send ping data each minute
ws = Faye::WebSocket::Client.new(url, nil, headers: headers, ping: 60) 

However, if you don't want to rely on the server behaviour, since it can finish the connection even if you are sending some data periodically, you can just put the client setup inside a method and start all over again if the server closes the connection.

def start_connection
  ws = Faye::WebSocket::Client.new(url, nil, headers: headers, ping: 60)

  ws.on :open do |event|
    p [:open]
  end 

  ws.on :message do |event|
    # here is the entry point for data coming from the server.
    p JSON.parse(event.data)
  end 

  ws.on :close do |event|
    # connection has been closed callback.
    p [:close, event.code, event.reason]

    # restart the connection
    start_connection
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top