river7816
river7816

Reputation: 53

How to send custome ping message using python websockets?

I am using websockets==13.1 and have read the documentation. The websockets library provides a keep-alive function to maintain the connection, but the ping message sent by keep-alive is an empty ByteFrame. How can I modify the following code to send a custom message like {"op": "ping"}?

async for websocket in websockets.connect(
            uri=self._base_url,
            ping_interval=self._ping_interval,
            ping_timeout=self._ping_timeout,
            close_timeout=self._close_timeout,
            max_queue=self._max_queue,
        ):
            try:
                payload = json.dumps(payload)
                await websocket.send(payload)
                async for msg in websocket:
                    msg = orjson.loads(msg)
                    print(msg)
            except websockets.ConnectionClosed:
                self._log.error(f"Connection closed, reconnecting...")

Upvotes: 0

Views: 38

Answers (0)

Related Questions