我不是付费的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);
});
}
答案 0 :(得分:0)
默认情况下,不会直接启用或支持历史记录。您需要在帐户中启用历史记录。启用历史记录之前发布的消息将不会保存。仅保存启用历史记录后发布的消息。接下来在您的应用中,您需要添加历史记录方法。
访问https://admin.pubnub.com/并启用 "Storage and Playback"
功能。
最初在此示例中尚未实现历史记录。您可以通过简单的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)