Socket.io - 当客户端断开连接时关闭套接字

时间:2021-01-13 12:49:14

标签: javascript node.js websocket socket.io

我想在一个耗时的过程中从服务器向客户端发送消息。在快乐的道路上它运行良好。

但是我看到当页面刷新时,客户端断开连接,并将作为具有新套接字 ID 的新套接字重新连接,但是在服务器端,我观察到,即使客户端断开连接。

客户-

import socketIOClient from "socket.io-client";
this.socket = socketIOClient("http://localhost:5005"); //in constructor(react)

this.socket.on('generationAck', (ack) => { 
    console.log("message from server", this.socket.id, ack);
});

服务器-

var server = app.listen(5005, function () {
    console.log('listening for requests on port 5005,');
});
io.on('connection', socket => {
    //some time consuming task in a loop
    //I want to send some acknowledgement to client
    socket.emit('generationAck', msg)

    socket.on('disconnect', function () { //triggered when client page refreshed
         socket.disconnect(); // not seems working
    });

});

我在页面刷新后再次执行该操作,发现服务器向旧套接字发出一些消息,向新套接字发出一些消息。发送给新消息的消息仅由客户端接收。

这里有什么问题?有谁可以帮忙吗?

0 个答案:

没有答案