-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocketIoHandler.js
More file actions
149 lines (132 loc) · 4.1 KB
/
socketIoHandler.js
File metadata and controls
149 lines (132 loc) · 4.1 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { Server } from 'socket.io';
// import { parse } from 'cookie';
// import socketioAuth from 'socketio-auth';
export default function injectSocketIO(server) {
const io = new Server(server, {
cors: {
origin: [
'http://localhost:5173',
'http://localhost:4173',
process.env?.ORIGIN ?? 'http://localhost',
'https://tauri.localhost' // Remote Tauri App (SongTextProjector for example)
]
}
});
io.use((socket, next) => {
const token = socket.handshake.auth.token;
if (token && token == process.env.PUBLIC_LIVEVIEW_TOKEN) next();
else next(new Error('Not Authorized'));
});
/*
io.engine.on("headers", (headers, request) => {
if (!request.headers.cookie) return;
const cookies = parse(request.headers.cookie);
if (!cookies.auth_session) {
return false;
}
});
socketioAuth(io, {
authenticate: function (socket, data, callback) {
//get credentials sent by the client
var username = data.username;
var password = data.password;
var user = { password: ''};
// db.findUser('User', {username:username}, function(err, user) {
// inform the callback of auth success/failure
// if (err || !user) return callback(new Error("User not found"));
return callback(null, user.password == password);
// });
}
});
*/
const current_connections = {};
io.on('connection', (socket) => {
let identifier = null;
socket.on('ready', async (data) => {
const playlistId = data?.playlistId;
const joinCode = data?.joinCode;
if (!playlistId || !joinCode) return;
identifier = `${playlistId}_${joinCode}`;
if (!current_connections[identifier]) {
current_connections[identifier] = {
playlist: data?.playlist ?? '{}',
currentSongId: 0,
currentCoupletId: 0
};
} else {
// if (current_connections[identifier].playlist === "{}") {
if (data?.playlist && data?.playlist !== '{}') {
current_connections[identifier].playlist = data?.playlist ?? '{}';
if (current_connections[identifier].playlist !== '{}')
io.to(identifier).emit('syncPlaylist', {
playlist: current_connections[identifier].playlist,
from: socket.id
});
}
}
// Leave the other rooms, if it has
for (const room of socket.rooms) {
if (room !== socket.id) socket.leave(room);
}
await socket.join(identifier);
const sockets = await io.in(identifier).fetchSockets();
io.to(identifier).emit('userStatus', {
message: 'user has joined: ' + socket.id,
count: sockets.length
});
socket.emit('syncAll', {
playlist: current_connections[identifier].playlist,
currentSongId: current_connections[identifier].currentSongId,
currentCoupletId: current_connections[identifier].currentCoupletId,
from: socket.id
});
});
socket.on('setPlaylist', (playlist) => {
if (identifier && current_connections[identifier]) {
current_connections[identifier].playlist = playlist;
io.to(identifier).emit('syncPlaylist', {
playlist,
from: socket.id
});
}
});
socket.on('setCurrentSongId', (id) => {
if (identifier && current_connections[identifier]) {
current_connections[identifier].currentSongId = id;
io.to(identifier).emit('syncCurrentSongId', {
currentSongId: id,
from: socket.id
});
current_connections[identifier].currentCoupletId = 0;
io.to(identifier).emit('syncCurrentCoupletId', {
currentCoupletId: 0,
from: socket.id
});
}
});
socket.on('setCurrentCoupletId', (id) => {
if (identifier && current_connections[identifier]) {
current_connections[identifier].currentCoupletId = id;
io.to(identifier).emit('syncCurrentCoupletId', {
currentCoupletId: id,
from: socket.id
});
}
});
socket.on('disconnecting', async () => {
for (const room of socket.rooms) {
if (room !== socket.id) {
const sockets = await io.in(room).fetchSockets();
socket.to(room).emit('userStatus', {
message: 'user has left: ' + socket.id,
count: sockets.length - 1
});
// if (sockets.length === 1) {
// delete current_connections[identifier];
// }
}
}
});
});
console.log('SocketIO injected');
}