【问题标题】:Pubnub.subscribe not workingPubnub.subscribe 不工作
【发布时间】:2018-02-05 10:43:44
【问题描述】:

我正在尝试使用pubnub 在我的AngularJs 1.4.5 网络应用程序中实现一个简单的聊天。

我正在按照pubnub tutorial 中给出的步骤进行操作。

$scope.channel = 'chat_for_trial';
$scope.uuid = 'user1';

Pubnub.init({
    publishKey: 'demo',
    subscribeKey: 'demo',
    uuid: $scope.uuid
});

// Send the messages over PubNub Network
$scope.messageContent = {message: ''};

$scope.sendMessage = function() {
    // $scope.messageContent = data;
    // Don't send an empty message 
    if (!$scope.messageContent.message || $scope.messageContent.message === '') {
         return;
     }
     Pubnub.publish({
         channel: $scope.channel,
         message: {
             content: $scope.messageContent.message,
             sender_uuid: $scope.uuid,
             date: new Date()
         },
         callback: function(m) {
             console.log(m);
         }
     });
     // Reset the messageContent input
     $scope.messageContent.message = '';

 };

 //get messages
 $scope.messageContent.messages = [];

// Subscribing to the ‘messages-channel’ and trigering the message callback
Pubnub.subscribe({
    channel: $scope.channel,
    triggerEvents: ['callback']
});

// Listening to the callbacks
$scope.$on(Pubnub.getMessageEventNameFor($scope.channel), function (ngEvent, m) {
    $scope.$apply(function () {
        console.log("i am here")
        $scope.messageContent.messages.push(m)
    });
});

我可以向频道chat_for_trial 发送消息,但是在查看pubnub console 中的占用情况时,未列出订阅的uuid

当我从控制台发送消息时,它不会显示在 Web 应用程序中。但是从 web 应用发送的数据可以在 pubnub 控制台中看到。

我正在与pubnub: 4.20.1pubnub-angular: 4.1.0angularjs: 1.4.5 合作

我想知道我在这里缺少什么。

【问题讨论】:

  • 您从开发控制台发送的消息与您的应用程序发送的消息的确切内容是什么?使用您的 Web 应用程序的另一个客户端可以接收从使用您的 Web 应用程序的另一个客户端发送的消息吗?您是否能够在浏览器网络选项卡中看到消息(订阅呼叫返回消息)?您确定您的subscribe 中的$scope.channel 与您要发布到的频道名称相同吗?
  • 您也应该考虑使用PubNub ChatEngine 来构建您的聊天应用程序。
  • @CraigConover 否,从一个客户端发送的消息在另一个客户端的网络选项卡中不可见。是的,$scope.channel 是一个固定值,只有用户 id 会根据登录的用户而有所不同。
  • 好的,所以如果您甚至没有收到应用程序“幕后”的消息,那么您的应用程序实际上并没有订阅频道。可能是 PubNub 或 addListener 代码超出范围的变量范围问题。我建议对此进行调试。如果您需要 PubNub 的进一步帮助,请向 PubNub 支持提交工单(包括您的子密钥、错误消息(如果有)、附加代码文件等)并参考此 SO 线程/链接,以便我们可以回发任何有价值的答案公开。
  • @CraigConover 感谢您的支持。我发现这个问题是由于教程中的sdk版本和我安装的sdk版本不匹配造成的。本教程适用于sdk v3,但当前可用版本为sdk v4。我已经用我的错误是什么以及如何修复我的问题更新了我的问题。

标签: javascript angularjs publish-subscribe pubnub


【解决方案1】:

问题是由于教程中的pubnub and pubnub-angular 版本与我的应用程序中使用bower 安装的版本不匹配。

本教程适用于sdk v3 和当前sdk 版本v4

请参阅 this link 以了解如何使用 pubnub sdk v4。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2018-12-22
    • 2016-04-19
    • 1970-01-01
    相关资源
    最近更新 更多