-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExampleServer.py
More file actions
57 lines (40 loc) · 1.38 KB
/
ExampleServer.py
File metadata and controls
57 lines (40 loc) · 1.38 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
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)
# Realistically, there is only one client connecting to the server
sid_lock = None
@sio.on('connect')
async def connect(sid, data):
global sid_lock
# Drop any new connections
if sid_lock is not None:
await sio.disconnect(sid)
sid_lock = sid
print(f"✅ Spicetify Client Connected: {sid}")
@sio.on('disconnect')
async def disconnect(sid):
global sid_lock
sid_lock = None
@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)
# Examples of sending the client commands
async def next():
await sio.emit('command', {'action': 'next'}, room=sid_lock)
async def prev():
await sio.emit('command', {'action': 'prev'}, room=sid_lock)
async def pause():
await sio.emit('command', {'action': 'pause'}, room=sid_lock)
async def play():
await sio.emit('command', {'action': 'play'}, room=sid_lock)
if __name__ == '__main__':
print("Socket server starting on http://localhost:8000")
uvicorn.run(app, host="0.0.0.0", port=8000)