我利用nodejs构造了一对TCP服务器和客户端之后,
客户端每隔5秒向服务器端发送一个TCP请求:
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);
}
然后我终止服务器端,
在TCP客户端我加上了如下的错误处理分支:
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);
})
最后运行时的输出:
2019-02-22 14:55:07 [ INFO - Jerry Statemachine] TCP Client: Connection error - destroy Connection Error: This socket is closed