2017-06-27 19:23:09 +00:00
|
|
|
const net = require('net');
|
|
|
|
const RpcMessage = require('./rpc-message');
|
|
|
|
|
2017-06-27 20:19:36 +00:00
|
|
|
let PipePrefix;
|
|
|
|
let PipePostfix;
|
|
|
|
if (process.platform == 'win32') {
|
|
|
|
PipePrefix = '\\\\.\\pipe\\';
|
|
|
|
PipePostfix = '';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PipePrefix = "/tmp";
|
|
|
|
PipePostfix = '.pipe';
|
|
|
|
}
|
2017-06-27 19:23:09 +00:00
|
|
|
|
2017-06-27 20:19:36 +00:00
|
|
|
const PipePath = PipePrefix + "DiscordRpcServer" + PipePostfix;
|
2017-06-27 19:23:09 +00:00
|
|
|
|
|
|
|
function sendMesg(testUpdatesToSend, stream) {
|
|
|
|
const msgObj = {
|
|
|
|
name: 'My Awesome Game',
|
|
|
|
state: (testUpdatesToSend % 2 == 0) ? 'In a match' : 'In Lobby'
|
|
|
|
};
|
|
|
|
console.log('Client: send update:', msgObj);
|
|
|
|
stream.write(RpcMessage.serialize(msgObj));
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendMessageLoop(testUpdatesToSend, interval, stream) {
|
|
|
|
sendMesg(testUpdatesToSend, stream);
|
|
|
|
if (testUpdatesToSend > 1) {
|
|
|
|
setTimeout(() => {sendMessageLoop(testUpdatesToSend - 1, interval, stream)}, interval);
|
|
|
|
} else {
|
|
|
|
shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 20:19:36 +00:00
|
|
|
const client = net.connect(PipePath, function(stream) {
|
2017-06-27 19:23:09 +00:00
|
|
|
console.log('Client: on connection');
|
|
|
|
|
|
|
|
sendMessageLoop(5, 3000, client);
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('data', function(data) {
|
|
|
|
const msgObj = RpcMessage.deserialize(data);
|
|
|
|
if (msgObj != null) {
|
|
|
|
console.log('Client: got data:', msgObj);
|
|
|
|
} else {
|
|
|
|
console.log('Client: got some data');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('end', function() {
|
|
|
|
console.log('Client: on end');
|
|
|
|
});
|
|
|
|
|
|
|
|
function shutdown() {
|
|
|
|
client.end();
|
|
|
|
}
|