Skip to content

Commit

Permalink
Add compatibility with websockets 11.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
aaugustin committed Nov 27, 2022
1 parent ad4e526 commit 7df55d8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
18 changes: 12 additions & 6 deletions sanic/server/protocols/websocket_protocol.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from typing import TYPE_CHECKING, Optional, Sequence, cast

from websockets.connection import CLOSED, CLOSING, OPEN
from websockets.server import ServerConnection

try: # websockets < 11.0
from websockets.connection import CLOSED, CLOSING, OPEN
from websockets.server import ServerConnection as ServerProtocol
except ImportError: # websockets >= 11.0
from websockets.protocol import CLOSED, CLOSING, OPEN
from websockets.server import ServerProtocol

from websockets.typing import Subprotocol

from sanic.exceptions import ServerError
Expand Down Expand Up @@ -90,7 +96,7 @@ async def websocket_handshake(
try:
if subprotocols is not None:
# subprotocols can be a set or frozenset,
# but ServerConnection needs a list
# but ServerProtocol needs a list
subprotocols = cast(
Optional[Sequence[Subprotocol]],
list(
Expand All @@ -100,13 +106,13 @@ async def websocket_handshake(
]
),
)
ws_conn = ServerConnection(
ws_proto = ServerProtocol(
max_size=self.websocket_max_size,
subprotocols=subprotocols,
state=OPEN,
logger=logger,
)
resp: "http11.Response" = ws_conn.accept(request)
resp: "http11.Response" = ws_proto.accept(request)
except Exception:
msg = (
"Failed to open a WebSocket connection.\n"
Expand All @@ -129,7 +135,7 @@ async def websocket_handshake(
else:
raise ServerError(resp.body, resp.status_code)
self.websocket = WebsocketImplProtocol(
ws_conn,
ws_proto,
ping_interval=self.websocket_ping_interval,
ping_timeout=self.websocket_ping_timeout,
close_timeout=self.websocket_timeout,
Expand Down
13 changes: 10 additions & 3 deletions sanic/server/websockets/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@
Union,
)

from websockets.connection import CLOSED, CLOSING, OPEN, Event
from websockets.exceptions import (
ConnectionClosed,
ConnectionClosedError,
ConnectionClosedOK,
)
from websockets.frames import Frame, Opcode
from websockets.server import ServerConnection


try: # websockets < 11.0
from websockets.connection import CLOSED, CLOSING, OPEN, Event
from websockets.server import ServerConnection as ServerProtocol
except ImportError: # websockets >= 11.0
from websockets.protocol import CLOSED, CLOSING, OPEN, Event
from websockets.server import ServerProtocol

from websockets.typing import Data

from sanic.log import error_logger, logger
Expand All @@ -30,7 +37,7 @@


class WebsocketImplProtocol:
connection: ServerConnection
connection: ServerProtocol
io_proto: Optional[SanicProtocol]
loop: Optional[asyncio.AbstractEventLoop]
max_queue: int
Expand Down

0 comments on commit 7df55d8

Please sign in to comment.