-
Notifications
You must be signed in to change notification settings - Fork 0
/
nowplaying.js
51 lines (45 loc) · 1.68 KB
/
nowplaying.js
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
// Importing the required modules
const WebSocketServer = require('ws');
const http = require('http');
const cors = require('cors');
const obj = {}
// Creating a new websocket server
const wss = new WebSocketServer.Server({ port: 8974 })
function splitKeyValue(str) {
const parts = str.split(/(?<!s):/);
const key = parts[0]; // the key is the first part
const value = parts[1]; // the value is the second part
if (parts[2] != null) {
const mins = parts[2];
return [key, `${value}:${mins}`];
}
return [key, value];
}
// Creating connection using websocket
wss.on("connection", ws => {
console.log("new client connected");
// sending message
ws.on("message", data => {
console.log(splitKeyValue(data.toString()))
const [key, value] = splitKeyValue(data.toString()); // destructure the tuple into separate variables
obj[key] = value; // add the key and value to the object
});
// handling what to do when clients disconnects from server
ws.on("close", () => {
console.log("the client has connected");
});
// handling client connection error
ws.onerror = function () {
console.log("Some Error occurred")
}
});
const server = http.createServer((req, res) => {
cors()(req, res, () => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(obj));
});
});
console.log("The WebSocket server is running on port 8974");
server.listen(8975, () => {
console.log('Server listening on port 8975');
});