Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run forever async websocket client?

Im trying to use websocket for a client. The client send some start messages, after that, he can receive messages, whenever he sends something or not. The client is async, and I got some code from doc but I have no idea of what im doing with my life.

async def wsrun(uri):
    async with websockets.connect(uri) as websocket:
        await websocket.send('hey')
        print(await websocket.recv()) # Starts receive things, not only once

asyncio.get_event_loop().run_until_complete(wsrun('wss://localhost:1515'))

The problem is, the websocket recv only shows the first thing that the server send :(

like image 883
Delsx Avatar asked Jan 27 '23 10:01

Delsx


1 Answers

While I can't help you with your life, try this:

async def wsrun(uri):
    async with websockets.connect(uri) as websocket:
        await websocket.send('hey')
        while True:
            print(await websocket.recv()) # Starts receive things, not only once

asyncio.get_event_loop().run_until_complete(wsrun('wss://localhost:1515'))
like image 124
drum Avatar answered Jan 28 '23 23:01

drum