forked from josmas/beetroot_board
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
70 lines (52 loc) · 1.79 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
from os import path as op
import tornado.web
import tornadio
import tornadio.router
import tornadio.server
import thread
import logging
global connections
ROOT = op.normpath(op.dirname(__file__))
class IndexHandler(tornado.web.RequestHandler):
"""Regular HTTP handler to serve the chatroom page"""
def get(self):
self.render("index.html")
class ChatConnection(tornadio.SocketConnection):
# Class level variable
participants = set()
def __init__(self, protocol, io_loop, heartbeat_interval):
tornadio.SocketConnection.__init__(self, protocol, io_loop, heartbeat_interval)
def on_open(self, *args, **kwargs):
self.participants.add(self)
self.send("CONNECTED")
def on_message(self, message):
for p in self.participants:
p.send(message)
def on_close(self):
self.participants.remove(self)
self.send("DISCONNECTED")
#use the routes classmethod to build the correct resource
ChatRouter = tornadio.get_router(ChatConnection)
#configure the Tornado application
application = tornado.web.Application(
[(r"/", IndexHandler), ChatRouter.route()],
enabled_protocols = ['websocket',
'flashsocket',
'xhr-multipart',
'xhr-polling'],
flash_policy_port = 843,
flash_policy_file = op.join(ROOT, 'flashpolicy.xml'),
socket_io_port = 8001
)
if __name__ == "__main__":
# initialize login
logging.getLogger().setLevel(logging.DEBUG)
# initliaze connections instance
global connections
connections = set()
try:
thread.start_new_thread(readLog, ("LogReader", 1,) )
except:
print "Error: unable to start thread"
# start server
tornadio.server.SocketServer(application)