After I constructed a pair of TCP servers and clients using NodeJS,
The client sends a TCP request to the server every 5 seconds:
function doRunLoopDo() {
log.info('timeout')
pushNotificationTest();
setTimeout(() = > {
doRunLoopDo();
}, 5000);
}
function pushNotificationTest() {
let pushMessage = {
command: 'newElement'.method: 'broadcastToGroup'.data: {group: "gui" },
id: Math.floor(Math.random() * 1e100),
dateTime: Date.now(),
value: Math.floor(Math.random() * 1000)
}
TCPClient.sendMessage(pushMessage);
}
Copy the code
Then I terminate the server side,
On the TCP client I added the following error handling branch:
async function startClient(ip = configJson.TCP.client.ip, port = configJson.TCP.client.port) {
client = new net.Socket();
await client.connect(port, ip, () = > {
console.log(" in tcpclient.js connect callback!!!");
});
client.on('error'.(error) = > {
log.info('TCP Client: Connection error - destroy Connection '+error);
})
Copy the code
Output from the final runtime:
2019-02-22 14:55:07 [ INFO – Jerry Statemachine] TCP Client: Connection error – destroy Connection Error: This socket is closed
For more of Jerry’s original articles, please follow the public account “Wang Zixi “: