-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
71 lines (52 loc) · 1.98 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
from socketio import socketio_manage
from socketio.server import SocketIOServer
from socketio.namespace import BaseNamespace
from mako.template import Template
from mako.lookup import TemplateLookup
import util
from util import route
from util import STATIC_DIR
class ControlNamespace(BaseNamespace):
_registry = {}
_page_num = 0
def initialize(self):
self._registry[id(self)] = self
self.emit("flip", ControlNamespace._page_num)
def disconnect(self, *args, **kwargs):
del self._registry[id(self)]
super(ControlNamespace, self).disconnect(*args, **kwargs)
def on_flip(self, new_slide):
ControlNamespace._page_num = new_slide
self._broadcast("flip", ControlNamespace._page_num)
def _broadcast(self, event, message):
for s in self._registry.values():
s.emit(event, message)
@util.expose
def presentation(start_response, args):
id = args[0]
#TODO: fetch data from database
template = template_lookup.get_template("presentation.mako")
return template.render(json_path="/data/final_presentation/slides.json")
@util.expose
def controls(start_response, args):
id = args[0]
#TODO: fetch data from database
print "hit here"
template = template_lookup.get_template("controls.mako")
return template.render(json_path="/data/final_presentation/slides.json")
template_lookup = TemplateLookup(directories=[STATIC_DIR + "/templates"])
def application(environ, start_response):
args = util.get_args(environ)
if route(environ, 'socket.io'):
return socketio_manage(environ, { '/control': ControlNamespace })
elif route(environ, 'presentation'):
return presentation(start_response, args)
elif route(environ, 'controls'):
return controls(start_response, args)
else:
return util.serve_file(environ, start_response)
sio_server = SocketIOServer(
('', 8080), application,
policy_server=False)
sio_server.serve_forever()