python-websockets / websockets

Library for building WebSocket servers and clients in Python

Home Page:https://websockets.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[QUESTION] Switching from websocket-client to websockets

FuckingToasters opened this issue · comments

Hello, so i have an existing codebase which uses websocket-client which i want to change to use websockets.

Now to intiialize the websocket session with websocket-client, the codebase uses following:

self.ws = websocket.WebSocketApp(
            'wss://url.com',
            on_open=self.on_open,
            on_message=self.on_message,
            on_error=self.on_error,
            on_close=self.on_close,
            header={'Origin': 'https://url.com'}
        )

self.on_open are custom functions which should run whenever a connection is opend, closed etc (I did nto found such a thing on websockets)

I added following new way of handeling the connections:

with connect("wss://url.com") as websocket:
            self.ws = websocket
            self.token = token

outside the with context i made a send function which i call multiple times:

def send(self, payload):
        self.ws.send(json.dumps(payload))

Now my Question is, is there something like there is with websocket-client with which you cxan call custom functions when the connection si coped, closed etc and if so, how?