beardeadclown
beardeadclown

Reputation: 377

How do I send keepalive requests with websocket-client?

I need to add some code to send keepalive request every 5 seconds to the following program which is using websocket-client

def on_open(wsapp):
    wsapp.send(json.dumps(reqlogin))
    wsapp.send(json.dumps(reqsub))

def on_message(wsapp, msg):
    handle(msg)

wsapp = websocket.WebSocketApp(url, on_open=on_open, on_message=on_message)
wsapp.run_forever()

I've looked at documentation but couldn't find anything appropriate.

Upvotes: 1

Views: 740

Answers (1)

beardeadclown
beardeadclown

Reputation: 377

I managed to solve the issue using threading:

def on_open(wsapp):
    wsapp.send(json.dumps(reqlogin))
    wsapp.send(json.dumps(reqsub))

def on_message(wsapp, msg):
    handle(msg)

wsapp = websocket.WebSocketApp(url, on_open=on_open, on_message=on_message)
wsappthread = threading.Thread(target=wsapp.run_forever,
                               daemon=True)
wsappthread.start()
while True:
    time.sleep(5)
    wsapp.send(json.dumps(reqkeepalive))

Upvotes: 1

Related Questions