简体   繁体   English

socket.emit在while循环中

[英]socket.emit in while loop

I am trying to emit some JSON to a server every 1 second. 我试图每1秒向服务器发出一些JSON。

while (i > 1) {
    var checkData = setInterval (function () {
        var db = new sqlite3.Database('hud_db_master.sqlite3');
        db.all("SELECT * FROM hud ORDER BY col_id DESC LIMIT 1;", function(err, rows) {
            console.log(rows);
            var jsonData = JSON.stringify(rows);
            socket.emit('clientMessage', jsonData, 'Pi-Voyager');
        });
    }, 1000);
}

I get a blank console for about 20 seconds and then the following error: 我得到一个空白的控制台大约20秒钟,然后出现以下错误:

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory

Before messing with setInterval I was able to successfully emit the data once upon execution. 在弄乱setInterval之前,我能够在执行后成功发出一次数据。

You call while (i > 1) { at the beginning but never bring it below 1 which puts it into an infinite loop that will run until out of memory 您在开始时调用while (i > 1) { ,但永远不要使其低于1,这会使它陷入无限循环,直到内存耗尽为止

If you remove the while loop, you should still successfully achieve the intended result from what I assume from your intentions 如果删除while循环,您仍然应该根据我的意图成功地达到预期的结果

EDIT: 编辑:

Also, your db doesn't make sense as you create a new one EVERY second. 而且,您每隔一秒钟创建一个新数据库时,数据库就没有意义。 Move the call to db creation OUTSIDE of the loop 将调用移到循环外的db创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM