/
rpcping.py
52 lines (35 loc) · 1.24 KB
/
rpcping.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
from os import path as op
import datetime
from tornado import web
from tornadio2 import SocketConnection, TornadioRouter, SocketServer, event
ROOT = op.normpath(op.dirname(__file__))
class IndexHandler(web.RequestHandler):
"""Regular HTTP handler to serve the chatroom page"""
def get(self):
self.render('index.html')
class SocketIOHandler(web.RequestHandler):
def get(self):
self.render('../socket.io.js')
class PingConnection(SocketConnection):
@event
def ping(self, client, text):
print 'Got %s from client' % text
now = datetime.datetime.now()
self.emit('pong',
client,
[now.hour, now.minute, now.second, now.microsecond / 1000])
# Create tornadio router
PingRouter = TornadioRouter(PingConnection)
# Create socket application
application = web.Application(
PingRouter.apply_routes([(r"/", IndexHandler),
(r"/socket.io.js", SocketIOHandler)]),
flash_policy_port = 843,
flash_policy_file = op.join(ROOT, 'flashpolicy.xml'),
socket_io_port = 8001
)
if __name__ == "__main__":
import logging
logging.getLogger().setLevel(logging.DEBUG)
# Create and start tornadio server
SocketServer(application)