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

63 lines
1.7 KiB
JavaScript
Raw Normal View History

2017-06-27 20:23:09 +01:00
const net = require('net');
const {OPCODES, PipePath, RpcMessage} = require('./rpc-message');
2017-06-27 20:23:09 +01:00
const APP_ID = '12345678910';
global.isConnected = false;
global.timeoutId = null;
2017-06-27 20:23:09 +01:00
function sendMesg(testUpdatesToSend, stream) {
const msgObj = {
state: (testUpdatesToSend % 2 == 0) ? 'In a match' : 'In Lobby',
details: 'Excited'
2017-06-27 20:23:09 +01:00
};
console.log('Client: send update:', msgObj);
stream.write(RpcMessage.send(msgObj));
2017-06-27 20:23:09 +01:00
}
function sendMessageLoop(testUpdatesToSend, interval, stream) {
global.timeoutId = null;
if (!global.isConnected) {
return;
}
2017-06-27 20:23:09 +01:00
sendMesg(testUpdatesToSend, stream);
if (testUpdatesToSend > 1) {
global.timeoutId = setTimeout(() => {sendMessageLoop(testUpdatesToSend - 1, interval, stream)}, interval);
2017-06-27 20:23:09 +01:00
} else {
shutdown();
}
}
2017-06-27 21:19:36 +01:00
const client = net.connect(PipePath, function(stream) {
2017-06-27 20:23:09 +01:00
console.log('Client: on connection');
global.isConnected = true;
client.write(RpcMessage.handshake(APP_ID));
sendMessageLoop(10, 3000, client);
2017-06-27 20:23:09 +01:00
});
client.on('data', function(data) {
const msgObj = RpcMessage.deserialize(data);
if (msgObj != null) {
const {opcode, data} = msgObj;
console.log(`Client: got opcode: ${opcode}, data: ${JSON.stringify(data)}`);
if (opcode == OPCODES.CLOSE) {
shutdown();
}
2017-06-27 20:23:09 +01:00
} else {
console.log('Client: got some data', data);
2017-06-27 20:23:09 +01:00
}
});
client.on('end', function() {
global.isConnected = false;
2017-06-27 20:23:09 +01:00
console.log('Client: on end');
});
function shutdown() {
if (global.timeoutId !== null) {
clearTimeout(global.timeoutId);
global.timeoutId = null;
}
2017-06-27 20:23:09 +01:00
client.end();
}