node.js socket.io 这是内存泄漏吗?
如果我有这样的代码:
io.sockets.on('connection', function (socket) {
// setup listeners for this socket
setup_socket_events(socket);
});
function setup_socket_events(socket)
{
socket.on('disconnect', function(){event_disconnect(socket);});
}
function event_disconnect(socket)
{
console.log("disconnect");
}
当套接字断开连接时,这是内存泄漏吗?如果是,为什么以及如何阻止它?
更新: 尝试以下代码从assertvarnish创建循环断言
var assertvanish = require('assertvanish');
io.sockets.on('connection', function (socket) {
// setup listeners for this socket
setup_socket_events(socket);
});
function setup_socket_events(socket)
{
socket.on('disconnect', event_disconnect(socket) );
}
function event_disconnect(socket)
{
return function() {
console.log("disconnect");
// do something with socket
assertvanish(socket, 5000);
}
}
If I have code like this:
io.sockets.on('connection', function (socket) {
// setup listeners for this socket
setup_socket_events(socket);
});
function setup_socket_events(socket)
{
socket.on('disconnect', function(){event_disconnect(socket);});
}
function event_disconnect(socket)
{
console.log("disconnect");
}
When the socket disconnects, is this a memory leak, and if so, why and how do you stop it?
UPDATE:
Trying the following code creates a cyclic assertion from assertvarnish
var assertvanish = require('assertvanish');
io.sockets.on('connection', function (socket) {
// setup listeners for this socket
setup_socket_events(socket);
});
function setup_socket_events(socket)
{
socket.on('disconnect', event_disconnect(socket) );
}
function event_disconnect(socket)
{
return function() {
console.log("disconnect");
// do something with socket
assertvanish(socket, 5000);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不存在内存泄漏。你是说你正在使用assertvanish。我认为
assertvanish(socket, 10000)
只是说套接字在 10 秒后不会被删除,这没关系,因为你想让套接字永远保持活动状态,对吗?There is no memory leak. You are saying that you are using assertvanish. I think that
assertvanish(socket, 10000)
just says that socket is not deleted after 10s, which is ok, because you want to keep your socket alive forever, right?