Przeglądaj źródła

Added JSONMessage and some comments + code cleanup

Taddeus Kroes 12 lat temu
rodzic
commit
bd6d116d75
4 zmienionych plików z 47 dodań i 9 usunięć
  1. 9 0
      __init__.py
  2. 11 1
      message.py
  3. 23 4
      server.py
  4. 4 4
      websocket.py

+ 9 - 0
__init__.py

@@ -0,0 +1,9 @@
+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']

+ 11 - 1
message.py

@@ -1,7 +1,9 @@
+import json
+
 from frame import Frame, OPCODE_TEXT, OPCODE_BINARY
 
 
-__all__ = ['Message', 'TextMessage', 'BinaryMessage']
+__all__ = ['Message', 'TextMessage', 'BinaryMessage', 'JSONMessage']
 
 
 class Message(object):
@@ -30,6 +32,14 @@ class BinaryMessage(Message):
         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_TEXT: TextMessage,
     OPCODE_BINARY: BinaryMessage,

+ 23 - 4
server.py

@@ -10,6 +10,25 @@ from exceptions import HandshakeError
 
 
 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=[]):
         logging.basicConfig(level=loglevel,
                 format='%(asctime)s: %(levelname)s: %(message)s',
@@ -55,16 +74,16 @@ class Server(object):
         self.onclose(client, code, reason)
 
     def onopen(self, client):
-        logging.debug('Opened socket to %s' % client)
+        logging.debug('Opened socket to %s', client)
 
     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):
-        logging.debug('Sent ping "%s" to %s' % (payload, client))
+        logging.debug('Sent ping "%s" to %s', payload, client)
 
     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):
         msg = 'Closed socket to %s' % client

+ 4 - 4
websocket.py

@@ -60,10 +60,10 @@ class websocket(object):
         client handshake). Note that the handshake may raise an HandshakeError
         exception.
         """
-        client, address = self.sock.accept()
-        client = websocket(client)
-        client.server_handshake()
-        return client, address
+        sock, address = self.sock.accept()
+        wsock = websocket(sock)
+        wsock.server_handshake()
+        return wsock, address
 
     def connect(self, address):
         """