Middleware

convert any WSGI compatible web framework to Websocket+HTTP framework using middleware.

works with many WSGI compatible servers

can used with any WSGIarrow-up-right compatible web framework

Flask, Django, Pyramid, Bottle, ... supported

This middleware adds wsgi.websocketarrow-up-right variable to WSGIarrow-up-right environment dictionary.

example with bottlearrow-up-right:

from bottle import request, Bottle
from wsocket import WSocketApp, WebSocketError, logger, run
from time import sleep

logger.setLevel(10)  # for debugging

bottle = Bottle()
app = WSocketApp(bottle)
# app = WSocketApp(bottle, "WAMP")

@bottle.route("/")
def handle_websocket():
    wsock = request.environ.get("wsgi.websocket")
    if not wsock:
        return "Hello World!"

    while True:
        try:
            message = wsock.receive()
            if message != None:
                print("participator : " + message)
                
            wsock.send("you : "+message)
            sleep(2)
            wsock.send("you : "+message)
            
        except WebSocketError:
            break
            
run(app)
circle-info

you should use HTTP version 1.1 Server with your WSGIarrow-up-right framework for some clients like Firefox browser

for examples on other web frameworks visit examples/frameworksarrow-up-right folder

WSocketApp

Class variables

GUID - unique ID to generate websocket accept key

SUPPORTED_VERSIONS - 13, 8 or 7

websocket_class - "wsgi.websocket" in WSGI Environ

Last updated