-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleServer.py
More file actions
71 lines (53 loc) · 1.86 KB
/
ExampleServer.py
File metadata and controls
71 lines (53 loc) · 1.86 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
59
60
61
62
63
64
65
66
67
68
69
70
71
import socketio, uvicorn, time
# Create an ASYNC Socket.io server
sio = socketio.AsyncServer(async_mode='asgi', cors_allowed_origins='*')
# Wrap it in an ASGI application
app = socketio.ASGIApp(sio)
# Keep track of the spotify sid
spotify_sid = None
@sio.on('connect')
async def connect(sid, data):
print(f"A new client has connected: {sid}")
@sio.on('disconnect')
async def disconnect(sid):
global spotify_sid
if(sid == spotify_sid):
spotify_sid = None
print("The spotify client has disconnected.")
else:
print(f"A client has disconnected: {sid}")
@sio.on('p2s-connect')
async def p2s_connect(sid, data):
global spotify_sid
# There should NOT be more than one spotify client connecting
if(spotify_sid != None): return
spotify_sid = sid
print(f"Spotify has connected!")
print(f"Data sent from connection:\n{data}")
@sio.on('songchange')
async def handle_song_change(sid, data):
print(f"[Song Change]:", data)
@sio.on('playpause')
async def handle_play_pause(sid, data):
print(f"[Play Pause]:", data)
@sio.on('progress')
async def handle_progress(sid, data):
print(f"[Progress]:", data)
# Play next song
async def next():
await sio.emit('s2p-playback', {'type': 'next'}, room=sid_lock)
# Play previous / Start of track
async def prev():
await sio.emit('s2p-playback', {'type': 'prev'}, room=sid_lock)
# Play/Pause
async def toggle():
await sio.emit('s2p-playback', {'type': 'toggle'}, room=sid_lock)
# Play a spotify URI
async def play():
await sio.emit('s2p-playback', {'type': 'play', 'uri': 'spotify:track:709ZIqPHyFOpx2QdjmeWAM' })
# Seek current track to 5 seconds in
async def seek():
await sio.emit('s2p-playback', {'type': 'seek', 'milli': '5000'})
if __name__ == '__main__':
print("Socket server starting on http://localhost:8000")
uvicorn.run(app, host="0.0.0.0", port=8000)