-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect.js
129 lines (115 loc) · 3.94 KB
/
connect.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
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
const ngrok = require('ngrok');
const { format_trackID } = require("./utils.js")
const { WebSocketServer } = require("ws");
const { store } = require("./main.js");
const { networkInterfaces } = require("os")
const {
togglePlay,
playNext,
playPrevious
} = require("./mediaScripts.js");
var urlNgrok = null;
var wss = null;
const ngrokSetup = async function (domain, authtoken) {
try {
// Start ngrok and expose a local port (e.g., 5050)
urlNgrok = await ngrok.connect({
authtoken: authtoken,
proto: 'http', // Protocol to use (http or tcp)
addr: 5050, // Port on which your WebSocket server is running
region: 'us', // Optional: Specify ngrok region (e.g., 'us', 'eu', 'ap')
hostname: domain,
});
console.log("NGROK:", urlNgrok);
return urlNgrok;
} catch (err) {
urlNgrok = null;
console.error('Error starting ngrok:', err);
}
};
const ngrokShutdown = async function () {
try {
await ngrok.disconnect(); // stops all
urlNgrok = null;
} catch (err) {
console.error('Could not shutdown ngrok:', err);
}
};
const webSocketSetup = (current_song, spot_instance) => {
wss = new WebSocketServer({ port: 5050 });
wss.on('connection', (ws) => {
if (current_song.setUp)
format_trackID(current_song.trackID, spot_instance, 1).then((data) => {
ws.send(JSON.stringify({ type: "message", ...current_song, album: data }));
});
ws.on('error', (e) => { console.error("Error:", e) });
ws.on('close', (e) => {
console.error("Closed:", e);
clearInterval(keepAlive);
});
ws.on('message', function message(data) {
/*
Actions supported: "next", "prev", "play"
*/
const action = data.toString();
if (action === 'next')
playNext({ store, spot_instance });
if (action === 'prev')
playPrevious({ store, spot_instance })
if (action === 'play')
togglePlay({ store, spot_instance });
console.log("Received: %s", data)
});
const keepAlive = setInterval(() => {
ws.send(JSON.stringify({ type: "ping" }));
}, 240000);// 4 minutes
});
}
const webSocketShutdown = () => {
try {
wss.close();
wss = null;
} catch (err) {
console.error('Could not shutdown websocket server:', err);
}
}
const getClients = () => {
return wss?.clients ?? new Set();
}
const notify = (message) => {
if (wss === null) return;
console.log(wss.clients.size)
wss.clients.forEach((client) => {
client.send(JSON.stringify({ type: "message", ...message }));
})
}
const fetchIp = () => {
console.log(urlNgrok);
if (urlNgrok !== null)
return urlNgrok;
// Fetch local wifi ip
const nets = networkInterfaces();
const results = Object.create(null); // Or just '{}', an empty object
for (const name of Object.keys(nets)) {
for (const net of nets[name]) {
// Skip over non-IPv4 and internal (i.e. 127.0.0.1) addresses
// 'IPv4' is in Node <= 17, from 18 it's a number 4 or 6
const familyV4Value = typeof net.family === 'string' ? 'IPv4' : 4
if (net.family === familyV4Value && !net.internal) {
if (!results[name]) {
results[name] = [];
}
results[name].push(net.address);
}
}
}
console.log(results['en0']);
return "ws://" + results['en0'] + ":5050";
}
module.exports.ngrokSetup = ngrokSetup;
module.exports.ngrokShutdown = ngrokShutdown;
module.exports.webSocketSetup = webSocketSetup;
module.exports.webSocketShutdown = webSocketShutdown;
module.exports.getClients = getClients;
module.exports.notify = notify;
module.exports.fetchIp = fetchIp;