node-ipc/test/TCP/TCPServer.js

53 lines
1.1 KiB
JavaScript
Raw Normal View History

2021-07-03 07:35:02 +10:00
import ipc from '../../node-ipc.js';
import process from 'process';
2016-01-10 20:03:05 +11:00
2021-07-03 07:35:02 +10:00
const dieAfter = 30e3;
function killServerProcess(){
process.exit(0);
}
2015-12-05 13:40:38 +11:00
2016-01-10 18:23:40 +11:00
//die after 60 seconds
setTimeout(
2021-07-03 07:35:02 +10:00
killServerProcess,
2016-01-10 18:23:40 +11:00
dieAfter
);
2015-12-05 13:40:38 +11:00
2016-01-10 18:23:40 +11:00
ipc.config.id = 'tcpServer';
2015-12-05 13:40:38 +11:00
ipc.config.retry= 1500;
2016-01-10 18:23:40 +11:00
ipc.config.networkPort=8300;
ipc.config.silent=true;
2015-12-05 13:40:38 +11:00
ipc.serveNet(
2016-01-10 18:23:40 +11:00
function serverStarted(){
2015-12-05 13:40:38 +11:00
ipc.server.on(
'message',
2016-01-10 18:23:40 +11:00
function gotMessage(data,socket){
2021-07-03 07:35:02 +10:00
console.log('Server recieved message',data);
if(data.message=="END"){
killServerProcess();
}
2015-12-05 13:40:38 +11:00
ipc.server.emit(
socket,
'message',
{
id : ipc.config.id,
message : 'I am TCP server!'
}
);
2021-07-03 07:35:02 +10:00
console.log('server emitted data')
2015-12-05 13:40:38 +11:00
}
);
2021-07-03 07:35:02 +10:00
console.log('TCP server up');
2015-12-05 13:40:38 +11:00
}
);
ipc.server.start();
2021-07-03 07:35:02 +10:00
export {
killServerProcess as default,
killServerProcess
}