[英]How to run forever async websocket client?
我試圖為客戶端使用websocket。 客戶端發送一些啟動消息,之后,無論何時發送或不發送消息,他都可以接收消息。 客戶端是異步的,我從doc獲得了一些代碼,但我不知道我在做什么。
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'))
問題是,websocket recv只顯示服務器發送的第一件事:(
雖然我無法幫助你,但試試這個:
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'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.