forked from ytmdesktop/ytmdesktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
231 lines (182 loc) · 7.05 KB
/
server.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const { ipcMain } = require('electron');
const os = require( 'os' );
//const mdns = require('mdns-js');
const networkInterfaces = os.networkInterfaces();
const qrcode = require('qrcode-generator');
const ip = '0.0.0.0';
const port = 9863;
const http = require('http');
const pattIgnoreInterface = /(virtual)\w*/gmi;
let connectionsTotal = 0;
/*function createAdvertisement() {
try {
var ad = mdns.createAdvertisement(mdns.tcp('_http'), port,
{
name:'ytmdesktop._companion',
port: port,
txt: {
txtvers:'1'
},
}
);
ad.start();
} catch (ex) {
handleError(ex);
}
}
function handleError(error) {
switch (error.errorCode) {
case mdns.kDNSServiceErr_Unknown:
console.warn(error);
setTimeout(createAdvertisement, 5000);
break;
default:
throw error;
}
}
createAdvertisement();
*/
const server = http.createServer( ( req, res ) => {
let collection = '';
Object.keys(networkInterfaces).forEach( ( v, k ) => {
if( !pattIgnoreInterface.test( v ) ) {
networkInterfaces[v].forEach( ( vv, kk) => {
if ( vv.family == 'IPv4' && vv.internal == false ) {
var qr = qrcode(0, 'M');
qr.addData(`{ "name": "${os.hostname()}", "ip":"${vv.address}" }`);
qr.make();
collection += `
<div class="row" style="margin-top: 10px;">
<div class="col s12">
<div class="card transparent z-depth-0">
<div class="card-content">
<div class="row" style="margin-bottom: 0 !important;">
<div class="col s6">
<img class="card card-content" style="padding: 10px !important;" src="${qr.createDataURL(6)}" width="180" />
</div>
<div class="col s6 white-text" style="border-left: solid 1px #222 !important; heigth: 500px; margin-top: 2.8% !important;">
<h3>${os.hostname()}</h3>
<h5 style="font-weight: 100 !important;">${vv.address}</h5>
</div>
</div>
</div>
</div>
</div>
</div>
`;
}
});
}
} );
res.writeHead( 200, {'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*'});
res.write(`<html>
<head>
<title>YTMDesktop Companion Server</title>
<meta http-equiv="refresh" content="60">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
<style>
html, body {
margin: 0;
padding: 0;
text-align: center;
background: linear-gradient(to right top, #000 20%, #1d1d1d 80%);
}
h5 {
margin: 1rem 0 1rem 0 !important;
}
</style>
</head>
<body>
<div class="col s12">
<h3 class="red-text">YTMDesktop Companion</h3>
</div>
<div class="container" style="margin-top: 13%;">
${collection}
</div>
<div class="card-panel transparent z-depth-0" style="position: fixed; bottom: 0; text-align: center; width: 100%;">
<a class="white-text btn-flat tooltipped" data-position="top" data-tooltip="Devices Connected"><i class="material-icons left">devices</i>${connectionsTotal}</a>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
var elems = document.querySelectorAll('.tooltipped');
M.Tooltip.init(elems, {});
});
</script>
</html>`);
res.end();
} );
server.listen(port, ip);
const io = require('socket.io')(server);
function convertToHuman(time) {
var _aux = time;
var _minutes = 0;
var _seconds = 0;
while (_aux >= 60) {
_aux = _aux - 60;
_minutes++;
}
_seconds = _aux;
if (_seconds < 10) {
return _minutes + ':0' + _seconds;
}
return _minutes + ':' + _seconds;
}
setInterval( () => {
connectionsTotal = Object.keys(io.sockets.sockets).length;
console.log(connectionsTotal + ' devices connected');
}, 1000);
io.on('connection', (socket) => {
let timer = setInterval( () => {
ipcMain.emit('what-is-song-playing-now');
}, 500);
socket.on('disconnect', () => {
clearInterval(timer);
});
ipcMain.on('song-playing-now-is', (data) => {
data['song']['durationHuman'] = convertToHuman(data['song']['duration']);
data['player']['seekbarCurrentPositionHuman'] = convertToHuman(data['player']['seekbarCurrentPosition']);
io.emit('media-now-playing', data);
});
socket.on('media-commands', (cmd, data) => {
switch( cmd ) {
case 'previous-track':
ipcMain.emit('media-previous-track', true);
break;
case 'play-track':
ipcMain.emit('media-play-pause', true);
break;
case 'pause-track':
ipcMain.emit('media-play-pause', true);
break;
case 'next-track':
ipcMain.emit('media-next-track', true);
break;
case 'thumbs-up-track':
ipcMain.emit('media-up-vote', true);
break;
case 'thumbs-down-track':
ipcMain.emit('media-down-vote', true);
break;
case 'volume-up':
ipcMain.emit('media-volume-up', true);
break;
case 'volume-down':
ipcMain.emit('media-volume-down', true);
break;
case 'forward-X-seconds':
ipcMain.emit('media-forward-X-seconds', true);
break;
case 'rewind-X-seconds':
ipcMain.emit('media-rewind-X-seconds', true);
break;
case 'change-seekbar':
ipcMain.emit('media-change-seekbar', data);
break;
}
} );
});
console.log("Companion Server listening on port " + port);