Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Websocket subprotocol support #874

Closed
hballard opened this issue Jul 30, 2017 · 3 comments
Closed

Websocket subprotocol support #874

hballard opened this issue Jul 30, 2017 · 3 comments
Milestone

Comments

@hballard
Copy link

The websockets library that Sanic utilizes supports passing a list of acceptable websocket subprotocols into their server or client implementations, to be used during the opening handshake ("Sec-WebSocket-Protocol"). Sanic does not support this currently and does not do anything w/ subprotocols in the websocket_handshake method. The eventual websocket object that gets passed to the Sanic handler has an "subprotocol" attribute, which would normally be set during the handshake. Currently it is empty. Is there any plan to support subprotocols in the future? Thanks for the excellent framework!

@yunstanford
Copy link
Member

hmm.. seems like Sanic is using WebSocketCommonProtocol, not WebSocketServerProtocol. But WebSocketCommonProtocol doesn't support to pass in subprotocols directly (we can hook it up if really needs). @r0fls is there any reason to use WebSocketCommonProtocol, instead of WebSocketServerProtocol ?

Also , https://github.com/channelcat/sanic/blob/master/sanic/websocket.py#L48 this piece of code is mainly from the implementation of WebSocketServerProtocol.

@messense
Copy link
Contributor

cc @miguelgrinberg

@miguelgrinberg
Copy link
Contributor

Yes, the support for negotiating a subprotocol is in the WebSocketServerProtocol class, which I think would be harder to integrate with the sanic request object than WebSocketCommonProtocol, which only has the low-level WebSocket support.

What I think makes the most sense here is to implement the negotiation of the subprotocol in websocket_handshake. We can add a subprotocols argument to the @websocket decorator so that the application can specify what subprotocols it accepts. Example:

@app.websocket('/feed', subprotocols=['foo', 'bar'])
async def feed(request, ws):
    if ws.subprotocol:
        print('The subprotocol is: ', ws.subprotocol)
    while True:
        data = 'hello!'
        print('Sending: ' + data)
        await ws.send(data)
        data = await ws.recv()
        print('Received: ' + data)

miguelgrinberg added a commit to miguelgrinberg/sanic that referenced this issue Aug 8, 2017
miguelgrinberg added a commit to miguelgrinberg/sanic that referenced this issue Aug 8, 2017
miguelgrinberg added a commit to miguelgrinberg/sanic that referenced this issue Aug 8, 2017
@seemethere seemethere modified the milestone: 0.6.1 Aug 8, 2017
@seemethere seemethere modified the milestones: 0.6.1, 0.7.0 Nov 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants