Клиенты получают тайм-аут при попытке подключения к общедоступному серверу websocket (Python)
def first(request):
PORT = 7890
print("Server listening on Port " + str(PORT))
async def echo(websocket, path):
print("A client just connected")
try:
async for message in websocket:
print("Received message from client: " + message)
await websocket.send("Pong: " + message + "\npath - " + path)
except websockets.exceptions.ConnectionClosed as e:
print("A client just disconnected")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
start_server = websockets.serve(echo, "", PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
return HttpResponse("done")
Вышеупомянутый код - это запрос Django для запуска вебсокета, который был развернут на aws elasticbeanstalk.
def main():
async def listen():
url = "http://something.com:7890"
# Connect to the server
async with websockets.connect(url) as ws:
# Send a greeting message
await ws.send("Hello oku[dated!")
# Stay alive forever, listening to incoming msgs
while True:
msg = await ws.recv()
print(msg)
# Start the connection
asyncio.get_event_loop().run_until_complete(listen())
Второй скрипт - это локально запускаемый файл для подключения вебсокета.
При попытке подключения к websocket я получаю ошибку asyncio.exceptions.TimeoutError в последней строке скрипта. Поэтому я хотел бы знать, как это можно сделать правильно.
Примечание:
- Both the website (websocket server) and client work fine when the code is run locally.
- The error is being caused only when the website (websocket server) is deployed to aws for hosting.