fix: address issue with disconnecting clients
This commit is contained in:
10
main.py
10
main.py
@@ -82,6 +82,10 @@ def create_game(game_id, player=None):
|
|||||||
async def init(websocket, path):
|
async def init(websocket, path):
|
||||||
try:
|
try:
|
||||||
async for message in websocket:
|
async for message in websocket:
|
||||||
|
if message == "ping":
|
||||||
|
print(message)
|
||||||
|
await websocket.send("pong")
|
||||||
|
else:
|
||||||
data = json.loads(message)
|
data = json.loads(message)
|
||||||
if data["action"] == "enter_game":
|
if data["action"] == "enter_game":
|
||||||
await enter_game(data["gameId"], websocket)
|
await enter_game(data["gameId"], websocket)
|
||||||
@@ -94,8 +98,10 @@ async def init(websocket, path):
|
|||||||
await exit_game(websocket.remote_address[0])
|
await exit_game(websocket.remote_address[0])
|
||||||
print("Connection closed...")
|
print("Connection closed...")
|
||||||
|
|
||||||
start_server = websockets.serve(init, "", int(os.environ["PORT"]))
|
start_server = websockets.serve(init, "", int(os.environ["PORT"]), ping_interval=None)
|
||||||
#start_server = websockets.serve(init, "localhost", 5001)
|
#start_server = websockets.serve(init, "localhost", 5001, ping_interval=None)
|
||||||
|
# ping_interval=None is important, otherwise the client will disconnect
|
||||||
|
# https://stackoverflow.com/a/58993145/11512104
|
||||||
|
|
||||||
asyncio.get_event_loop().run_until_complete(start_server)
|
asyncio.get_event_loop().run_until_complete(start_server)
|
||||||
asyncio.get_event_loop().run_forever()
|
asyncio.get_event_loop().run_forever()
|
||||||
Reference in New Issue
Block a user