discord-rpc/test-rpc-server/rpc-server.js
Chris Marsh 381edef1fa testing
2017-07-10 14:54:58 -07:00

62 lines
1.6 KiB
JavaScript

const net = require('net');
const repl = require('repl');
const {PipePath, RpcMessage} = require('./rpc-message');
let connectionNonce = 0;
global.connections = {};
const server = net.createServer(function(sock) {
connectionNonce += 1;
console.log('Server: on connection', connectionNonce);
let myConnection = connectionNonce;
let messages = 0;
global.connections[myConnection] = sock;
sock.on('data', function(data) {
messages++;
const msgObj = RpcMessage.deserialize(data);
if (msgObj != null) {
const {opcode, data} = msgObj;
console.log(`\nServer (${myConnection}): got opcode: ${opcode}, data: ${JSON.stringify(data)}`);
}
else {
console.log('\nServer: got some data', data.toString());
}
});
sock.on('end', function() {
delete global.connections[myConnection];
console.log('\nServer: on end', myConnection);
});
});
server.on('close', function(){
console.log('\nServer: on close');
});
try {
server.listen(PipePath, function(){
console.log('\nServer: on listening');
});
} catch(e) {
console.error('\nServer: could not start:', e);
}
const replServer = repl.start({prompt: '> ', useGlobal: true, breakEvalOnSigint: true});
replServer.defineCommand('kill', {
help: 'Kill a client',
action(who) {
this.bufferedCommand = '';
who = parseInt(who, 10);
const sock = global.connections[who];
if (sock) {
console.log('killing', who);
sock.write(RpcMessage.sendClose(123, 'killed'));
sock.destroy();
}
this.displayPrompt();
}
});