All Projects → pikhovkin → simple-websocket-server

pikhovkin / simple-websocket-server

Licence: MIT license
A simple WebSocket server

Programming Languages

python
139335 projects - #7 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to simple-websocket-server

Websockets
Library for building WebSocket servers and clients in Python
Stars: ✭ 3,724 (+14223.08%)
Mutual labels:  websocket-server, websockets, websocket-library
Facil.io
Your high performance web application C framework
Stars: ✭ 1,393 (+5257.69%)
Mutual labels:  websocket-server, websockets
Sandstone
PHP microframework designed to build a RestApi working together with a websocket server. Build a real time RestApi!
Stars: ✭ 98 (+276.92%)
Mutual labels:  websocket-server, websockets
Ws Tcp Relay
A simple relay between WebSocket clients and TCP servers
Stars: ✭ 186 (+615.38%)
Mutual labels:  websocket-server, websockets
Stl.fusion
Get real-time UI updates in Blazor apps and 10-1000x faster API responses with a novel approach to distributed reactive computing. Fusion brings computed observables and automatic dependency tracking from Knockout.js/MobX/Vue to the next level by enabling a single dependency graph span multiple servers and clients, including Blazor apps running in browser.
Stars: ✭ 858 (+3200%)
Mutual labels:  websocket-server, websockets
Websockets Chat
Laravel WebSockets chat
Stars: ✭ 44 (+69.23%)
Mutual labels:  websocket-server, websockets
Websocket
WSServer is a fast, configurable, and extendable WebSocket Server for UNIX systems written in C (C11).
Stars: ✭ 144 (+453.85%)
Mutual labels:  websocket-server, websockets
Gwsocket
fast, standalone, language-agnostic WebSocket server RFC6455 compliant
Stars: ✭ 550 (+2015.38%)
Mutual labels:  websocket-server, websockets
Websocat
Command-line client for WebSockets, like netcat (or curl) for ws:// with advanced socat-like functions
Stars: ✭ 3,477 (+13273.08%)
Mutual labels:  websocket-server, websockets
Websocketd
Turn any program that uses STDIN/STDOUT into a WebSocket server. Like inetd, but for WebSockets.
Stars: ✭ 15,828 (+60776.92%)
Mutual labels:  websocket-server, websockets
Rockets
REST and websockets C++ library
Stars: ✭ 39 (+50%)
Mutual labels:  websocket-server, websocket-library
Awesome Websockets
A curated list of Websocket libraries and resources.
Stars: ✭ 850 (+3169.23%)
Mutual labels:  websocket-server, websockets
Cowboy
Small, fast, modern HTTP server for Erlang/OTP.
Stars: ✭ 6,533 (+25026.92%)
Mutual labels:  websocket-server, websockets
Arduinowebsockets
arduinoWebSockets
Stars: ✭ 1,265 (+4765.38%)
Mutual labels:  websocket-server, websockets
Ulfius
Web Framework to build REST APIs, Webservices or any HTTP endpoint in C language. Can stream large amount of data, integrate JSON data with Jansson, and create websocket services
Stars: ✭ 666 (+2461.54%)
Mutual labels:  websocket-server, websockets
Php Wss
Web-socket server/client with multi-process and parse templates support on server and send/receive options on client
Stars: ✭ 117 (+350%)
Mutual labels:  websocket-server, websockets
channeled-dashboard
Repository for the talk `Building real time applications with Django and Channels`
Stars: ✭ 20 (-23.08%)
Mutual labels:  websocket-server, websockets
Seasocks
Simple, small, C++ embeddable webserver with WebSockets support
Stars: ✭ 517 (+1888.46%)
Mutual labels:  websocket-server, websockets
Rockgo
A developing game server framework,based on Entity Component System(ECS).
Stars: ✭ 532 (+1946.15%)
Mutual labels:  websocket-server, websockets
Ixwebsocket
websocket and http client and server library, coming with ws, a command line swiss army knife utility
Stars: ✭ 204 (+684.62%)
Mutual labels:  websocket-server, websockets

A simple WebSocket server

CircleCI PyPI PyPI - Python Version PyPI - License

Based on simple-websocket-server.

  • RFC 6455 (All latest browsers)
  • TLS/SSL out of the box
  • Passes Autobahns Websocket Testsuite
  • Support for Python 2 and 3

Installation

pip install simple-websocket-server

Echo Server Example

from simple_websocket_server import WebSocketServer, WebSocket


class SimpleEcho(WebSocket):
    def handle(self):
        # echo message back to client
        self.send_message(self.data)

    def connected(self):
        print(self.address, 'connected')

    def handle_close(self):
        print(self.address, 'closed')


server = WebSocketServer('', 8000, SimpleEcho)
server.serve_forever()

Open tests/websocket.html and connect to the server.

Chat Server Example

from simple_websocket_server import WebSocketServer, WebSocket


class SimpleChat(WebSocket):
    def handle(self):
        for client in clients:
            if client != self:
                client.send_message(self.address[0] + u' - ' + self.data)

    def connected(self):
        print(self.address, 'connected')
        for client in clients:
            client.send_message(self.address[0] + u' - connected')
        clients.append(self)

    def handle_close(self):
        clients.remove(self)
        print(self.address, 'closed')
        for client in clients:
            client.send_message(self.address[0] + u' - disconnected')


clients = []

server = WebSocketServer('', 8000, SimpleChat)
server.serve_forever()

Open multiple tests/websocket.html and connect to the server.

Want to get up and running faster?

There is an example which provides a simple echo and chat server

Echo Server

python tests/example_server.py --example echo

Chat Server (open up multiple tests/websocket.html files)

python tests/example_server.py --example chat

TLS/SSL Example

  1. Generate a certificate with key

     openssl req -new -x509 -days 365 -nodes -out cert.pem -keyout key.pem
    
  2. Run the secure TSL/SSL server (in this case the cert.pem file is in the same directory)

     python tests/example_server.py --example chat --ssl 1
    
  3. Offer the certificate to the browser by serving tests/websocket.html through https. The HTTPS server will look for cert.pem in the local directory. Ensure the tests/websocket.html is also in the same directory to where the server is run.

     python tests/simple_https_server.py
    
  4. Open a web browser to: https://localhost:443/tests/websocket.html

  5. Change ws://localhost:8000/ to wss://localhost:8000 and click connect.

Note: if you are having problems connecting, ensure that the certificate is added in your browser against the exception https://localhost:8000 or whatever host:port pair you want to connect to.

For the Programmers

connected: called when handshake is complete

  • self.address: TCP address port tuple of the endpoint

handle_close: called when the endpoint is closed or there is an error

  • self.address: TCP address port tuple of the endpoint

handle: gets called when there is an incoming message from the client endpoint

  • self.address: TCP address port tuple of the endpoint
  • self.opcode: the WebSocket frame type (STREAM, TEXT, BINARY)
  • self.data: bytearray (BINARY frame) or unicode string payload (TEXT frame)
  • self.request: HTTP details from the WebSocket handshake (refer to BaseHTTPRequestHandler)

send_message: send some text or binary data to the client endpoint

  • sending data as a unicode object will send a TEXT frame
  • sending data as a bytearray object will send a BINARY frame

close: send close frame to endpoint

Licensing

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].