discord-rpc/test-rpc-server/rpc-server.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-06-27 19:23:09 +00:00
const net = require('net');
const RpcMessage = require('./rpc-message');
let PipePrefix;
2017-06-27 20:19:36 +00:00
let PipePostfix;
2017-06-27 19:23:09 +00:00
if (process.platform == 'win32') {
2017-06-27 20:19:36 +00:00
PipePrefix = '\\\\.\\pipe\\';
PipePostfix = '';
2017-06-27 19:23:09 +00:00
}
else {
PipePrefix = "/tmp";
2017-06-27 20:19:36 +00:00
PipePostfix = '.pipe';
2017-06-27 19:23:09 +00:00
}
const PipePath = PipePrefix + 'DiscordRpcServer' + PipePostfix;
let connections = 0;
2017-06-27 19:23:09 +00:00
const server = net.createServer(function(sock) {
connections += 1;
console.log('Server: on connection', connections);
let myConnection = connections;
2017-06-27 19:23:09 +00:00
sock.on('data', function(data) {
2017-06-27 19:23:09 +00:00
const msgObj = RpcMessage.deserialize(data);
if (msgObj != null) {
console.log('Server: on data:', myConnection, msgObj);
2017-06-27 19:23:09 +00:00
}
else {
console.log('Server: got some data', data.toString());
2017-06-27 19:23:09 +00:00
}
});
sock.on('end', function() {
connections -= 1;
console.log('Server: on end', connections);
2017-06-27 19:23:09 +00:00
});
});
server.on('close', function(){
console.log('Server: on close');
})
try {
server.listen(PipePath, function(){
console.log('Server: on listening');
});
} catch(e) {
console.error('could not start server:', e);
}