SpotifyDisk-node/switchServer.js
2020-03-21 13:33:37 +00:00

73 lines
2.1 KiB
JavaScript

var net = require('net');
const { exec } = require("child_process");
var path = require('path');
// var localPlayerFunctioins = require('./bin/local')
async function playStuff() {
return new Promise(function (resolve, reject) {
exec(`node player.js file`, (error, stdout, stderr) => {
if (error) {
if (error.message.includes('already mounted')) {
resolve(true)
return;
} else {
resolve(false)
return;
}
} if (stderr) {
resolve(false)
return;
}
resolve(true)
})
})
}
async function pauseStuff() {
return new Promise(function (resolve, reject) {
exec(`node player.js pause`, (error, stdout, stderr) => {
if (error) {
if (error.message.includes('already mounted')) {
resolve(true)
return;
} else {
resolve(false)
return;
}
} if (stderr) {
resolve(false)
return;
}
resolve(true)
})
})
}
var server = net.createServer(function(connection) {
connection.on('data', function(data) {
// data is a Buffer, so we'll .toString() it for this example
console.log(data.toString());
const input = data.toString()
if (input.includes('play')) {
playStuff()
} else if (input.includes('pause')) {
pauseStuff()
}
});
});
// This creates a UNIX socket in the current directory named "nodejs_bridge.sock"
server.listen((path.resolve(__dirname, "nodejs_bridge.sock")));
// Make sure we close the server when the process exits so the file it created is removed
process.on('exit', function() {
server.close();
});
// Call process.exit() explicitly on ctl-c so that we actually get that event
process.on('SIGINT', function() {
process.exit();
});
// Resume stdin so that we don't just exit immediately
process.stdin.resume();