Wsocket
  • Wsocket
  • Installation
  • Getting Started
  • Guide
    • App
    • Framework
    • Middleware
    • Server
    • Websocket
  • Changelog
Powered by GitBook
On this page
  • WSocketApp
  • Class variables

Was this helpful?

  1. Guide

Middleware

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

PreviousFrameworkNextServer

Last updated 4 years ago

Was this helpful?

works with many WSGI compatible servers

can used with any compatible web framework

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

This middleware adds variable to environment dictionary.

example with :

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)

WSocketApp

WSocketApp(
   app,
   protocol
)

Class variables

GUID - unique ID to generate websocket accept key

SUPPORTED_VERSIONS - 13, 8 or 7

websocket_class - "wsgi.websocket" in WSGI Environ

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

for examples on other web frameworks visit folder

WSGI
wsgi.websocket
WSGI
bottle
WSGI
examples/frameworks