Python Forum
Help with Websocket-Client Module - Infinite Loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with Websocket-Client Module - Infinite Loop
#1
I need some help with the Websocket-Client (documentation). I have written a program to permanently get data from a websocket server. So far the program also works. At some point I get a "Ping/Pong timed out" error and my program closes. Until then, the program mostly ran for a few hours.

Now my question: How can I automatically restart the program? (creating an infinite Loop)


In the Code below, there is no URL given because I tried different URLĀ“s and always the same issue.

Thanks a lot for your help or any advice.

import websocket
from datetime import datetime

websocket.enableTrace(True)
socketurl = "..."

def on_message(wsapp, message):
    print(message)
    pass


def on_open(wsapp):
    print(f'{str(datetime.now())}   ### ONLINE ###')


def on_close(wsapp):
    print(f'{str(datetime.now())}   ### OFFLINE ###')


def on_error(wsapp, error):
    print(f'{str(error)}   ### OFFLINE ###')


def on_ping(wsapp, message):
    print(f'{str(datetime.now())}   ### Got a Ping! ###')


def on_pong(wsapp, message):
    print(f'{str(datetime.now())}   ### Send a Pong! ###')


wsapp = websocket.WebSocketApp(socketurl,
                               on_open=on_open,
                               on_message=on_message,
                               on_error=on_error,
                               on_close=on_close,
                               on_ping=on_ping,
                               on_pong=on_pong)


wsapp.run_forever(ping_interval=40, ping_timeout=30)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  websocket help johnbob 0 633 Jan-13-2024, 06:32 AM
Last Post: johnbob
  trying to stream data from a websocket to client tomtom 2 1,981 Jan-12-2023, 03:01 PM
Last Post: Vadanane
  Websocket conection closes abnormally Joshua_Omolo 2 3,820 Feb-17-2021, 08:03 AM
Last Post: Joshua_Omolo
  how to send an image from server to client using PICKLE module dafdaf 1 3,066 Jun-02-2020, 01:08 PM
Last Post: nuffink
  Websocket server not async? korenron 0 1,733 Sep-23-2019, 01:40 PM
Last Post: korenron
  define a variable before looped websocket korenron 0 1,819 Sep-22-2019, 12:53 PM
Last Post: korenron
  Send data BMP180 between client and server trought module socket smalhao 0 2,806 Jul-30-2018, 12:56 PM
Last Post: smalhao
  need to understand better __setup__.py, websocket-client, packaging penright 0 2,932 Jul-08-2018, 09:40 PM
Last Post: penright
  Pro's and Con's of some different websocket libraries penright 0 2,336 Jun-29-2018, 12:07 PM
Last Post: penright

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020