python-hyper / wsproto

Sans-IO WebSocket protocol implementation
https://wsproto.readthedocs.io/
MIT License
261 stars 38 forks source link

feature request: implement `response()` method for `Request` event #172

Closed crosser closed 2 years ago

crosser commented 2 years ago

Of three events that require "trivial" response, Request, CloseConnection, and Ping, two implement response(), but one, Request, does not. If it did had response() method that returns AcceptConnection(), event processing workflow could be simpler than it has to be now:

for event in ws.events():
    if isinstance(event, (CloseConnection, Ping, Request)):
        write_data += ws.send(event.response())
    elif isinstance(event, TextMessage):
        # meningful processing

but as it is in the version 1.0.0, we have to have an extra elif for the Request case, that always sends AcceptConnection().

pgjones commented 2 years ago

I think the current API is best as to accept the connection the subprotocol, and extensions should be considered. Whereas the response is very clear for Ping and CloseConnection events.