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
conn.send(msg(foo='Hello, World!'))
- 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()`.
Built-in Server
===============
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):
def onopen(self, client):
print 'Client %s connected' % client
import wspy
def onmessage(self, client, message):
print 'Received message "%s"' % message.payload
client.send(wspy.TextMessage(message.payload))
class EchoServer(wspy.Server):
def onopen(self, client):
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):
print 'Client %s disconnected' % client
def onclose(self, client, code, reason):
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
`KeyboardInterrupt` raised when this happens is caught by the server.
The server can be stopped by typing CTRL-C in the command line. The
`KeyboardInterrupt` raised when this happens is caught by the server.
Extensions
......
......@@ -25,7 +25,7 @@ class Server(object):
>>> print 'Received message "%s"' % message.payload
>>> client.send(wspy.TextMessage(message.payload))
>>> def onclose(self, client):
>>> def onclose(self, client, code, reason):
>>> print 'Client %s disconnected' % client
>>> 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