-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSocketSetup.js
More file actions
87 lines (67 loc) · 2.88 KB
/
SocketSetup.js
File metadata and controls
87 lines (67 loc) · 2.88 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var SocketMessageType = require('juke-protocols');
var ToClientMessages = SocketMessageType.ToClientMessages;
var ToServerMessages = SocketMessageType.ToServerMessages;
var api = require("./api/main");
module.exports = function (io) {
/**
* Keep up with the last set greeting message for newly connecting users
*/
var lastGreetingMessage = "";
/**
* Keep up with the last set now playing song for newly connecting users
*/
var lastNowPlaying = null;
// Keep users updated with the greeting message
api.getGreetingMessage$().subscribe(function (message) {
lastGreetingMessage = message;
io.emit(ToClientMessages.GreetingMessage, message);
});
// Keep users updated with what's now playing
api.getNowPlaying$().subscribe(function(nowPlaying) {
lastNowPlaying = nowPlaying;
io.emit(ToClientMessages.NowPlaying, nowPlaying);
});
// Keep the clients up to date on whether or not the song is playing or paused
api.getPlayer().status$.subscribe(function(status){
io.emit(ToClientMessages.PlayerStatus, status);
// Update the queue whenever a song finishes
if (status === 'finish') {
io.emit(ToClientMessages.EntireQ, api.getSongQueue());
}
});
/**
* Whenever the queue changes send the whole thing through.
*
* TODO: This can be optimized to only send what actually gets changed.
* Said solution would introduce more complexity in syncing up and
* making sure everyone sees the same thing so this sort of thing
* should probably happen wayyy later down the road.
*/
api.getSongQueue().on('change', function () {
io.emit(ToClientMessages.EntireQ, api.getSongQueue());
});
io.on('connection', function (socket) {
console.log("User Connected!");
// Update newly connected users to what's going on
socket.emit(ToClientMessages.EntireQ, api.getSongQueue());
socket.emit(ToClientMessages.GreetingMessage, lastGreetingMessage);
socket.emit(ToClientMessages.NowPlaying, lastNowPlaying);
// Whenever a user adds something to the queue
socket.on(ToServerMessages.AddToQ, function (songToAdd) {
if (!api.getSongQueue().has(songToAdd) && songToAdd !== '{}') {
api.addSongToQueue(songToAdd);
}
});
// Whenever a user votes on a song
socket.on(ToServerMessages.SetVote, function (vote) {
api.setVote(vote);
io.emit(ToClientMessages.EntireQ, api.getSongQueue());
});
socket.on(ToServerMessages.SetUsername, function (name) {
api.setUsersName(name);
})
socket.on(ToServerMessages.SetAdminPassword, function (pw) {
io.emit(ToClientMessages.SetAdminPrivledgeLevel, api.setUsersPassword(pw.ui, pw.password));
})
});
};