代码之家  ›  专栏  ›  技术社区  ›  Pankesh Patel

节点。js代码未在不提示任何错误的情况下执行

  •  0
  • Pankesh Patel  · 技术社区  · 7 年前

    我正在尝试运行以下节点。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'] 
        });
    };
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   LMokrane    7 年前

    您已经编写了函数publish(),但忘记执行它。你只需要添加 publish(); 在代码末尾。