mirror of
https://github.com/yuzu-emu/discord-rpc
synced 2024-11-23 02:23:49 +00:00
60 lines
1.6 KiB
JavaScript
60 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.end(RpcMessage.sendClose(123, 'killed'));
|
|
}
|
|
this.displayPrompt();
|
|
}
|
|
});
|
|
|