Commit 6f65ec01 authored by Taddeüs Kroes's avatar Taddeüs Kroes

More README additions

parent d4c3c564
......@@ -166,7 +166,7 @@ handles client crashes properly. By default, a `Server` instance only logs
every event using Python's `logging` module. To create a custom server, The
`Server` class should be extended and its event handlers overwritten. The event
handlers are named identically to the `Connection` event handlers, but they
also receive an additional `client` argument. The client argumetn is a modified
also receive an additional `client` argument. The client argument is a modified
`Connection` instance, so you can invoke `send()` and `recv()`.
For example, the `EchoConnection` example above can be rewritten to:
......@@ -187,7 +187,16 @@ For example, the `EchoConnection` example above can be rewritten to:
EchoServer(('', 8000)).run()
The server can be stopped by typing CTRL-C in the command line. The
`KeyboardInterrupt` raised when this happens is caught by the server.
`KeyboardInterrupt` raised when this happens is caught by the server, making it
exit gracefully.
The full list of overwritable methods is: `onopen`, `onmessage`, `onclose`,
`onerror`, `onping`, `onpong`.
The server uses Python's built-in
[logging](https://docs.python.org/2/library/logging.html) module for logging.
Try passing the argument `loglevel=logging.DEBUG` to the server constructor if
you are having trouble debugging.
Asynchronous (recommended)
--------------------------
......@@ -199,6 +208,12 @@ sent later when the socket is ready. The client argument is again a modified
`Connection` instance, with a non-blocking `send()` method (`recv` is still
blocking, use the server's `onmessage` callback instead).
The asynchronous server has one additional method which you can implement:
`AsyncServer.onsent(self, client, message)`, which is called after a message
has completely been written to the socket. You will probably not need this
unless you are doing something advanced or have to clear a buffer in a
high-performance application.
Extensions
==========
......
......@@ -38,7 +38,7 @@ class AsyncConnection(Connection):
for frame in frames[:-1]:
self.sock.queue_send(frame)
self.sock.queue_send(frames[-1], lambda: self.onsend(message))
self.sock.queue_send(frames[-1], lambda: self.onsent(message))
def send_frame(self, frame, callback):
self.sock.queue_send(frame, callback)
......@@ -79,7 +79,7 @@ class AsyncConnection(Connection):
self.ping_payload = payload
self.ping_sent = True
def onsend(self, message):
def onsent(self, message):
"""
Called after a message has been written.
"""
......@@ -165,7 +165,7 @@ class AsyncServer(Server):
self.epoll.modify(conn.sock.fileno(), mask)
def onsend(self, client, message):
def onsent(self, client, message):
return NotImplemented
......@@ -179,9 +179,9 @@ class AsyncClient(Client, AsyncConnection):
AsyncConnection.send(self, message, fragment_size, mask)
self.server.update_mask(self)
def onsend(self, message):
def onsent(self, message):
logging.debug('Finished sending %s to %s', message, self)
self.server.onsend(self, message)
self.server.onsent(self, message)
if __name__ == '__main__':
......
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