mirror of
https://github.com/yuzu-emu/discord-rpc.git
synced 2024-11-27 18:54:24 +01:00
adding a js test server
This commit is contained in:
parent
661b5fa3b5
commit
6271f29996
9
test-rpc-server/package.json
Normal file
9
test-rpc-server/package.json
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"name": "test-rpc-server",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"server": "node rpc-server.js",
|
||||
"client": "node test-client.js"
|
||||
}
|
||||
}
|
24
test-rpc-server/rpc-message.js
Normal file
24
test-rpc-server/rpc-message.js
Normal file
@ -0,0 +1,24 @@
|
||||
|
||||
module.exports = class RpcMessage {
|
||||
static serialize(obj) {
|
||||
const serializedJson = JSON.stringify(obj);
|
||||
const msgLen = 4 + serializedJson.length;
|
||||
let buff = Buffer.alloc(msgLen);
|
||||
buff.writeInt32LE(msgLen, 0);
|
||||
buff.write(serializedJson, 4, serializedJson.length, 'utf-8');
|
||||
return buff;
|
||||
}
|
||||
|
||||
static deserialize(buff) {
|
||||
const msgLen = buff.readInt32LE(0);
|
||||
if (buff.length < msgLen) {
|
||||
return null;
|
||||
}
|
||||
const msg = buff.toString('utf-8', 4, msgLen);
|
||||
try {
|
||||
return JSON.parse(msg);
|
||||
} catch(e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
45
test-rpc-server/rpc-server.js
Normal file
45
test-rpc-server/rpc-server.js
Normal file
@ -0,0 +1,45 @@
|
||||
const net = require('net');
|
||||
const RpcMessage = require('./rpc-message');
|
||||
|
||||
console.log('Start up');
|
||||
|
||||
let PipePrefix;
|
||||
if (process.platform == 'win32') {
|
||||
PipePrefix = "\\\\.\\pipe\\";
|
||||
}
|
||||
else {
|
||||
PipePrefix = "/tmp";
|
||||
}
|
||||
|
||||
const PipePath = PipePrefix + "DiscordRpcServer";
|
||||
|
||||
var server = net.createServer(function(stream) {
|
||||
console.log('Server: on connection')
|
||||
|
||||
stream.on('data', function(data) {
|
||||
const msgObj = RpcMessage.deserialize(data);
|
||||
if (msgObj != null) {
|
||||
console.log('Server: on data:', msgObj);
|
||||
}
|
||||
else {
|
||||
console.log('Server: got some data');
|
||||
}
|
||||
});
|
||||
|
||||
stream.on('end', function() {
|
||||
console.log('Server: on end')
|
||||
server.close();
|
||||
});
|
||||
});
|
||||
|
||||
server.on('close', function(){
|
||||
console.log('Server: on close');
|
||||
})
|
||||
|
||||
try {
|
||||
server.listen(PipePath, function(){
|
||||
console.log('Server: on listening');
|
||||
});
|
||||
} catch(e) {
|
||||
console.error('could not start server:', e);
|
||||
}
|
48
test-rpc-server/test-client.js
Normal file
48
test-rpc-server/test-client.js
Normal file
@ -0,0 +1,48 @@
|
||||
const net = require('net');
|
||||
const RpcMessage = require('./rpc-message');
|
||||
|
||||
var PIPE_NAME = "DiscordRpcServer";
|
||||
var PIPE_PATH = "\\\\.\\pipe\\" + PIPE_NAME;
|
||||
|
||||
const msg = new RpcMessage();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
const client = net.connect(PIPE_PATH, function(stream) {
|
||||
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();
|
||||
}
|
Loading…
Reference in New Issue
Block a user