2019-03-19 18:13:15 +08:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2019-04-12 19:46:27 +08:00
|
|
|
const log = require("./log.js").log;
|
2020-05-05 18:16:33 +08:00
|
|
|
const Queue = require("./queue").Queue;
|
2019-03-19 18:13:15 +08:00
|
|
|
|
2019-03-19 23:22:37 +08:00
|
|
|
function initConnection(request) {
|
|
|
|
let ws = request.accept();
|
|
|
|
log("ws connection accepted from origin", request.origin);
|
|
|
|
|
|
|
|
let parts = (request.resourceURL.query.server || "").split(":");
|
2020-03-29 18:35:26 +08:00
|
|
|
let host = parts[0] || "localhost";
|
2019-03-19 23:22:37 +08:00
|
|
|
let port = Number(parts[1]) || 6600;
|
|
|
|
log(`connecting to mpd at ${host}:${port}`);
|
2019-03-19 18:13:15 +08:00
|
|
|
|
|
|
|
let mpd = new (require("net").Socket)();
|
|
|
|
mpd.setTimeout(0);
|
2019-03-19 23:22:37 +08:00
|
|
|
mpd.connect(port, host);
|
2019-03-19 18:13:15 +08:00
|
|
|
|
2020-05-05 18:16:33 +08:00
|
|
|
// data coming from the response parser
|
|
|
|
let queue = new Queue(mpd);
|
|
|
|
queue.on("response", data => {
|
|
|
|
log("ws <--", data);
|
|
|
|
ws.send(JSON.stringify(data));
|
|
|
|
});
|
2019-03-19 18:13:15 +08:00
|
|
|
|
2020-05-05 18:16:33 +08:00
|
|
|
// data going into the response parser
|
2019-03-19 18:13:15 +08:00
|
|
|
ws.on("message", message => {
|
|
|
|
log("ws -->", message.utf8Data);
|
2020-05-05 18:16:33 +08:00
|
|
|
queue.add(message.utf8Data);
|
2019-03-19 18:13:15 +08:00
|
|
|
});
|
|
|
|
|
2020-05-05 18:16:33 +08:00
|
|
|
// client closes
|
2019-03-19 18:13:15 +08:00
|
|
|
ws.on("close", (reasonCode, description) => {
|
|
|
|
log(`ws ${ws.remoteAddress} disconnected`);
|
|
|
|
mpd.end();
|
|
|
|
});
|
|
|
|
|
2020-05-05 18:16:33 +08:00
|
|
|
// server closes
|
2019-03-19 18:13:15 +08:00
|
|
|
mpd.on("close", () => {
|
|
|
|
log("mpd disconnected");
|
|
|
|
ws.close();
|
|
|
|
});
|
|
|
|
|
2020-05-05 18:16:33 +08:00
|
|
|
// fail to conect
|
2019-03-20 05:10:02 +08:00
|
|
|
mpd.on("error", () => {
|
|
|
|
log("mpd connection error");
|
|
|
|
ws.close();
|
|
|
|
});
|
2019-03-19 18:13:15 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 19:46:27 +08:00
|
|
|
exports.logging = function(enabled) {
|
|
|
|
log.enabled = enabled;
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:20:30 +08:00
|
|
|
exports.ws2mpd = function(httpServer, requestValidator) {
|
2019-03-19 18:13:15 +08:00
|
|
|
function ready() { log("ws2mpd attached to a http server", httpServer.address()); }
|
|
|
|
(httpServer.listening ? ready() : httpServer.on("listening", ready));
|
|
|
|
|
|
|
|
let wsServer = new (require("websocket").server)({
|
|
|
|
httpServer,
|
|
|
|
autoAcceptConnections: false
|
|
|
|
});
|
2019-03-19 23:22:37 +08:00
|
|
|
|
|
|
|
wsServer.on("request", request => {
|
2019-04-15 20:20:30 +08:00
|
|
|
if (requestValidator && !requestValidator(request)) {
|
2019-03-19 23:22:37 +08:00
|
|
|
log("rejecting connection from origin", request.origin);
|
|
|
|
return request.reject();
|
|
|
|
}
|
|
|
|
initConnection(request);
|
|
|
|
});
|
2019-03-19 18:13:15 +08:00
|
|
|
}
|