阻止NodeJS退出事件循环 [英] Prevent NodeJS from exiting event-loop

查看:229
本文介绍了阻止NodeJS退出事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿hava谷歌搜索并尝试了所有的东西 - 但似乎没有任何作用,所以现在我会问你们:

Hey hava googled and tried every thing - but nothing seems to work, so now i will ask you guys:

我如何制作我的节点应用程序。从停止退出 - 当我喜欢它继续运行并等待来自Web Socket连接的响应?
正如你所看到的,我在'PreventExit'函数中尝试了不同的东西。我只想要这款应用。继续从Web套接字连接接收数据 - 不关闭/退出。

How do i make my Node app. from stop exiting - when i like it to keep running and wait for response from a Web Socket connection? As you can see i have tried different stuff in the 'PreventExit' function. I simply want the app. to keep receiving data from the web socket connection - without closing/exiting.

var Gdax = require('gdax');
var WSemitter = require('../eventemitters/websocket-emitter');

module.exports = {
    initws: function () {
        var WebSocketEmit = new WSemitter();

        test();

        function test() {
            try {
                PreventExit();

                var websocket = new Gdax.WebsocketClient();
                websocket.on('message', function (data) {
                    console.log(data);
                    WebSocketEmit.fakeEvent();
                });
            }
            catch (err) {
                console.log(err);
            }
        }

        function PreventExit(){
            WebSocketEmit.on('fakeEvent', function () {
                console.log('Just fake - to keep node running!');
            });

            setTimeout(function(){}, 100000);
            process.stdin.resume();
        }
    }
}

UPDATE1:

My Web Socket提供商CoinBase最近更新了他们的API。从Coinbase-exchange到Gdax。我只是试图替换:

My Web Socket provider CoinBase has recently updated their API. From Coinbase-exchange to Gdax. I just tried to replace:

var Gdax = require('gdax');
var CoinbaseExchange = require('coinbase-exchange');

//var websocket = new Gdax.WebsocketClient();
var websocket = new CoinbaseExchange.WebsocketClient();

现在我的应用程序等了!但只有1个收到json对象后我的节点应用程序仍然退出!我如何让它保持开放而不仅仅是关闭?但是却继续从API接收数据?

and now my app waits! But after only 1 received json object my node app still exits!?? How do i make it keep the coonection open and not just shut down? But instead keeps receiving data from the API?

更新2

当我删除我的所有测试代码,并且只有与API相关的代码,它可以工作 - 但只有旧的... coinbase-exchange?所以它必须是新的(gdax)API或什么错误?但是没有例外被抛出?

When i remove all my test code and ONLY have the code related to the API, it works - BUT only with the old one...coinbase-exchange?? So it must be an error in there new (gdax)API or what? But no exception is thrown??

请在此处查看文档:

https://www.npmjs.com/package/coinbase-exchange#websocket-客户

https://www.npmjs.com/package/gdax#websocket-client

进一步说明DOC状态(这没有意义 - 当它只是旧的API仍然有效):

Further the DOC states (Which makes no sense - when it is only the old API which is the one still working):

GDAX转换
作为将Coinbase Exchange重新命名为GDAX的一部分,API端点已发生变化。旧的.exchange.coinbase.com端点将继续工作到2016年8月24日。对于新的端点,请参阅下面的文档。

GDAX transition As part of the rebranding of Coinbase Exchange to GDAX, API endpoints have changed. The old *.exchange.coinbase.com endpoints will continue working until Aug 24th, 2016. For the new endpoints see the documentation below.

推荐答案

使用setTimeout



你可以这样做 - 创建一个重新注册的超时:

Using setTimeout

You can do something like this - create a timeout that re-registers itself:

var t;
function nt() {
  t = setTimeout(nt, 10000);
}
nt();

每次都可以通过调用 clearTimeout 。例如,要使程序在2秒后退出,您可以添加:

Every time you can turn it off by calling clearTimeout. For example to make your program exit after 2 seconds you can add:

setTimeout(function () {
  clearTimeout(t);
}, 2000);

因此它甚至不必等待那些完整的10秒(或任何间隔)完成。

So it doesn't even have to wait for those full 10 seconds (or whatever is the interval) to finish.

使用 setInterval <可以使它更简单/ code>:

You can make it even simpler with setInterval:

var i = setInterval(()=>{}, 10000);

你可以用 clearInterval 取消它:

setTimeout(function () {
  clearInterval(i);
}, 2000);



真正的问题



但是如果您的流程退出,那么很有可能它会这样做,因为它不再等待任何事情发生,这才是真正的问题。因此,在这种情况下,保持Node退出可能无法解决您的问题 - 它只会等待没有任何事情发生

Real problem

But if your process exits then there's a good chance that it does so because it no longer waits for anything to happen and that's the real problem. So keeping Node from exiting may not fix your problem in that case - it will just wait for nothing to happen.

这篇关于阻止NodeJS退出事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆