2023-11-20 08:04:04 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import abc
|
2024-04-06 02:40:29 +00:00
|
|
|
import urllib.parse
|
2023-11-20 08:04:04 +00:00
|
|
|
|
2024-04-06 02:40:29 +00:00
|
|
|
from .common import RequestHandler, Response, register_preference
|
2023-11-20 08:04:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebSocketResponse(Response):
|
|
|
|
|
|
|
|
def send(self, message: bytes | str):
|
|
|
|
"""
|
|
|
|
Send a message to the server.
|
|
|
|
|
|
|
|
@param message: The message to send. A string (str) is sent as a text frame, bytes is sent as a binary frame.
|
|
|
|
"""
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def recv(self):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
|
|
class WebSocketRequestHandler(RequestHandler, abc.ABC):
|
|
|
|
pass
|
2024-04-06 02:40:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
@register_preference(WebSocketRequestHandler)
|
|
|
|
def websocket_preference(_, request):
|
|
|
|
if urllib.parse.urlparse(request.url).scheme in ('ws', 'wss'):
|
|
|
|
return 200
|
|
|
|
return 0
|