编程语言
首页 > 编程语言> > javascript-Angular中的Pubnub聊天

javascript-Angular中的Pubnub聊天

作者:互联网

我正在AngularJS应用程序中实现pubnub聊天.我正在追踪tutorial

问题是,如果我从头开始创建新的AngularJS应用程序,则聊天有效,但是如果我在现有应用程序中实现代码,则会出现此错误:

Missing Callback                                                   pubnub.min.js:1 

而且我看不到我写的消息和应该收到的消息,但是我可以发送它们,并且可以在聊天的另一端看到这些消息.
你知道我该怎么解决吗?

编辑:这是pubnub聊天的控制器:

'use strict';

angular.module('myApp.appointments', ['ngRoute'])

.config(['$routeProvider', function($routeProvider) {
}])

.controller('appointmentsCtrl', ["$rootScope", "$scope", "$http", "$timeout", "$cookies", "URL", "Pubnub", function($rootScope, $scope, $http, $timeout, $cookies, URL, Pubnub) {

    $scope.sortType = 'name';
    $scope.sortReverse = false;

    $scope.sortType_s = 'time';

    $scope.filterAppointments = false;
    $scope.filterDate = '';

    $scope.highlightRow = '';

    $scope.$on('$routeChangeSuccess', function() {
        var data = {
            "token":$cookies.get('userToken'),
            "hairdresser_id": $cookies.get('userId')
        };

        $http.post(URL.url + 'appointments', data).then(function(res){
            $scope.app_list = res.data.appointments; 
            $scope.service_list = res.data.appointment_services; 
            $scope.customers_list = res.data.customers;
            $scope.pending_number = res.data.pending;
        });

        data = {
            "token":$cookies.get('userToken'),
            "hairdresser_id": $cookies.get('userId')
        };

        $http.post(URL.url + 'monthly_earnings', data).then(function(res){
            $rootScope.monthly_earnings = res.data.amount;
        });
    });

    // Pubnub implementation
    $scope.channel = "messages-channel";
    $scope.uuid =  _.random(100).toString();
    Pubnub.init({
        publish_key: MY_KEY,
        subscribe_key: SUB_KEY,
        ssl: true,
        uuid: $scope.uuid
    });

    $scope.sendMessage = function() {
        if (!$scope.messageContent  || $scope.messageContent === '') {
            return;
        }
        Pubnub.publish({
            channel: $scope.channel,
            message: {
                content: $scope.messageContent,
                sender_uuid: $scope.uuid,
                date: new Date()
            },
            callback: function(m) {
                 console.log(m);
            }
        });

        $scope.messageContent = '';
    }

    $scope.messages = [];

    Pubnub.subscribe({
        channel: $scope.channel,
        triggerEvent: ['callback']
    });

    $scope.$on(Pubnub.getMessageEventNameFor($scope.channel), function(ngEvent, m) {
        $scope.apply(function() {
            $scope.messages.push(m)
        });
    });

}]);

解决方法:

您在Pubub.subscribe函数的triggerEvents语句末尾忘记了s:

Pubnub.subscribe({
    channel: $scope.channel,
    triggerEvents: ['callback']
});

让我知道它是否解决了您的问题.

标签:pubnub,angularjs,javascript
来源: https://codeday.me/bug/20191027/1943096.html