This repository has been archived by the owner on Aug 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws.py
123 lines (95 loc) · 3.71 KB
/
ws.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import json
from ws4py import configure_logger, format_addresses
from ws4py.manager import WebSocketManager
from ws4py.websocket import WebSocket
from utils import parse_yturl
logger = configure_logger()
client_manager = WebSocketManager()
audio_manager = WebSocketManager()
class ControlWebSocket(WebSocket):
heartbeat_freq = 1
def received_message(self, msg):
logger.debug('[LOG] Rx msg: {}'.format(msg.data))
try:
data = json.loads(msg.data.decode())
client = data.get('client', None)
audio = data.get('audio', None)
action = data['action']
assert(client or audio)
except ValueError:
self.send(json.dumps({'error': 'Invalid JSON'}))
return
except (KeyError, AssertionError):
self.send(json.dumps({'error': 'missing key "audio" or "client"'}))
return
if client:
ret = self.client_handler(client, data)
elif audio:
ret = self.audio_handler(audio, data)
self.send(json.dumps(ret), False)
def client_handler(self, client, data):
action = data['action']
if action == 'join':
logger.debug('[client join] {}'.format(format_addresses(self)))
client_manager.add(self)
elif action == 'play':
logger.debug('[action] play')
audio_manager.broadcast(json.dumps({'action': 'play'}))
elif action == 'pause':
logger.debug('[action] pause')
audio_manager.broadcast(json.dumps({'action': 'pause'}))
elif action == 'stop':
logger.debug('[action] stop')
audio_manager.broadcast(json.dumps({'action': 'stop'}))
elif action == 'add':
link = data.get('link')
if not link:
return {'error': 'missing link for add'}
try:
link_param = parse_yturl(link)
except (ValueError, KeyError):
return {'error': 'Invalid youtube link to add'}
logger.debug('[action] add')
audio_manager.broadcast(json.dumps({
'action': 'add',
'user': client,
'link': link_param,
}))
return {'ok': True, 'msg': 'link {} added'.format(link)}
elif action == 'get_info':
name = data.get('name')
if not name:
return {'error': 'missing name for get_info'}
logger.debug('[action] get_info: {}'.format(name))
audio_manager.broadcast(json.dumps({
'action': 'get_info',
'name': name,
}))
elif action == 'set_vol':
value = data.get('value', None)
if not value:
return {'error': 'missing value for set_vol'}
logger.debug('[action] set_vol: {}'.format(value))
audio_manager.broadcast(json.dumps({
'action': 'set_vol',
'value': value,
}))
return {'ok': True}
def audio_handler(self, audio, data):
action = data['action']
if action == 'join':
logger.debug('[audio join] {}'.format(format_addresses(self)))
audio_manager.add(self)
elif action == 'status_change':
logger.debug('[audio status_change]')
client_manager.broadcast(json.dumps({
'action': 'status_change',
'data': data['data'],
}))
elif action == 'get_info':
logger.debug('[audio get_info]')
client_manager.broadcast(json.dumps({
'action': 'get_info',
'data': data['data']
}))
return {'ok': True}