2014-09-12 69 views
2

我在一个node.js项目中使用pubnub。我可以初始化pubnub并发布一条消息,如:如何关闭pubnub连接?

Node.js的

var PUBNUB = require('pubnub'); 
var pubnub = PUBNUB.init({ 
    publish_key : 'demo', 
    subscribe_key : 'demo' 
}); 

var CHANNEL = 'test1234'; 

pubnub.subscribe({ 
    channel: CHANNEL, 
    message: function (message) { 
    console.log('Received message:', message); 

    // unsubscribe again 
    pubnub.unsubscribe({channel: CHANNEL}); 

    // PROBLEM: The node.js process does not exit because pubnub is still 
    //   active. How to close the pubnub connection so it's completely 
    //   gone from memory? 
    }, 
    connect: function() { 
    // send a test message 
    pubnub.publish({channel: CHANNEL, message: 'hello world!'}); 
    } 
}); 

但我怎么能关闭这个pubnub实例?这样它就从内存和事件循环中消失了,并且不会使node.js进程保持运行。

我在API文档中找不到任何东西,并尝试使用pubnub.close()pubnub.destroy()pubnub.disconnect(),但没有成功。

+0

Afaik您的代码段尚未建立连接,通常您会通过订阅频道并再次取消订阅来关闭它。请参阅示例:https://github.com/pubnub/javascript/blob/master/examples/dev-console/dev-console.html – m90 2014-09-12 11:00:46

+0

谢谢。我已经发布了一个完整的代码示例来更好地解释问题。重点是在node.js中,进程永远不会结束,因为pubnub在内存中仍然处于活动状态。 – 2014-09-12 11:20:15

+0

pubnub.unsubscribe()应该删除订阅过程。您可以通过[email protected]与我们联系,以便我们与您取得更好的解决方案(我们将在此处汇报我们的发现。) – Geremy 2014-09-12 18:21:35

回答

1

此问题已在较新版本的pubnub中解决(当前为v3.7.8)。