Commit bd6d116d authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added JSONMessage and some comments + code cleanup

parent a6952f5b
from websocket import websocket
from server import Server
from frame import Frame, ControlFrame
from Connection import Connection
from message import Message, TextMesage, BinaryMessage, JSONMessage
__all__ = ['websocket', 'Server', 'Frame', 'ControlFrame', 'Connection',
'Message', 'TextMessage', 'BinaryMessage', 'JSONMessage']
import json
from frame import Frame, OPCODE_TEXT, OPCODE_BINARY from frame import Frame, OPCODE_TEXT, OPCODE_BINARY
__all__ = ['Message', 'TextMessage', 'BinaryMessage'] __all__ = ['Message', 'TextMessage', 'BinaryMessage', 'JSONMessage']
class Message(object): class Message(object):
...@@ -30,6 +32,14 @@ class BinaryMessage(Message): ...@@ -30,6 +32,14 @@ class BinaryMessage(Message):
super(BinaryMessage, self).__init__(OPCODE_BINARY, payload) super(BinaryMessage, self).__init__(OPCODE_BINARY, payload)
class JSONMessage(TextMessage):
def __init__(self, dictionary, **kwargs):
self.data = {}
self.data.extend(dictionary)
self.data.extend(kwargs)
super(JSONMessage, self).__init__(json.dumps(self.data))
OPCODE_CLASS_MAP = { OPCODE_CLASS_MAP = {
OPCODE_TEXT: TextMessage, OPCODE_TEXT: TextMessage,
OPCODE_BINARY: BinaryMessage, OPCODE_BINARY: BinaryMessage,
......
...@@ -10,6 +10,25 @@ from exceptions import HandshakeError ...@@ -10,6 +10,25 @@ from exceptions import HandshakeError
class Server(object): class Server(object):
"""
Websocket server object, used to manage multiple client connections.
Example usage:
>>> import websocket
>>> class GameServer(websocket.Server):
>>> def onopen(self, client):
>>> # client connected
>>> def onclose(self, client):
>>> # client disconnected
>>> def onmessage(self, client, message):
>>> # handle message from client
>>> GameServer(port=8000).run()
"""
def __init__(self, port, hostname='', loglevel=logging.INFO, protocols=[]): def __init__(self, port, hostname='', loglevel=logging.INFO, protocols=[]):
logging.basicConfig(level=loglevel, logging.basicConfig(level=loglevel,
format='%(asctime)s: %(levelname)s: %(message)s', format='%(asctime)s: %(levelname)s: %(message)s',
...@@ -55,16 +74,16 @@ class Server(object): ...@@ -55,16 +74,16 @@ class Server(object):
self.onclose(client, code, reason) self.onclose(client, code, reason)
def onopen(self, client): def onopen(self, client):
logging.debug('Opened socket to %s' % client) logging.debug('Opened socket to %s', client)
def onmessage(self, client, message): def onmessage(self, client, message):
logging.debug('Received %s from %s' % (message, client)) logging.debug('Received %s from %s', message, client)
def onping(self, client, payload): def onping(self, client, payload):
logging.debug('Sent ping "%s" to %s' % (payload, client)) logging.debug('Sent ping "%s" to %s', payload, client)
def onpong(self, client, payload): def onpong(self, client, payload):
logging.debug('Received pong "%s" from %s' % (payload, client)) logging.debug('Received pong "%s" from %s', payload, client)
def onclose(self, client, code, reason): def onclose(self, client, code, reason):
msg = 'Closed socket to %s' % client msg = 'Closed socket to %s' % client
......
...@@ -60,10 +60,10 @@ class websocket(object): ...@@ -60,10 +60,10 @@ class websocket(object):
client handshake). Note that the handshake may raise an HandshakeError client handshake). Note that the handshake may raise an HandshakeError
exception. exception.
""" """
client, address = self.sock.accept() sock, address = self.sock.accept()
client = websocket(client) wsock = websocket(sock)
client.server_handshake() wsock.server_handshake()
return client, address return wsock, address
def connect(self, address): def connect(self, address):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment