我正在尝试使用websockets包在Python上构建一个websockets客户端:Websockets 4.0 API
我使用这种方式而不是示例代码,因为我想创建一个websocket客户机类对象,并将其用作网关。在
我在客户端的侦听器方法(receiveMessage)有问题,这会在执行时引发ConnectionClose异常。我想也许这个循环有什么问题。在
这是我尝试构建的简单webSocket客户端:import websockets
class WebSocketClient():
def __init__(self):
pass
async def connect(self):
'''
Connecting to webSocket server
websockets.client.connect returns a WebSocketClientProtocol, which is used to send and receive messages
'''
self.connection = await websockets.client.connect('ws://127.0.0.1:8765')
if self.connection.open:
print('Connection stablished. Client correcly connected')
# Send greeting
await self.sendMessage('Hey server, this is webSocket client')
# Enable listener
await self.receiveM