Skip to content

Commit

Permalink
Better Ctrl-C handling in signaling_server (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
philippewarren authored Jul 15, 2024
1 parent 162a2e0 commit 9a296d6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.2
1.2.3
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ def __init__(self, web_socket_client_manager):

self._lock = None

async def clear_all(self):
self._create_lock_if_none()
async with self._lock:
self._room_by_id.clear()
self._ids_by_room.clear()
self._client_name_by_id.clear()
self._client_datum_by_id.clear()

async def add_client(self, id, client_name, client_data, room):
self._create_lock_if_none()
async with self._lock:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ async def get_ice_servers(request: web.Request):
return web.json_response([])


async def on_shutdown(app):
await web_socket_client_manager.close_all()
await room_manager.clear_all()


def _isAuthorized(user_password):
return password is None or user_password == password

Expand Down Expand Up @@ -310,13 +315,16 @@ def main(other_routes=None):
if args.static_folder is not None:
app.add_routes([web.static('/', args.static_folder, follow_symlinks=args.follow_symlinks)])

# Shutdown callback
app.on_shutdown.append(on_shutdown)

# Run app
if using_tls:
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(args.certificate, args.key)
web.run_app(app, port=args.port, ssl_context=ssl_context)
web.run_app(app, port=args.port, ssl_context=ssl_context, shutdown_timeout=2)
else:
web.run_app(app, port=args.port)
web.run_app(app, port=args.port, shutdown_timeout=2)


if __name__ == '__main__':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ async def close(self, id):
await self._ws_by_id[id].close()
del self._ws_by_id[id]

async def close_all(self):
self._create_lock_if_none()
async with self._lock:
asyncio.gather(*[ws.close() for ws in self._ws_by_id.values()])
self._ws_by_id.clear()

async def send_to(self, message, ids):
if isinstance(ids, str):
ids = [ids]
Expand Down

0 comments on commit 9a296d6

Please sign in to comment.