Skip to content

pskishere/Webss

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Webss

Wercker PyPI - Python Version License

A websocket protocol-based server-side module.

Reference

The WebSocket Protocol

Websocket(zh.wikipedia.org)

Usage

Implementation based on weboskcet protocol.

Through the inheritance BaseWSRequestHandler class and overrides the method method. Use the do_Send method to send data. Use the do_Receive method to receive data

python code
import sys
from Webss.webss import BaseWSRequestHandler
from socketserver import ThreadingTCPServer

class SimpleWSRequestHandler(BaseWSRequestHandler):
    def method(self):
        print(self.do_Receive())
        self.do_Send('I receive the message')

def test(HandlerClass=SimpleWSRequestHandler, protocol="HTTP/1.1", port=8000, bind="127.0.0.1"):
    server_address = (bind, port)

    HandlerClass.protocol_version = protocol
    with ThreadingTCPServer(server_address, HandlerClass) as httpd:
        sa = httpd.socket.getsockname()
        serve_message = "Serving HTTP on {host} port {port} (http://{host}:{port}/) ..."
        print(serve_message.format(host=sa[0], port=sa[1]))
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("\nKeyboard interrupt received, exiting.")
            sys.exit(0)

test()
javascript code
var ws = new WebSocket('ws://127.0.0.1:8000');

ws.onopen = function(evt) {
  console.log('Connection open ...');
  ws.send('Hello WebSockets!');
};

ws.onmessage = function(evt) {
  console.log('Received Message: ' + evt.data);
  ws.close();
};

ws.onclose = function(evt) {
  console.log('Connection closed.');
};

Results screenshot

avatar

avatar

About

A websocket protocol-based server-side script.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published