-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
32 lines (26 loc) · 914 Bytes
/
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
import tornado.ioloop
import tornado.web
import tornado.websocket
import tornado.template
class MainHandler(tornado.web.RequestHandler):
def get(self):
loader = tornado.template.Loader(".")
self.write(loader.load("index.html").generate())
class WSHandler(tornado.websocket.WebSocketHandler):
def open(self):
print 'Connection opened.'
self.write_message("Welcome to my websocket server.")
def on_message(self, message):
if message == "help":
self.write_message("Need help, huh?")
print 'Message received: \'%s\'' % message
def on_close(self):
print 'Connection closed.'
application = tornado.web.Application([
(r'/ws', WSHandler),
(r"/", MainHandler),
(r"/(.*)", tornado.web.StaticFileHandler, {"path": "./resources"}),
])
if __name__ == "__main__":
application.listen(9090)
tornado.ioloop.IOLoop.instance().start()