我正在尝试为客户端实现一个具有多个线程的websocket服务器,每当我创建一个新线程时,由于正在获取协程对象,因此无法在套接字上接收任何消息
服务器代码
import ServerThread
import asyncio
import websockets
async def consumer_handler(websocket,path):
new_con = ServerThread.Server(websocket)
new_con.start()
start_server = websockets.serve(consumer_handler, '{ip}', 5050)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
这是服务器线程逻辑,我刚刚拔出了相关代码
class Server(Thread):
def __init__(self, connection):
Thread.__init__(self)
self.con = connection
def run(self):
# Receive first initial message with connection details in for of a
# json object - { type -> passenger type (driver = 0 ,passenger = 1) , route -> current route }
client_type = json.loads(self.con.recv())
if client_type["type"] == 1:
print(" [REGISTERING CLIENT] connected a passenger")
这是错误已完全消除
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/ClientThread.py", line 25, in run
client_type = json.loads(self.con.recv())
File "/usr/lib/python3.7/json/__init__.py", line 341, in loads
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
TypeError: the JSON object must be str, bytes or bytearray, not coroutine