Skip to content

Latest commit

 

History

History
39 lines (28 loc) · 1.56 KB

websockets.rst

File metadata and controls

39 lines (28 loc) · 1.56 KB

Websockets

WebSockets are supported by slimHTTP, but enabled by a plugin.
You'll need to install slimWS one way or another.
After that, simply plug in the upgrader to slimHTTP:
import slimHTTP
import slimWS

http = slimHTTP.host(slimHTTP.HTTP)
websocket = slimWS.WebSocket()

@http.on_upgrade
def upgrade(request):
    new_identity = websocket.WS_CLIENT_IDENTITY(request)
    new_identity.upgrade(request) # Sends Upgrade request to client
    return new_identity

@websocket.frame
def on_ws_frame(frame):
    print('Got websocket data:', frame.data)

    yield {
        'status' : 'recieved'
    }

http.run()

Note

slimWS has a rudimentary API support, which can be viewed on the slimWS documentation.

The following example will catch any Connection: upgrade request,
and then proceed to in-memory replace the ~slimHTTP.HTTP_CLIENT_IDENTITY with a slimWS.WS_CLIENT_IDENTITY.

Identities are usually one-shot-sessions, but since WebSockets in general are a session based connection, the slimWS.WS_CLIENT_IDENTITY persists over requests - as there are no socket.close() event for that protocol. slimHTTP honors the keep-alive in the identity and doesn't touch the socket after each response.