node.js socket.io 这是内存泄漏吗?

发布于 2024-12-26 11:19:10 字数 947 浏览 2 评论 0原文

如果我有这样的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

兔小萌 2025-01-02 11:19:11

不存在内存泄漏。你是说你正在使用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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文