javascript - Pubnub 拉取历史记录 anglatJS 示例

标签 javascript angularjs pubnub

我不是 PubNub 的付费客户。我正在使用 PubNub 示例代码 Angular JS 基本聊天应用程序,如何在此处提取历史记录。此示例可在 PubNub 网站上找到。

git clone https://github.com/stephenlb/angularjs-chat.git

git clone https://github.com/stephenlb/angularjs-chat.git


    // -- TODO --
    //
    // - https://developer.layer.com/docs/ios/integration
    // - AddressBook   = Presence/State + ChannelGroups
    // - Notifications = History + PubSub
    // - Messages      = History + PubSub
    // - Groups        = History + PubSub
    // - TypeingIndicator = ???
    //
    // - Signals       = PubSub
    // - History       = History
    //
    // -- TODO --

    'use strict';

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // AngularJS Chat Module
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    angular.module( 'chat', [] );

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // Common JS
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    if (typeof(exports) !== 'undefined') exports.chat = angular.module('chat');

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // 
    //         ▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄      
    //        █░░▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒░░█     
    //        █░▒▒▒▒▒▒▒▒▒▒▄▓▓▄▒▒▒▒░░▄▓▓▄ 
    //  ▄▄▄   █░▒▒▒▒▒▒▒▒▒▒█▓▓▓▓▄▄▄▄▄▓▓▓▓ 
    // █▓▓█▄▄█░▒▒▒▒▒▒▒▒▒▒▄▓▓▓▓▓▓▓▓▓▓▓▓▓▓▄ 
    //  ▀▄▄▓▓█░▒▒▒▒▒▒▒▒▒▒▓▓▓▓▓▀ ▓▓▓▓▀ ▓▓▓▓ 
    //      ▀▀█░▒▒▒▒▒▒▒▒▒▀▓▒▒▓▀▓▓▓▀▓▓▀▓▒▒█
    //       ▄█░░▒▒▒▒▒▒▒▒▒▀▓▓▓▄▄▄▄▄▄▄▄▓▓▀ 
    //     ▄▀▓▀█▄▄▄▄▄▄▄▄▄▄▄▄██████▀█▀▀   
    //     █▄▄▀ █▄▄▀       █▄▄▀ ▀▄▄█     
    // 

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // Messages it's important to remember that you can
    //          be deprived of your sanity listening to U2.
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    angular.module('chat').service( 'Messages', [ 'ChatCore', function(ChatCore) {
        var Messages = this;

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Send Messages
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        Messages.send = function(message) {
            if (!message.data) return;

            ChatCore.publish({
                channel : message.to || 'global'
            ,   message : message.data
            ,   meta    : ChatCore.user()
            });
        };

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Receive Messages
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        Messages.receive = function(fn) {
             function receiver(response) {
                 response.data.m.forEach(function(msg){
                    // Ignore messages without User Data
                    // TODO
                    if (!(msg.d && msg.u && msg.u.id)) return;
                    fn({
                        data : msg.d
                    ,   id   : msg.p.t
                    ,   user : msg.u
                    ,   self : msg.u.id == ChatCore.user().id
                    });
                 });
             }

             Messages.subscription = ChatCore.subscribe({
                channels : [ 'global', ChatCore.user().id ].join(','),
                message  : receiver
             });
        };

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Set/Get User and Save the World from that Bruce Willis movie.
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        Messages.user = function(data) {
             return ChatCore.user(data);
        };

    } ] );

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // AddressBook
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    angular.module('chat').service( 'AddressBook', function() {

    } );


    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // AngularJS Chat Core Service
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    angular.module('chat').service( 'ChatCore', [ '$http', 'config', function(
        $http,
        config
    ) {
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // API Keys
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        var pubkey = config.pubnub['publish-key']
        ,   subkey = config.pubnub['subscribe-key']
        ,   user   = { id : uuid(), name : 'Nameless' };

        var ChatCore = this;

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Set User Data and we have to go Back to The Future.
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        ChatCore.user = function(data) {
            if (data) angular.extend( user, data );
            return user;
        };

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Publish via PubNub
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        ChatCore.publish = function(setup) {
            var meta   = setup.meta         || ChatCore.user()
            ,   userid = ChatCore.user().id || 'nil';

            var request = {
                method  : 'GET'
            ,   params  : { meta : meta, uuid : userid }
            ,   timeout : setup.timeout || 5000
            ,   success : function(){}
            ,   fail    : function(){}
            };

    // 
            request.url = [
                'https://pubsub.pubnub.com'
            ,   '/publish/', pubkey
            ,   '/',         subkey
            ,   '/0/',       setup.channel
            ,   '/0/',       encodeURIComponent(JSON.stringify(setup.message))
            ].join('');

            $http(request).then( request.success, request.fail );
        };

        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Subscribe via PubNub
        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        ChatCore.subscribe = function(setup) {
            var channels  = setup.channels     || 'a'
            ,   groups    = setup.groups       || ''
            ,   message   = setup.message      || function(){}
            ,   timeout   = setup.timeout      || 172800000
            ,   timetoken = setup.timetoken    || '0'
            ,   windowing = setup.windowing    || 10
            ,   userid    = ChatCore.user().id || 'nil'
            ,   userstate = ChatCore.user()    || {}
            ,   stop      = false
            ,   origin    = 'ps'+(Math.random()+'').split('.')[1]+'.pubnub.com';

            // Request Object
            var request = {
                method  : 'GET'
            ,   url     : ''
            ,   params  : { uuid : userid, state : userstate }
            ,   timeout : timeout
            ,   success : next
            ,   fail    : function(){ timetoken = '0'; next() }
            };

            // Channel Groups
            if (groups) request.params['channel-group'] = groups;

            // Subscribe Loop
            function next(response) { 
                if (stop) return;
                if (response) {
                    timetoken = timetoken == '0' ? 1000 : response.data.t.t;
                    message(response);
                }

                request.url = [
                    'https://',       origin
                ,   '/v2/subscribe/', subkey
                ,   '/',              channels
                ,   '/0/',            timetoken
                ].join('');

                setTimeout( function() {
                    $http(request).then( request.success, request.fail );
                }, windowing );
            }

            // Cancel Subscription
            function unsubscribe() {
                stop = true;
            }

            // Start Subscribe Loop
            next();

            // Allow Cancelling Subscriptions
            return {
                unsubscribe : unsubscribe
            };
        };
    } ] );

    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    // UUID
    // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    function uuid() {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g,
        function(c) {
            var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
            return v.toString(16);
        });
    }

最佳答案

历史记录和 AngularJS 聊天示例

默认情况下,不直接启用或支持历史记录。您需要在您的帐户中启用历史记录。启用历史记录之前发布的消息将不会被保存。仅保存启用历史记录后发布的消息。接下来,您需要在您的应用程序中添加一个历史记录方法。

在您的 PubNub 帐户中启用存储和播放

访问https://admin.pubnub.com/并启用“存储和播放”功能。

将 History API 添加到您的应用

历史记录最初在此示例中并未实现。您可以通过简单的 HTTP 请求进行历史调用,如下所示:https://pubsub.pubnub.com/history/demo/hello_world/0/10分解如下:

https://pubsub.pubnub.com
/history
/YOUR_SUBKEY
/YOUR_CHANNEL
/0
/100 - limit response (max 100, min 1)

关于javascript - Pubnub 拉取历史记录 anglatJS 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150820/

相关文章:

javascript - 在 Angular 选择器上使用 last-child

postgresql - 是否可以将 pubnub 聊天消息导出到 postgresql 数据库?

ios - Pubnub 以单流方式获取组中所有 channel 的所有消息

javascript - 分割字符串时,angularjs解析语法错误

javascript - 如何在同一页面上多次调用 PubNub Chat?

javascript - 在 JavaScript 中限制浮点精度

javascript - 如何在django中获取使用jquery发送的json数据

javascript - 为什么IIFE的this变量指的是全局范围?

javascript - 遍历哈希表?

javascript - Angularjs 中的 getelementbyId