在Angular中发布Pubnub

时间:2016-03-07 16:51:40

标签: javascript angularjs 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)
        });
    });

}]);

1 个答案:

答案 0 :(得分:4)

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

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

请告诉我它是否已解决您的问题。