-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.py
More file actions
41 lines (31 loc) · 1.13 KB
/
server.py
File metadata and controls
41 lines (31 loc) · 1.13 KB
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
import tornadio2
from tornadio2.server import SocketServer
import tornado
from tornado.web import Application
from boards import BoardsController
boards_controller = BoardsController()
# MyConnection Class
class MyConnection(tornadio2.SocketConnection):
def on_message(self, message):
boards_controller.on_message(self, message)
def on_close(self):
boards_controller.unregister(self)
class WebSocketsServer:
def __init__(self, uri, port):
# Creating a Tornadio2 server for the connection
MyRouter = tornadio2.TornadioRouter(MyConnection)
application = Application(MyRouter.urls, socket_io_port = port)
self.http_server = SocketServer(application, auto_start = False, xheaders=True)
# Start Tornadio2 server
def start(self):
io_loop = tornado.ioloop.IOLoop.instance()
io_loop.start()
# Stop Tornadio2 server
def stop(self):
self.http_server.stop()
tornado.ioloop.IOLoop.instance().stop()
if __name__ == "__main__":
# Port listening (8888)
import os
server = WebSocketsServer(r'/ws', os.getenv('PORT', 8888))
server.start()