Skip to content

Commit

Permalink
Improved documentation for subprotocol negotiation
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Aug 8, 2022
1 parent e0f82b2 commit c747854
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/simple_websocket/ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ class Base:
def __init__(self, sock=None, connection_type=None, receive_bytes=4096,
ping_interval=None, max_message_size=None,
thread_class=None, event_class=None, selector_class=None):
self.sock = sock
#: The name of the subprotocol chosen for the WebSocket connection.
self.subprotocol = None

self.sock = sock
self.receive_bytes = receive_bytes
self.ping_interval = ping_interval
self.max_message_size = max_message_size
Expand Down Expand Up @@ -131,16 +133,9 @@ def close(self, reason=None, message=None):
self.connected = False

def choose_subprotocol(self, request): # pragma: no cover
"""Choose a subprotocol to use for the WebSocket connection.
The default implementation does not accept any subprotocols. Subclasses
can override this method to implement subprotocol negotiation.
:param request: A ``Request`` object.
The method should return the subprotocol to use, or ``None`` if no
subprotocol is chosen.
"""
# The method should return the subprotocol to use, or ``None`` if no
# subprotocol is chosen. Can be overridden by subclasses that implement
# the server-side of the WebSocket protocol.
return None

def _thread(self):
Expand Down Expand Up @@ -336,6 +331,17 @@ def handshake(self):
self.connected = self._handle_events()

def choose_subprotocol(self, request):
"""Choose a subprotocol to use for the WebSocket connection.
The default implementation selects the first protocol request by the
client that is accepted by the server. Subclasses can override this
method to implement a different subprotocol negotiation algorithm.
:param request: A ``Request`` object.
The method should return the subprotocol to use, or ``None`` if no
subprotocol is chosen.
"""
for subprotocol in request.subprotocols:
if subprotocol in self.subprotocols:
return subprotocol
Expand Down

0 comments on commit c747854

Please sign in to comment.