如何让 WebSocket server/client 与两方通信
How to have a WebSocket server/client communicating with two parties
我在这里寻求帮助,因为我真的不知道该怎么做。
我想用 websocket 做到这一点:
我已经有了我的前端和ImageAnalyser。 ImageAnalyser 是一个 WebSocket 服务器。
在这种状态下,我的后端 (python) 必须同时是服务器和客户端。但是我没有找到任何可以使这种同步成为可能的东西。
我查看了 asyncio 和 websockets,但没有成功。有人可以帮助我吗?
事实是我需要等待“功能”的“位置”,但这就像一个等待内部客户端的内部服务器,我不知道是否可能。
谢谢
我是这样做的。不知道是不是好方法,但是确实有效
class Session:
# A session is a client for the FeatureExtractor, and a server for the Front-End.
def __init__(self, host: str, port: int, openFaceUri: str):
# Serving the front-end.
self.server = websockets.serve(self.main_server, host, port)
# Asking OpenFace
self.openFaceUri = openFaceUri
self.ws = None
# Image, features of the image. Note that features couldn't be associated to image through time.
self.img, self.features = None, None
# Flags for synchronization between client and server.
self.newImgRcvd = asyncio.Event()
self.workDone = asyncio.Event()
# Model
#self.model = Modelization()
#self.model.open().feature_engineering().create_model().train_model()
# Methods
def start(self):
asyncio.get_event_loop().run_until_complete(self.server)
asyncio.get_event_loop().run_until_complete(self.main_client())
#asyncio.get_event_loop().run_forever()
###################################################################
# For client. Keep connection open with OpenFace.
async def __async__connect(self):
'''
Keep a connection alive between the FeatureExtractor (Openface) and here.
'''
print("Attempting connection to {}".format(self.openFaceUri))
# perform async connect, and store the connected WebSocketClientProtocol
# object, for later reuse for send & recv
self.ws = await websockets.connect(self.openFaceUri)
print("Connected to Openface \n")
async def send_image_to_feature_extractor(self):
while True:
await self.newImgRcvd.wait()
self.newImgRcvd.clear()
await self.ws.send(self.img)
self.features = await self.ws.recv()
self.workDone.set()
async def main_client(self):
await self.__async__connect()
await self.send_image_to_feature_extractor()
############################### Server ####################################
async def main_server(self, websocket, path):
while True:
self.img = await websocket.recv()
self.newImgRcvd.set()
await self.workDone.wait()
self.workDone.clear()
await websocket.send(self.features)
srv = Session(host="localhost", port=8765, openFaceUri="ws://localhost:8080")
srv.start()
我在这里寻求帮助,因为我真的不知道该怎么做。
我想用 websocket 做到这一点:
我已经有了我的前端和ImageAnalyser。 ImageAnalyser 是一个 WebSocket 服务器。
在这种状态下,我的后端 (python) 必须同时是服务器和客户端。但是我没有找到任何可以使这种同步成为可能的东西。
我查看了 asyncio 和 websockets,但没有成功。有人可以帮助我吗? 事实是我需要等待“功能”的“位置”,但这就像一个等待内部客户端的内部服务器,我不知道是否可能。
谢谢
我是这样做的。不知道是不是好方法,但是确实有效
class Session:
# A session is a client for the FeatureExtractor, and a server for the Front-End.
def __init__(self, host: str, port: int, openFaceUri: str):
# Serving the front-end.
self.server = websockets.serve(self.main_server, host, port)
# Asking OpenFace
self.openFaceUri = openFaceUri
self.ws = None
# Image, features of the image. Note that features couldn't be associated to image through time.
self.img, self.features = None, None
# Flags for synchronization between client and server.
self.newImgRcvd = asyncio.Event()
self.workDone = asyncio.Event()
# Model
#self.model = Modelization()
#self.model.open().feature_engineering().create_model().train_model()
# Methods
def start(self):
asyncio.get_event_loop().run_until_complete(self.server)
asyncio.get_event_loop().run_until_complete(self.main_client())
#asyncio.get_event_loop().run_forever()
###################################################################
# For client. Keep connection open with OpenFace.
async def __async__connect(self):
'''
Keep a connection alive between the FeatureExtractor (Openface) and here.
'''
print("Attempting connection to {}".format(self.openFaceUri))
# perform async connect, and store the connected WebSocketClientProtocol
# object, for later reuse for send & recv
self.ws = await websockets.connect(self.openFaceUri)
print("Connected to Openface \n")
async def send_image_to_feature_extractor(self):
while True:
await self.newImgRcvd.wait()
self.newImgRcvd.clear()
await self.ws.send(self.img)
self.features = await self.ws.recv()
self.workDone.set()
async def main_client(self):
await self.__async__connect()
await self.send_image_to_feature_extractor()
############################### Server ####################################
async def main_server(self, websocket, path):
while True:
self.img = await websocket.recv()
self.newImgRcvd.set()
await self.workDone.wait()
self.workDone.clear()
await websocket.send(self.features)
srv = Session(host="localhost", port=8765, openFaceUri="ws://localhost:8080")
srv.start()