Test server: print more diagnostics, track connections more

This commit is contained in:
Chris Marsh 2017-06-29 08:58:03 -07:00
parent 568de58f36
commit d14e810be0
2 changed files with 16 additions and 14 deletions

View file

@ -18,6 +18,8 @@ module.exports = class RpcMessage {
try { try {
return JSON.parse(msg); return JSON.parse(msg);
} catch(e) { } catch(e) {
console.log(`failed to parse "${msg}"`);
console.error(e);
return {}; return {};
} }
} }

View file

@ -1,8 +1,6 @@
const net = require('net'); const net = require('net');
const RpcMessage = require('./rpc-message'); const RpcMessage = require('./rpc-message');
console.log('Start up');
let PipePrefix; let PipePrefix;
let PipePostfix; let PipePostfix;
if (process.platform == 'win32') { if (process.platform == 'win32') {
@ -13,25 +11,27 @@ else {
PipePrefix = "/tmp"; PipePrefix = "/tmp";
PipePostfix = '.pipe'; PipePostfix = '.pipe';
} }
const PipePath = PipePrefix + 'DiscordRpcServer' + PipePostfix;
let connections = 0;
const PipePath = PipePrefix + "DiscordRpcServer" + PipePostfix; const server = net.createServer(function(sock) {
connections += 1;
console.log('Server: on connection', connections);
let myConnection = connections;
var server = net.createServer(function(stream) { sock.on('data', function(data) {
console.log('Server: on connection');
stream.on('data', function(data) {
const msgObj = RpcMessage.deserialize(data); const msgObj = RpcMessage.deserialize(data);
if (msgObj != null) { if (msgObj != null) {
console.log('Server: on data:', msgObj); console.log('Server: on data:', myConnection, msgObj);
} }
else { else {
console.log('Server: got some data'); console.log('Server: got some data', data.toString());
} }
}); });
stream.on('end', function() { sock.on('end', function() {
console.log('Server: on end') connections -= 1;
server.close(); console.log('Server: on end', connections);
}); });
}); });