-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket_handler.py
More file actions
58 lines (44 loc) · 1.99 KB
/
websocket_handler.py
File metadata and controls
58 lines (44 loc) · 1.99 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
import json
from constants import SETTINGS
from urllib.parse import urlparse
from uuid import uuid4
from tornado import ioloop, gen
from tornado.websocket import WebSocketHandler
class WSHandler(WebSocketHandler):
LOGGER = logging.getLogger(__qualname__)
WHITELISTED_DOMAINS = SETTINGS['WHITELISTED_DOMAINS'].split(",")
def check_origin(self, origin):
parsed_origin = urlparse(origin)
if parsed_origin.hostname is 'localhost':
return True
domain = ".".join(parsed_origin.netloc.split(".")[1:])
return domain in WSHandler.WHITELISTED_DOMAINS
def open(self):
self._sess_id = uuid4().hex
WSHandler.LOGGER.debug(
'Websocket with id {0} is now connected.'.format(self._sess_id))
self.application.listener.websockets[self._sess_id] = self
@gen.coroutine
def on_message(self, body):
WSHandler.LOGGER.debug(
'Websocket {0} has received a message: {1}'.format(self._sess_id, body))
message = json.loads(body)
yield self.wait_still_stream_finishes(message['track'])
@gen.coroutine
def wait_still_stream_finishes(self, message):
# Disconnect the stream momentarily.
self.application.listener.stop_streaming()
while self.application.listener.is_stream_running():
yield gen.sleep(1)
ioloop.IOLoop.current().run_in_executor(None,
self.application.listener.start_streaming,
self._sess_id, message)
def on_close(self):
WSHandler.LOGGER.debug(
'Websocket with id {0} has disconnected.'.format(self._sess_id))
self.application.listener.remove_websocket_and_search(self._sess_id)
if(len(self.application.listener.websockets) is 0):
WSHandler.LOGGER.debug(
"No more connections to keep track of. Closing stream.")
self.application.listener.stop_streaming()