Python Forum
How to send a pong on websocket-client
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to send a pong on websocket-client
#1
I have made a request to Binance websocket using the python websocket client, but the problem is that the websocket connection use to disconnect automatically.

When i check the documentation of the host they said i have to send a pong frame every time the server send a ping, and they said that the server use to send a ping every 3 mins.

Heres the section of the doc:
   

Here's my try code:
all_market_tickers = '!ticker@arr'
url = f"wss://stream.binance.com:9443/ws"


subscribe = {
    "method": "SUBSCRIBE",
    "params":
        [
            all_market_tickers
        ],
    "id": 1
}


def on_message(ws, mesg):
  #do something with mesg received
  print(mesg) 




def on_open(ws):
    print("Opened connection")
    ws.send(json.dumps(subscribe))


def on_ping(ws, result):
    print('############# I HAVE BEEN PINGED') #I have not seen this line print before 
    print(result)
    ws.send(json.dumps(subscribe))


def on_pong(ws, pongista):
    ws.send(json.dumps(subscribe))


def on_close(ws):
    print("### closed ###")


def on_error(ws, error):
    print(error)
    print("They is err on_error")


def stream(url):
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp(url,
                                on_open=on_open,
                                on_message=on_message,
                                on_error=on_error,
                                on_close=on_close,
                                on_ping=on_ping,
                                on_pong=on_pong)

    ws.run_forever(dispatcher=rel)  # Set dispatcher to automatic reconnection
    rel.signal(2, rel.abort)  # Keyboard Interrupt
    rel.dispatch()


if __name__ == "__main__":
    stream(url)
The connection works normally but it keeps disconnecting automatically.
I'm confident that I'm not doing the right thing here which is sending a pong when the server send a ping>
Please how can i send a pong?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with my pong game code Than999 8 3,876 May-15-2022, 06:40 AM
Last Post: deanhystad
  Can't write to .txt after opening websocket gerald 5 1,593 May-03-2022, 12:43 PM
Last Post: deanhystad
  asyncio calls within sync function ( Websocket on_open) orion67 0 1,427 Jan-16-2022, 11:00 AM
Last Post: orion67
  Help with WebSocket reading data from anoter function korenron 0 1,345 Sep-19-2021, 11:08 AM
Last Post: korenron
  Pong game buss0140 7 4,140 Dec-27-2020, 07:04 AM
Last Post: ndc85430
  scoring issues in pong wildbill 1 2,212 Aug-05-2019, 01:48 AM
Last Post: metulburr
  PING PONG GAME akea 0 5,705 May-08-2019, 04:30 PM
Last Post: akea
  Python project "pong" without any makefile sylas 5 5,018 Nov-28-2017, 05:55 PM
Last Post: Larz60+
  Data through a websocket darkknight 1 2,408 Sep-28-2017, 08:00 PM
Last Post: nilamo
  ping and pong run both well sylas 1 3,183 Sep-24-2017, 05:14 PM
Last Post: sylas

Forum Jump:

User Panel Messages

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