2017-06-27 20:23:09 +01:00
|
|
|
const net = require('net');
|
2017-07-08 00:00:29 +01:00
|
|
|
const {OPCODES, PipePath, RpcMessage} = require('./rpc-message');
|
2017-06-27 20:23:09 +01:00
|
|
|
|
2017-07-08 00:00:29 +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 = {
|
2017-07-08 00:00:29 +01:00
|
|
|
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);
|
2017-07-08 00:00:29 +01:00
|
|
|
stream.write(RpcMessage.send(msgObj));
|
2017-06-27 20:23:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function sendMessageLoop(testUpdatesToSend, interval, stream) {
|
2017-07-08 00:00:29 +01:00
|
|
|
global.timeoutId = null;
|
|
|
|
if (!global.isConnected) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-27 20:23:09 +01:00
|
|
|
sendMesg(testUpdatesToSend, stream);
|
|
|
|
if (testUpdatesToSend > 1) {
|
2017-07-08 00:00:29 +01:00
|
|
|
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');
|
2017-07-08 00:00:29 +01:00
|
|
|
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) {
|
2017-07-08 00:00:29 +01:00
|
|
|
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 {
|
2017-07-08 00:00:29 +01:00
|
|
|
console.log('Client: got some data', data);
|
2017-06-27 20:23:09 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('end', function() {
|
2017-07-08 00:00:29 +01:00
|
|
|
global.isConnected = false;
|
2017-06-27 20:23:09 +01:00
|
|
|
console.log('Client: on end');
|
|
|
|
});
|
|
|
|
|
|
|
|
function shutdown() {
|
2017-07-08 00:00:29 +01:00
|
|
|
if (global.timeoutId !== null) {
|
|
|
|
clearTimeout(global.timeoutId);
|
|
|
|
global.timeoutId = null;
|
|
|
|
}
|
2017-06-27 20:23:09 +01:00
|
|
|
client.end();
|
|
|
|
}
|