author | ymh <ymh.work@gmail.com> |
Thu, 09 Oct 2014 08:44:36 +0200 | |
changeset 2 | fc1ab0074e29 |
parent 0 | e1d4d7a8255a |
child 13 | 435d5c15275a |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
from autobahn.twisted.resource import WebSocketResource, \ |
|
3 |
WSGIRootResource, \ |
|
4 |
HTTPChannelHixie76Aware |
|
5 |
||
6 |
||
7 |
from twisted.application import service, internet, strports |
|
8 |
from twisted.internet import reactor |
|
9 |
from twisted.python.threadpool import ThreadPool |
|
10 |
from twisted.web import server |
|
2
fc1ab0074e29
export config in config file + add static file
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
11 |
from twisted.web import resource |
0 | 12 |
from twisted.web.wsgi import WSGIResource |
2
fc1ab0074e29
export config in config file + add static file
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
13 |
from twisted.web.static import Data, File |
0 | 14 |
|
15 |
from oscserver import OSCServerProtocol |
|
16 |
from websockets import BroadcastServerFactory, AnotationServerFactory |
|
17 |
from webapp import app |
|
18 |
||
19 |
||
20 |
WEB_PORT = 8080 |
|
21 |
WS_PORT = 8090 |
|
22 |
OSC_PORT = 9090 |
|
23 |
||
24 |
def make_service(config, conn, debug=False): |
|
25 |
||
26 |
s = service.MultiService() |
|
27 |
# Create and start a thread pool, |
|
28 |
wsgiThreadPool = ThreadPool() |
|
29 |
wsgiThreadPool.start() |
|
30 |
||
31 |
# ensuring that it will be stopped when the reactor shuts down |
|
32 |
reactor.addSystemEventTrigger('after', 'shutdown', wsgiThreadPool.stop) |
|
33 |
||
34 |
||
35 |
wsFactory = BroadcastServerFactory("ws://localhost:%d/broadcast" % config.get('ws_port',WS_PORT), |
|
36 |
debug = debug, |
|
37 |
debugCodePaths = debug) |
|
38 |
wsFactory.setProtocolOptions(allowHixie76 = True) |
|
39 |
wsResource = WebSocketResource(wsFactory) |
|
40 |
||
41 |
||
42 |
wsAnnotFactory = AnotationServerFactory("ws://localhost:%d/annot" % config.get('ws_port',WS_PORT), |
|
43 |
debug = debug, |
|
44 |
debugCodePaths = debug, |
|
45 |
ws_factory = wsFactory, |
|
46 |
conn = conn) |
|
47 |
wsAnnotFactory.setProtocolOptions(allowHixie76 = True) |
|
48 |
wsAnnotResource = WebSocketResource(wsAnnotFactory) |
|
49 |
||
50 |
||
51 |
rootWs = Data("", "text/plain") |
|
52 |
||
53 |
rootWs.putChild("broadcast", wsResource) |
|
54 |
rootWs.putChild("annot", wsAnnotResource) |
|
55 |
||
56 |
||
2
fc1ab0074e29
export config in config file + add static file
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
57 |
webResource = resource.Resource() |
fc1ab0074e29
export config in config file + add static file
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
58 |
webResource.putChild(config['static_url'], File(config['static_root'])) |
0 | 59 |
# Create the WSGI resource |
60 |
wsgiAppAsResource = WSGIResource(reactor, wsgiThreadPool, app) |
|
2
fc1ab0074e29
export config in config file + add static file
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
61 |
webResource.putChild('', wsgiAppAsResource) |
fc1ab0074e29
export config in config file + add static file
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
62 |
|
0 | 63 |
|
64 |
# Hooks for twistd |
|
65 |
||
2
fc1ab0074e29
export config in config file + add static file
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
66 |
webserver = strports.service('tcp:%d' % config.get('web_port',WEB_PORT), server.Site(webResource)) |
0 | 67 |
webserver.setServiceParent(s) |
68 |
||
69 |
wsserver = strports.service('tcp:%d' % config.get('ws_port',WS_PORT), server.Site(rootWs)) |
|
70 |
wsserver.setServiceParent(s) |
|
71 |
||
72 |
oscservice = internet.UDPServer(config.get('osc_port',OSC_PORT), OSCServerProtocol(wsFactory, conn)) |
|
73 |
oscservice.setServiceParent(s) |
|
74 |
||
75 |
return s |