Giter Club home page Giter Club logo

pywebsocket's Introduction

pywebsocket

Simple websocket server library written in Python.

Example Server Code:
main.py

from pywebsocket.server import WebsocketServer, WebsocketClient

def on_client_connect(server : WebsocketServer, 
                      client : WebsocketClient) -> None:
    # Add this client's socket id to a channel's user list.
    server.default_channel["users"].append(client.get_id())
    client.data["current_channel"] = server.default_channel

    print(server.channel_list)

def on_client_disconnect(server : WebsocketServer, 
                          client : WebsocketClient) -> None:
    # Remove the client from the channel it is currently in.
    client.data["current_channel"]["users"].remove(client.get_id())

    print(server.channel_list)

def on_client_data(server : WebsocketServer, 
                   client : WebsocketClient,
                   data) -> None:
    # Echo client's message.
    print("Received from client:", data)
    server.send_string(client.get_id(), data)

server = WebsocketServer("192.168.1.2", 3630,
                         client_buffer_size       = 1024,
                         pass_data_as_string      = True,
                         daemon_handshake_handler = False, # if set to True, main process 
                                                           # will exit immediately. Be sure to
                                                           # create an endless loop after
                                                           # server.start() has been called.
                         debug                    = True)

# You can set your own variables to server like below:
server.channel_list = {
    "general" : {
        "users" : []
    },
    "news" : {
        "users" : []
    }
}
server.default_channel = server.channel_list["general"]

server.set_special_handler("client_connect",    on_client_connect)
server.set_special_handler("client_disconnect", on_client_disconnect)
server.set_special_handler("client_data",       on_client_data)

server.start()

Installation

Install via pip:

pip install pywebsocket

Or you can install manually by cloning this repo and running this command:

python3 setup.py install

Documentation

Please refer to here for documentation.


Notes:

  • Doesn't support HTTPS connection.
  • Server does support receiving fragmented messages but it doesn't support sending fragmented messages.

pywebsocket's People

Contributors

egebilecen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.