author | cavaliet |
Thu, 16 Oct 2014 11:04:45 +0200 | |
changeset 28 | 6025b8470d18 |
parent 13 | 435d5c15275a |
child 43 | e27c3c1c57f1 |
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 |
|
13
435d5c15275a
clean client npm and gulp config, change flask root url, reference static url in template, change config management to follow flask convention
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
15 |
import config |
0 | 16 |
from oscserver import OSCServerProtocol |
17 |
from websockets import BroadcastServerFactory, AnotationServerFactory |
|
18 |
from webapp import app |
|
19 |
||
20 |
||
21 |
WEB_PORT = 8080 |
|
22 |
WS_PORT = 8090 |
|
23 |
OSC_PORT = 9090 |
|
24 |
||
13
435d5c15275a
clean client npm and gulp config, change flask root url, reference static url in template, change config management to follow flask convention
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
25 |
def make_service(conn, debug=False): |
0 | 26 |
|
27 |
s = service.MultiService() |
|
28 |
# Create and start a thread pool, |
|
29 |
wsgiThreadPool = ThreadPool() |
|
30 |
wsgiThreadPool.start() |
|
31 |
||
32 |
# ensuring that it will be stopped when the reactor shuts down |
|
33 |
reactor.addSystemEventTrigger('after', 'shutdown', wsgiThreadPool.stop) |
|
34 |
||
35 |
||
13
435d5c15275a
clean client npm and gulp config, change flask root url, reference static url in template, change config management to follow flask convention
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
36 |
wsFactory = BroadcastServerFactory("ws://localhost:%d/broadcast" % getattr(config,'WS_PORT',WS_PORT), |
0 | 37 |
debug = debug, |
38 |
debugCodePaths = debug) |
|
39 |
wsFactory.setProtocolOptions(allowHixie76 = True) |
|
40 |
wsResource = WebSocketResource(wsFactory) |
|
41 |
||
42 |
||
13
435d5c15275a
clean client npm and gulp config, change flask root url, reference static url in template, change config management to follow flask convention
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
43 |
wsAnnotFactory = AnotationServerFactory("ws://localhost:%d/annot" % getattr(config,'WS_PORT',WS_PORT), |
0 | 44 |
debug = debug, |
45 |
debugCodePaths = debug, |
|
46 |
ws_factory = wsFactory, |
|
47 |
conn = conn) |
|
48 |
wsAnnotFactory.setProtocolOptions(allowHixie76 = True) |
|
49 |
wsAnnotResource = WebSocketResource(wsAnnotFactory) |
|
50 |
||
51 |
||
52 |
rootWs = Data("", "text/plain") |
|
53 |
||
54 |
rootWs.putChild("broadcast", wsResource) |
|
55 |
rootWs.putChild("annot", wsAnnotResource) |
|
56 |
||
57 |
||
2
fc1ab0074e29
export config in config file + add static file
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
58 |
webResource = resource.Resource() |
13
435d5c15275a
clean client npm and gulp config, change flask root url, reference static url in template, change config management to follow flask convention
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
59 |
webResource.putChild(getattr(config,'STATIC_URL').lstrip('/'), File(getattr(config,'STATIC_ROOT'))) |
0 | 60 |
# Create the WSGI resource |
61 |
wsgiAppAsResource = WSGIResource(reactor, wsgiThreadPool, app) |
|
13
435d5c15275a
clean client npm and gulp config, change flask root url, reference static url in template, change config management to follow flask convention
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
62 |
webResource.putChild('p', wsgiAppAsResource) |
2
fc1ab0074e29
export config in config file + add static file
ymh <ymh.work@gmail.com>
parents:
0
diff
changeset
|
63 |
|
0 | 64 |
|
65 |
# Hooks for twistd |
|
66 |
||
13
435d5c15275a
clean client npm and gulp config, change flask root url, reference static url in template, change config management to follow flask convention
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
67 |
webserver = strports.service('tcp:%d' % getattr(config,'WEB_PORT',WEB_PORT), server.Site(webResource)) |
0 | 68 |
webserver.setServiceParent(s) |
69 |
||
13
435d5c15275a
clean client npm and gulp config, change flask root url, reference static url in template, change config management to follow flask convention
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
70 |
wsserver = strports.service('tcp:%d' % getattr(config,'WS_PORT',WS_PORT), server.Site(rootWs)) |
0 | 71 |
wsserver.setServiceParent(s) |
72 |
||
13
435d5c15275a
clean client npm and gulp config, change flask root url, reference static url in template, change config management to follow flask convention
ymh <ymh.work@gmail.com>
parents:
2
diff
changeset
|
73 |
oscservice = internet.UDPServer(getattr(config,'OSC_PORT',OSC_PORT), OSCServerProtocol(wsFactory, conn)) |
0 | 74 |
oscservice.setServiceParent(s) |
75 |
||
76 |
return s |