-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·89 lines (66 loc) · 2.33 KB
/
app.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import logging
from tornado.ioloop import IOLoop
from tornado.options import define, options
from tornado.web import Application, RequestHandler
from tornado.websocket import WebSocketHandler
rel = lambda *x: os.path.abspath(os.path.join(os.path.dirname(__file__), *x))
class MainHandler(RequestHandler):
def get(self):
self.render('index.html')
class WebRTCHandler(RequestHandler):
def get(self):
self.render('index3.html')
class EchoWebSocket(WebSocketHandler):
clients = []
def open(self):
logging.info('WebSocket opened from %s', self.request.remote_ip)
EchoWebSocket.clients.append(self)
def on_message(self, message):
logging.info('got message from %s: %s', self.request.remote_ip, message)
for client in EchoWebSocket.clients:
if client is self:
continue
client.write_message(message)
def on_close(self):
logging.info('WebSocket closed')
EchoWebSocket.clients.remove(self)
class SignallingHandler(WebSocketHandler):
clients = []
def open(self):
logging.info('WebSocket opened from %s', self.request.remote_ip)
SignallingHandler.clients.append(self)
def on_message(self, message):
logging.info('got message from %s: %s', self.request.remote_ip, message)
for client in SignallingHandler.clients:
if client is not self:
client.write_message(message)
def on_close(self):
logging.info('WebSocket closed')
SignallingHandler.clients.remove(self)
def main():
define('listen', metavar='IP', default='127.0.0.1', help='listen on IP address (default 127.0.0.1)')
define('port', metavar='PORT', default=8888, type=int, help='listen on PORT (default 8888)')
define('debug', metavar='True|False', default=False, type=bool,
help='enable Tornado debug mode: templates will not be cached '
'and the app will watch for changes to its source files '
'and reload itself when anything changes')
options.parse_command_line()
settings = dict(
template_path=rel('templates'),
static_path=rel('static'),
debug=options.debug
)
application = Application([
(r'/', MainHandler),
(r'/ws', EchoWebSocket),
(r'/websocket', SignallingHandler),
(r'/webrtc', WebRTCHandler)
], **settings)
#application.listen(address=options.listen, port=options.port)
application.listen(7080)
IOLoop.instance().start()
if __name__ == '__main__':
main()