我试图通过命令提示符运行以下Node.js代码。但是,代码不会执行(它没有在命令提示符下显示任何错误)。
我也使用(pubnub
)安装了必要的npm install pubnub
包。但是,问题仍然存在。
我错过了什么吗?代码链接在https://www.pubnub.com/docs/nodejs-javascript/pubnub-javascript-sdk#initializing-the-client
var PubNub = require('pubnub')
function publish() {
pubnub = new PubNub({
publishKey : 'demo',
subscribeKey : 'demo'
})
function publishSampleMessage() {
console.log("Since we're publishing on subscribe connectEvent, we're sure we'll receive the following publish.");
var publishConfig = {
channel : "hello_world",
message : "Hello from PubNub Docs!"
}
pubnub.publish(publishConfig, function(status, response) {
console.log(status, response);
})
}
pubnub.addListener({
status: function(statusEvent) {
if (statusEvent.category === "PNConnectedCategory") {
publishSampleMessage();
}
},
message: function(message) {
console.log("New Message!!", message);
},
presence: function(presenceEvent) {
// handle presence
}
})
console.log("Subscribing..");
pubnub.subscribe({
channels: ['hello_world']
});
};
答案 0 :(得分:1)
您已编写函数publish()但您忘记执行它。您只需在代码末尾添加publish();
即可。