Skip to content

Commit

Permalink
ws_server: fix for latest websockets version
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbarry committed Feb 8, 2025
1 parent aae31b2 commit 0261dff
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/server/ws/ws_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime
import collections
import websockets
import websockets.asyncio.server
import random
import ssl
import string
Expand Down Expand Up @@ -97,7 +98,7 @@ async def client_left(websocket, session_id, client_name):
await send_msg(ws, "ctrl", 'player_left:%s' % client_name)
# TODO catch websockets.exceptions.ConnectionClosedError here?

async def client_handler(websocket, path):
async def client_handler(websocket):
global total_connections
our_name = get_ws_name(websocket)
log('Received connection from %r' % our_name)
Expand Down Expand Up @@ -204,16 +205,19 @@ async def client_handler(websocket, path):
""")

#port = 55433
log('Hosting server on port %d' % args.port)
ws_server = websockets.serve(client_handler, host=None, port=args.port, ssl=ssl_ctx)
loop = asyncio.get_event_loop()
try:
loop.run_until_complete(ws_server)
loop.run_forever()
except KeyboardInterrupt:
log("KeyboardInterrupt received, exiting.")
if total_connections != 0: log("Note that %d connections will be lost" % total_connections)
except Exception as e:
log("Exception %r encountered, exiting script" % e)
if total_connections != 0: log("Note that %d connections will be lost" % total_connections)
raise
async def main():
async with websockets.asyncio.server.serve(client_handler, host=None, port=args.port, ssl=ssl_ctx) as server:
log('Hosting server on port %d' % args.port)
try:
await server.serve_forever()
except KeyboardInterrupt:
log("KeyboardInterrupt received, exiting.")
if total_connections != 0: log("Note that %d connections will be lost" % total_connections)
except Exception as e:
log("Exception %r encountered, exiting script" % e)
if total_connections != 0: log("Note that %d connections will be lost" % total_connections)
raise


if __name__ == '__main__':
asyncio.run(main())

0 comments on commit 0261dff

Please sign in to comment.