diff --git a/server/utils.py b/server/utils.py index a1ee54a61..e51990b62 100644 --- a/server/utils.py +++ b/server/utils.py @@ -2,6 +2,8 @@ import random import string +from aiohttp.web import WebSocketResponse + from game import MAX_PLY try: @@ -22,6 +24,12 @@ log = logging.getLogger(__name__) +class MyWebSocketResponse(WebSocketResponse): + @property + def closed(self): + return self._closed or self._req is None or self._req.transport is None + + async def tv_game(db, app): """ Get latest played game id """ if app["tv"] is not None: diff --git a/server/wsl.py b/server/wsl.py index 25010ee0d..5811ddf7f 100644 --- a/server/wsl.py +++ b/server/wsl.py @@ -5,14 +5,13 @@ import aiohttp from aiohttp import web -from aiohttp.web import WebSocketResponse import aiohttp_session from broadcast import lobby_broadcast from const import STARTED from seek import challenge, create_seek, get_seeks, Seek from user import User -from utils import new_game, load_game, online_count +from utils import new_game, load_game, online_count, MyWebSocketResponse log = logging.getLogger(__name__) @@ -38,7 +37,7 @@ async def lobby_socket_handler(request): sockets = request.app["lobbysockets"] seeks = request.app["seeks"] - ws = WebSocketResponse(heartbeat=3.0, receive_timeout=10.0) + ws = MyWebSocketResponse(heartbeat=3.0, receive_timeout=10.0) ws_ready = ws.can_prepare(request) if not ws_ready.ok: diff --git a/server/wsr.py b/server/wsr.py index ae1d28112..7b8e58519 100644 --- a/server/wsr.py +++ b/server/wsr.py @@ -7,7 +7,6 @@ import aiohttp from aiohttp import web -from aiohttp.web import WebSocketResponse import aiohttp_session from broadcast import lobby_broadcast, round_broadcast @@ -15,7 +14,7 @@ from fairy import WHITE, BLACK from seek import challenge, Seek from user import User -from utils import analysis_move, play_move, draw, new_game, load_game, tv_game, tv_game_user, online_count +from utils import analysis_move, play_move, draw, new_game, load_game, tv_game, tv_game_user, online_count, MyWebSocketResponse log = logging.getLogger(__name__) @@ -30,7 +29,7 @@ async def round_socket_handler(request): games = request.app["games"] db = request.app["db"] - ws = WebSocketResponse(heartbeat=3.0, receive_timeout=10.0) + ws = MyWebSocketResponse(heartbeat=3.0, receive_timeout=10.0) ws_ready = ws.can_prepare(request) if not ws_ready.ok: