我有一个存储所有连接的TCP套接字的模块。当一个套接字关闭时,我有一个应该删除套接字的功能。由于某种原因,套接字并不总是被删除,我收到此错误:
错误:此套接字已被另一方结束
我的模块:
module.exports = Agent;
var Agents = []; // Stores all sockets
// This fires when a new socket is created
Agent.prototype.subscribe = function (server, socket) {
this.server = server;
this.socket = socket;
this.id = socket.name;
this.request = socket.request;
Agents.push(this);
}
// This fires when a socket is closed
Agent.prototype.onclose = function (reason) {
console.log('[Removing agent socket with reason: %s]', reason);
console.log(Agents.length);
Agents.splice(Agents.indexOf(this), 1);
console.log(Agents.length);
};
我想我可能会以错误的方式移除套接字?有什么想法吗?