-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
59 lines (45 loc) · 1.18 KB
/
index.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
'use strict';
const EventEmitter = require('events').EventEmitter;
const net = require('net');
const connection = require('./connection');
const defaultServerOpts = {
port: 6600
};
module.exports = function (cmdHandler) {
const mpd = Object.create(new EventEmitter());
mpd.connections = [];
mpd.server = net.createServer((socket) => {
let con = connection(socket, cmdHandler);
mpd.connections.push(con);
mpd.emit('connect', con);
con.on('close', () => {
mpd.emit('disconnect', con);
let i = mpd.connections.indexOf(con);
if(i != -1) {
mpd.connections.splice(i, 1);
}
});
con.on('error', err => {
mpd.emit('error', err, con);
mpd.emit('disconnect', con);
});
}).on('error', (err) => {
mpd.emit('error', err);
});
mpd.listen = function(options, cb) {
if (typeof options === 'function') {
cb = options;
options = undefined;
}
options = options || defaultServerOpts;
//https://nodejs.org/api/net.html#net_server_listen_options_callback
mpd.server.listen(options, cb);
};
mpd.systemUpdate = function(subSystem) {
// send updates to connections
for (let c of mpd.connections) {
c.systemUpdate(subSystem);
}
};
return mpd;
};