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

Moved server stuff into new section in README

parent 364f446c
...@@ -111,34 +111,37 @@ Basic usage ...@@ -111,34 +111,37 @@ Basic usage
conn.send(msg(foo='Hello, World!')) conn.send(msg(foo='Hello, World!'))
- The built-in `Server` implementation is very basic. It starts a new thread Built-in Server
with a `Connection.receive_forever()` loop for each client that connects. It ===============
also 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. This argument is a
modified `Connection` instance, so you can invoke `send()` and `recv()`.
For example, the `EchoConnection` example above can be rewritten to: The built-in `Server` implementation is very basic. It starts a new thread with
a `Connection.receive_forever()` loop for each client that connects. It also
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. This argument is a modified
`Connection` instance, so you can invoke `send()` and `recv()`.
import wspy For example, the `EchoConnection` example above can be rewritten to:
class EchoServer(wspy.Server): import wspy
def onopen(self, client):
print 'Client %s connected' % client
def onmessage(self, client, message): class EchoServer(wspy.Server):
print 'Received message "%s"' % message.payload def onopen(self, client):
client.send(wspy.TextMessage(message.payload)) print 'Client %s connected' % client
def onmessage(self, client, message):
print 'Received message "%s"' % message.payload
client.send(wspy.TextMessage(message.payload))
def onclose(self, client, code, reason): def onclose(self, client, code, reason):
print 'Client %s disconnected' % client print 'Client %s disconnected' % client
EchoServer(('', 8000)).run() EchoServer(('', 8000)).run()
The server can be stopped by typing CTRL-C in the command line. The 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.
Extensions Extensions
......
...@@ -25,7 +25,7 @@ class Server(object): ...@@ -25,7 +25,7 @@ class Server(object):
>>> print 'Received message "%s"' % message.payload >>> print 'Received message "%s"' % message.payload
>>> client.send(wspy.TextMessage(message.payload)) >>> client.send(wspy.TextMessage(message.payload))
>>> def onclose(self, client): >>> def onclose(self, client, code, reason):
>>> print 'Client %s disconnected' % client >>> print 'Client %s disconnected' % client
>>> EchoServer(('', 8000)).run() >>> EchoServer(('', 8000)).run()
......
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