score.websockets¶
A module providing a score.serve.Worker
, which launches a websocket
server using the websockets library.
Quickstart¶
The module defaults are suitable for development, so you just need to write
your Worker
class and load it into
score.serve
:
[score]
modules =
score.asyncio
score.ctx
score.websockets
myapplication
[serve]
modules = myapplication
import asyncio
from score.init import ConfiguredModule
from score.websockets import Worker
class Myapplication(ConfiguredModule):
def __init__(self, websockets, asyncio):
super().__init__('myapplication')
self.websockets = websockets
self.asyncio = asyncio
def score_serve_workers(self):
return Worker(self.websockets,
self.asyncio,
self.echo_service)
@asyncio.coroutine
def echo_service(self, ctx):
for message in (yield from ctx.websocket):
yield from ctx.websocket.send(message)
After starting your application, you can access your echo server as “ws://localhost:8081”.
API¶
-
score.websockets.
init
(confdict, ctx)[source]¶ Initializes this module acoording to our module initialization guidelines with the following configuration keys:
- host 0.0.0.0
- The hostname to listen for connnections on.
- port 8081
- The port to listen for connnections on.
- stop_timeout None
Defines how long the module will wait for connections to close when pausing the worker. The value will be interpreted through a call to
score.init.parse_time_interval()
.The default value None indicates that the module will wait indefinitely. If you want to the server to terminate immediately, without waiting for open connections at all, you must pass “0”.
- reuse_port False
- Whether the
reuse_port
keyword argument should be passed to the underlying event loop’screate_server()
method.
-
class
score.websockets.
ConfiguredWebsocketsModule
[source]¶ This module’s
configuration class
.