centrifuge
Advanced tools
Comparing version 0.5.2 to 0.7.0
{ | ||
"name": "centrifuge", | ||
"version": "0.5.2", | ||
"version": "0.7.0", | ||
"ignore": [ | ||
"node_modules" | ||
"node_modules", | ||
"**/.*", | ||
"bower_components", | ||
"test", | ||
"tests" | ||
], | ||
"dependencies": {} | ||
} | ||
"main": "centrifuge.js", | ||
"authors": [ | ||
"FZambia <frvzmb@gmail.com>" | ||
], | ||
"description": "Centrifuge javascript client", | ||
"keywords": [ | ||
"sockjs", | ||
"websocket" | ||
], | ||
"license": "MIT", | ||
"homepage": "https://github.com/centrifugal/centrifuge-js" | ||
} |
@@ -1,5 +0,2 @@ | ||
/** | ||
* Centrifuge javascript client | ||
* v0.5.2 | ||
*/ | ||
// v0.7.0 | ||
;(function () { | ||
@@ -567,2 +564,90 @@ 'use strict'; | ||
// http://krasimirtsonev.com/blog/article/Cross-browser-handling-of-Ajax-requests-in-absurdjs | ||
var AJAX = { | ||
request: function(url, method, ops) { | ||
ops.data = ops.data || {}; | ||
var getParams = function(data, request_url) { | ||
var arr = [], str; | ||
for(var name in data) { | ||
if (typeof data[name] === 'object') { | ||
for (var i in data[name]) { | ||
arr.push(name + '=' + encodeURIComponent(data[name][i])); | ||
} | ||
} else { | ||
arr.push(name + '=' + encodeURIComponent(data[name])); | ||
} | ||
} | ||
str = arr.join('&'); | ||
if(str != '') { | ||
return request_url ? (request_url.indexOf('?') < 0 ? '?' + str : '&' + str) : str; | ||
} | ||
return ''; | ||
}; | ||
var api = { | ||
host: {}, | ||
setHeaders: function(headers) { | ||
for(var name in headers) { | ||
this.xhr && this.xhr.setRequestHeader(name, headers[name]); | ||
} | ||
}, | ||
process: function(ops) { | ||
var self = this; | ||
this.xhr = null; | ||
if (window.ActiveXObject) { | ||
this.xhr = new ActiveXObject('Microsoft.XMLHTTP'); | ||
} | ||
else if (window.XMLHttpRequest) { | ||
this.xhr = new XMLHttpRequest(); | ||
} | ||
if(this.xhr) { | ||
this.xhr.onreadystatechange = function() { | ||
if(self.xhr.readyState == 4 && self.xhr.status == 200) { | ||
var result = self.xhr.responseText; | ||
if (typeof JSON != 'undefined') { | ||
result = JSON.parse(result); | ||
} else { | ||
throw "JSON undefined"; | ||
} | ||
self.doneCallback && self.doneCallback.apply(self.host, [result, self.xhr]); | ||
} else if(self.xhr.readyState == 4) { | ||
self.failCallback && self.failCallback.apply(self.host, [self.xhr]); | ||
} | ||
self.alwaysCallback && self.alwaysCallback.apply(self.host, [self.xhr]); | ||
} | ||
} | ||
if(method == 'get') { | ||
this.xhr.open("GET", url + getParams(ops.data, url), true); | ||
} else { | ||
this.xhr.open(method, url, true); | ||
this.setHeaders({ | ||
'X-Requested-With': 'XMLHttpRequest', | ||
'Content-type': 'application/x-www-form-urlencoded' | ||
}); | ||
} | ||
if(ops.headers && typeof ops.headers == 'object') { | ||
this.setHeaders(ops.headers); | ||
} | ||
setTimeout(function() { | ||
method == 'get' ? self.xhr.send() : self.xhr.send(getParams(ops.data)); | ||
}, 20); | ||
return this; | ||
}, | ||
done: function(callback) { | ||
this.doneCallback = callback; | ||
return this; | ||
}, | ||
fail: function(callback) { | ||
this.failCallback = callback; | ||
return this; | ||
}, | ||
always: function(callback) { | ||
this.alwaysCallback = callback; | ||
return this; | ||
} | ||
}; | ||
return api.process(ops); | ||
} | ||
}; | ||
function endsWith(value, suffix) { | ||
@@ -572,2 +657,6 @@ return value.indexOf(suffix, value.length - suffix.length) !== -1; | ||
function startsWith(value, prefix) { | ||
return value.lastIndexOf(prefix, 0) === 0; | ||
} | ||
function stripSlash(value) { | ||
@@ -613,2 +702,5 @@ if (value.substring(value.length - 1) == "/") { | ||
this._isBatching = false; | ||
this._isAuthBatching = false; | ||
this._authChannels = {}; | ||
this._refreshTimeout = null; | ||
this._config = { | ||
@@ -618,2 +710,3 @@ retry: 3000, | ||
debug: false, | ||
insecure: false, | ||
server: null, | ||
@@ -630,3 +723,8 @@ protocols_whitelist: [ | ||
'jsonp-polling' | ||
] | ||
], | ||
privateChannelPrefix: "$", | ||
refreshEndpoint: "/centrifuge/refresh", | ||
authEndpoint: "/centrifuge/auth", | ||
authHeaders: {}, | ||
refreshHeaders: {} | ||
}; | ||
@@ -640,5 +738,5 @@ if (options) { | ||
var centrifuge_proto = Centrifuge.prototype; | ||
var centrifugeProto = Centrifuge.prototype; | ||
centrifuge_proto._debug = function () { | ||
centrifugeProto._debug = function () { | ||
if (this._config.debug === true) { | ||
@@ -649,3 +747,3 @@ log('debug', arguments); | ||
centrifuge_proto._configure = function (configuration) { | ||
centrifugeProto._configure = function (configuration) { | ||
this._debug('Configuring centrifuge object with', configuration); | ||
@@ -663,6 +761,2 @@ | ||
if (!this._config.token) { | ||
throw 'Missing required configuration parameter \'token\' specifying the sign of authorization request'; | ||
} | ||
if (!this._config.project) { | ||
@@ -673,9 +767,26 @@ throw 'Missing required configuration parameter \'project\' specifying project ID in Centrifuge'; | ||
if (!this._config.user && this._config.user !== '') { | ||
throw 'Missing required configuration parameter \'user\' specifying user\'s unique ID in your application'; | ||
if (!this._config.insecure) { | ||
throw 'Missing required configuration parameter \'user\' specifying user\'s unique ID in your application'; | ||
} else { | ||
this._debug("user not found but this is OK for insecure mode - anonymous access will be used"); | ||
this._config.user = ""; | ||
} | ||
} | ||
if (!this._config.timestamp) { | ||
throw 'Missing required configuration parameter \'timestamp\''; | ||
if (!this._config.insecure) { | ||
throw 'Missing required configuration parameter \'timestamp\''; | ||
} else { | ||
this._debug("token not found but this is OK for insecure mode"); | ||
} | ||
} | ||
if (!this._config.token) { | ||
if (!this._config.insecure) { | ||
throw 'Missing required configuration parameter \'token\' specifying the sign of authorization request'; | ||
} else { | ||
this._debug("timestamp not found but this is OK for insecure mode"); | ||
} | ||
} | ||
this._config.url = stripSlash(this._config.url); | ||
@@ -692,3 +803,3 @@ | ||
centrifuge_proto._setStatus = function (newStatus) { | ||
centrifugeProto._setStatus = function (newStatus) { | ||
if (this._status !== newStatus) { | ||
@@ -700,19 +811,23 @@ this._debug('Status', this._status, '->', newStatus); | ||
centrifuge_proto._isDisconnected = function () { | ||
centrifugeProto._isDisconnected = function () { | ||
return this._isConnected() === false; | ||
}; | ||
centrifuge_proto._isConnected = function () { | ||
centrifugeProto._isConnected = function () { | ||
return this._status === 'connected'; | ||
}; | ||
centrifuge_proto._nextMessageId = function () { | ||
centrifugeProto._isConnecting = function () { | ||
return this._status === 'connecting'; | ||
}; | ||
centrifugeProto._nextMessageId = function () { | ||
return ++this._messageId; | ||
}; | ||
centrifuge_proto._clearSubscriptions = function () { | ||
centrifugeProto._clearSubscriptions = function () { | ||
this._subscriptions = {}; | ||
}; | ||
centrifuge_proto._send = function (messages) { | ||
centrifugeProto._send = function (messages) { | ||
// We must be sure that the messages have a clientId. | ||
@@ -735,4 +850,8 @@ // This is not guaranteed since the handshake may take time to return | ||
centrifuge_proto._connect = function (callback) { | ||
centrifugeProto._connect = function (callback) { | ||
if (this.isConnected()) { | ||
return; | ||
} | ||
this._clientId = null; | ||
@@ -774,6 +893,4 @@ | ||
'params': { | ||
'token': self._config.token, | ||
'user': self._config.user, | ||
'project': self._config.project, | ||
'timestamp': self._config.timestamp | ||
'project': self._config.project | ||
} | ||
@@ -783,7 +900,9 @@ }; | ||
if (self._config.info !== null) { | ||
self._debug("connect using additional info"); | ||
centrifugeMessage['params']['info'] = self._config.info; | ||
} else { | ||
self._debug("connect without additional info"); | ||
centrifugeMessage["params"]["info"] = self._config.info; | ||
} | ||
if (!self._config.insecure) { | ||
centrifugeMessage["params"]["timestamp"] = self._config.timestamp; | ||
centrifugeMessage["params"]["token"] = self._config.token; | ||
} | ||
self.send(centrifugeMessage); | ||
@@ -816,3 +935,3 @@ }; | ||
centrifuge_proto._disconnect = function () { | ||
centrifugeProto._disconnect = function () { | ||
this._clientId = null; | ||
@@ -825,3 +944,3 @@ this._setStatus('disconnected'); | ||
centrifuge_proto._getSubscription = function (channel) { | ||
centrifugeProto._getSubscription = function (channel) { | ||
var subscription; | ||
@@ -835,3 +954,3 @@ subscription = this._subscriptions[channel]; | ||
centrifuge_proto._removeSubscription = function (channel) { | ||
centrifugeProto._removeSubscription = function (channel) { | ||
try { | ||
@@ -842,9 +961,34 @@ delete this._subscriptions[channel]; | ||
} | ||
try { | ||
delete this._authChannels[channel]; | ||
} catch (e) { | ||
this._debug('nothing to delete from authChannels for channel ', channel); | ||
} | ||
}; | ||
centrifuge_proto._connectResponse = function (message) { | ||
centrifugeProto._connectResponse = function (message) { | ||
if (this.isConnected()) { | ||
return; | ||
} | ||
if (message.error === null) { | ||
this._clientId = message.body; | ||
if (!message.body) { | ||
return; | ||
} | ||
var isExpired = message.body.expired; | ||
if (isExpired) { | ||
this.refresh(); | ||
return; | ||
} | ||
this._clientId = message.body.client; | ||
this._setStatus('connected'); | ||
this.trigger('connect', [message]); | ||
if (this._refreshTimeout) { | ||
window.clearTimeout(this._refreshTimeout); | ||
} | ||
if (message.body.ttl !== null) { | ||
var self = this; | ||
this._refreshTimeout = window.setTimeout(function() { | ||
self.refresh.call(self); | ||
}, message.body.ttl * 1000); | ||
} | ||
} else { | ||
@@ -856,7 +1000,5 @@ this.trigger('error', [message]); | ||
centrifuge_proto._disconnectResponse = function (message) { | ||
centrifugeProto._disconnectResponse = function (message) { | ||
if (message.error === null) { | ||
this.disconnect(); | ||
//this.trigger('disconnect', [message]); | ||
//this.trigger('disconnect:success', [message]); | ||
} else { | ||
@@ -868,3 +1010,3 @@ this.trigger('error', [message]); | ||
centrifuge_proto._subscribeResponse = function (message) { | ||
centrifugeProto._subscribeResponse = function (message) { | ||
if (message.error !== null) { | ||
@@ -891,3 +1033,3 @@ this.trigger('error', [message]); | ||
centrifuge_proto._unsubscribeResponse = function (message) { | ||
centrifugeProto._unsubscribeResponse = function (message) { | ||
var body = message.body; | ||
@@ -905,3 +1047,3 @@ var channel = body.channel; | ||
centrifuge_proto._publishResponse = function (message) { | ||
centrifugeProto._publishResponse = function (message) { | ||
var body = message.body; | ||
@@ -921,3 +1063,3 @@ var channel = body.channel; | ||
centrifuge_proto._presenceResponse = function (message) { | ||
centrifugeProto._presenceResponse = function (message) { | ||
var body = message.body; | ||
@@ -938,3 +1080,3 @@ var channel = body.channel; | ||
centrifuge_proto._historyResponse = function (message) { | ||
centrifugeProto._historyResponse = function (message) { | ||
var body = message.body; | ||
@@ -955,3 +1097,3 @@ var channel = body.channel; | ||
centrifuge_proto._joinResponse = function(message) { | ||
centrifugeProto._joinResponse = function(message) { | ||
var body = message.body; | ||
@@ -966,3 +1108,3 @@ var channel = body.channel; | ||
centrifuge_proto._leaveResponse = function(message) { | ||
centrifugeProto._leaveResponse = function(message) { | ||
var body = message.body; | ||
@@ -977,3 +1119,3 @@ var channel = body.channel; | ||
centrifuge_proto._messageResponse = function (message) { | ||
centrifugeProto._messageResponse = function (message) { | ||
var body = message.body; | ||
@@ -988,3 +1130,15 @@ var channel = body.channel; | ||
centrifuge_proto._dispatchMessage = function(message) { | ||
centrifugeProto._refreshResponse = function (message) { | ||
if (this._refreshTimeout) { | ||
window.clearTimeout(this._refreshTimeout); | ||
} | ||
if (message.body.ttl !== null) { | ||
var self = this; | ||
self._refreshTimeout = window.setTimeout(function () { | ||
self.refresh.call(self); | ||
}, message.body.ttl * 1000); | ||
} | ||
}; | ||
centrifugeProto._dispatchMessage = function(message) { | ||
if (message === undefined || message === null) { | ||
@@ -1030,2 +1184,5 @@ return; | ||
break; | ||
case 'refresh': | ||
this._refreshResponse(message); | ||
break; | ||
case 'message': | ||
@@ -1039,3 +1196,3 @@ this._messageResponse(message); | ||
centrifuge_proto._receive = function (data) { | ||
centrifugeProto._receive = function (data) { | ||
if (Object.prototype.toString.call(data) === Object.prototype.toString.call([])) { | ||
@@ -1053,3 +1210,3 @@ for (var i in data) { | ||
centrifuge_proto._flush = function() { | ||
centrifugeProto._flush = function() { | ||
var messages = this._messages.slice(0); | ||
@@ -1060,3 +1217,3 @@ this._messages = []; | ||
centrifuge_proto._ping = function () { | ||
centrifugeProto._ping = function () { | ||
var centrifugeMessage = { | ||
@@ -1071,23 +1228,25 @@ "method": "ping", | ||
centrifuge_proto.getClientId = function () { | ||
centrifugeProto.getClientId = function () { | ||
return this._clientId; | ||
}; | ||
centrifuge_proto.isConnected = centrifuge_proto._isConnected; | ||
centrifugeProto.isConnected = centrifugeProto._isConnected; | ||
centrifuge_proto.isDisconnected = centrifuge_proto._isDisconnected; | ||
centrifugeProto.isConnecting = centrifugeProto._isConnecting; | ||
centrifuge_proto.configure = function (configuration) { | ||
centrifugeProto.isDisconnected = centrifugeProto._isDisconnected; | ||
centrifugeProto.configure = function (configuration) { | ||
this._configure.call(this, configuration); | ||
}; | ||
centrifuge_proto.connect = centrifuge_proto._connect; | ||
centrifugeProto.connect = centrifugeProto._connect; | ||
centrifuge_proto.disconnect = centrifuge_proto._disconnect; | ||
centrifugeProto.disconnect = centrifugeProto._disconnect; | ||
centrifuge_proto.getSubscription = centrifuge_proto._getSubscription; | ||
centrifugeProto.getSubscription = centrifugeProto._getSubscription; | ||
centrifuge_proto.ping = centrifuge_proto._ping; | ||
centrifugeProto.ping = centrifugeProto._ping; | ||
centrifuge_proto.send = function (message) { | ||
centrifugeProto.send = function (message) { | ||
if (this._isBatching === true) { | ||
@@ -1100,3 +1259,3 @@ this._messages.push(message); | ||
centrifuge_proto.startBatching = function () { | ||
centrifugeProto.startBatching = function () { | ||
// start collecting messages without sending them to Centrifuge until flush | ||
@@ -1107,3 +1266,3 @@ // method called | ||
centrifuge_proto.stopBatching = function(flush) { | ||
centrifugeProto.stopBatching = function(flush) { | ||
// stop collecting messages | ||
@@ -1117,8 +1276,93 @@ flush = flush || false; | ||
centrifuge_proto.flush = function() { | ||
centrifugeProto.flush = function() { | ||
// send batched messages to Centrifuge | ||
this._flush(); | ||
}; | ||
centrifuge_proto.subscribe = function (channel, callback) { | ||
centrifugeProto.startAuthBatching = function() { | ||
// start collecting private channels to create bulk authentication | ||
// request to authEndpoint when stopAuthBatching will be called | ||
this._isAuthBatching = true; | ||
}; | ||
centrifugeProto.stopAuthBatching = function(callback) { | ||
// create request to authEndpoint with collected private channels | ||
// to ask if this client can subscribe on each channel | ||
this._isAuthBatching = false; | ||
var authChannels = this._authChannels; | ||
this._authChannels = {}; | ||
var channels = []; | ||
for (var channel in authChannels) { | ||
var subscription = this.getSubscription(channel); | ||
if (!subscription) { | ||
continue; | ||
} | ||
channels.push(channel); | ||
} | ||
if (channels.length == 0) { | ||
if (callback) { | ||
callback(); | ||
} | ||
return; | ||
} | ||
var data = { | ||
"client": this.getClientId(), | ||
"channels": channels | ||
}; | ||
var self = this; | ||
AJAX.request(this._config.authEndpoint, "post", { | ||
"headers": this._config.authHeaders, | ||
"data": data | ||
}).done(function(data) { | ||
for (var i in channels) { | ||
var channel = channels[i]; | ||
var channelResponse = data[channel]; | ||
if (!channelResponse) { | ||
// subscription:error | ||
self._subscribeResponse({ | ||
"error": 404, | ||
"body": { | ||
"channel": channel | ||
} | ||
}); | ||
continue; | ||
} | ||
if (!channelResponse.status || channelResponse.status === 200) { | ||
var centrifugeMessage = { | ||
"method": "subscribe", | ||
"params": { | ||
"channel": channel, | ||
"client": self.getClientId(), | ||
"info": channelResponse.info, | ||
"sign": channelResponse.sign | ||
} | ||
}; | ||
self.send(centrifugeMessage); | ||
} else { | ||
self._subscribeResponse({ | ||
"error": channelResponse.status, | ||
"body": { | ||
"channel": channel | ||
} | ||
}); | ||
} | ||
} | ||
}).fail(function() { | ||
log("info", "authorization request failed"); | ||
return false; | ||
}).always(function(){ | ||
if (callback) { | ||
callback(); | ||
} | ||
}) | ||
}; | ||
centrifugeProto.subscribe = function (channel, callback) { | ||
if (arguments.length < 1) { | ||
@@ -1146,3 +1390,3 @@ throw 'Illegal arguments number: required 1, got ' + arguments.length; | ||
centrifuge_proto.unsubscribe = function (channel) { | ||
centrifugeProto.unsubscribe = function (channel) { | ||
if (arguments.length < 1) { | ||
@@ -1164,3 +1408,3 @@ throw 'Illegal arguments number: required 1, got ' + arguments.length; | ||
centrifuge_proto.publish = function (channel, data, callback) { | ||
centrifugeProto.publish = function (channel, data, callback) { | ||
var subscription = this.getSubscription(channel); | ||
@@ -1175,3 +1419,3 @@ if (subscription === null) { | ||
centrifuge_proto.presence = function (channel, callback) { | ||
centrifugeProto.presence = function (channel, callback) { | ||
var subscription = this.getSubscription(channel); | ||
@@ -1186,3 +1430,3 @@ if (subscription === null) { | ||
centrifuge_proto.history = function (channel, callback) { | ||
centrifugeProto.history = function (channel, callback) { | ||
var subscription = this.getSubscription(channel); | ||
@@ -1197,2 +1441,45 @@ if (subscription === null) { | ||
centrifugeProto.refresh = function () { | ||
// ask web app for connection parameters - project ID, user ID, | ||
// timestamp, info and token | ||
var self = this; | ||
this._debug('refresh'); | ||
AJAX.request(this._config.refreshEndpoint, "post", { | ||
"headers": this._config.refreshHeaders, | ||
"data": {} | ||
}).done(function(data) { | ||
self._config.user = data.user; | ||
self._config.project = data.project; | ||
self._config.timestamp = data.timestamp; | ||
self._config.info = data.info; | ||
self._config.token = data.token; | ||
if (self._reconnect && self.isDisconnected()) { | ||
self.connect(); | ||
} else { | ||
var centrifugeMessage = { | ||
"method": "refresh", | ||
"params": { | ||
'user': self._config.user, | ||
'project': self._config.project, | ||
'timestamp': self._config.timestamp, | ||
'info': self._config.info, | ||
'token': self._config.token | ||
} | ||
}; | ||
self.send(centrifugeMessage); | ||
} | ||
}).fail(function(xhr){ | ||
// 403 or 500 - does not matter - if connection check activated then Centrifuge | ||
// will disconnect client eventually | ||
self._debug(xhr); | ||
self._debug("error getting connect parameters"); | ||
if (self._refreshTimeout) { | ||
window.clearTimeout(self._refreshTimeout); | ||
} | ||
self._refreshTimeout = window.setTimeout(function(){ | ||
self.refresh.call(self); | ||
}, 3000); | ||
}); | ||
}; | ||
function Subscription(centrifuge, channel) { | ||
@@ -1210,13 +1497,19 @@ /** | ||
var sub_proto = Subscription.prototype; | ||
var subscriptionProto = Subscription.prototype; | ||
sub_proto.getChannel = function () { | ||
subscriptionProto.getChannel = function () { | ||
return this.channel; | ||
}; | ||
sub_proto.getCentrifuge = function () { | ||
subscriptionProto.getCentrifuge = function () { | ||
return this._centrifuge; | ||
}; | ||
sub_proto.subscribe = function (callback) { | ||
subscriptionProto.subscribe = function (callback) { | ||
/* | ||
If channel name does not start with privateChannelPrefix - then we | ||
can just send subscription message to Centrifuge. If channel name | ||
starts with privateChannelPrefix - then this is a private channel | ||
and we should ask web application backend for permission first. | ||
*/ | ||
var centrifugeMessage = { | ||
@@ -1228,3 +1521,16 @@ "method": "subscribe", | ||
}; | ||
this._centrifuge.send(centrifugeMessage); | ||
if (startsWith(this.channel, this._centrifuge._config.privateChannelPrefix)) { | ||
// private channel | ||
if (this._centrifuge._isAuthBatching) { | ||
this._centrifuge._authChannels[this.channel] = true; | ||
} else { | ||
this._centrifuge.startAuthBatching(); | ||
this.subscribe(callback); | ||
this._centrifuge.stopAuthBatching(); | ||
} | ||
} else { | ||
this._centrifuge.send(centrifugeMessage); | ||
} | ||
if (callback) { | ||
@@ -1235,3 +1541,3 @@ this.on('message', callback); | ||
sub_proto.unsubscribe = function () { | ||
subscriptionProto.unsubscribe = function () { | ||
this._centrifuge._removeSubscription(this.channel); | ||
@@ -1247,3 +1553,3 @@ var centrifugeMessage = { | ||
sub_proto.publish = function (data, callback) { | ||
subscriptionProto.publish = function (data, callback) { | ||
var centrifugeMessage = { | ||
@@ -1262,3 +1568,3 @@ "method": "publish", | ||
sub_proto.presence = function (callback) { | ||
subscriptionProto.presence = function (callback) { | ||
var centrifugeMessage = { | ||
@@ -1276,3 +1582,3 @@ "method": "presence", | ||
sub_proto.history = function (callback) { | ||
subscriptionProto.history = function (callback) { | ||
var centrifugeMessage = { | ||
@@ -1279,0 +1585,0 @@ "method": "history", |
@@ -1,1 +0,1 @@ | ||
(function(){"use strict";function e(e,t){return e.prototype=Object.create(t.prototype),e.prototype.constructor=e,t.prototype}function t(){}function n(e,t){for(var n=e.length;n--;)if(e[n].listener===t)return n;return-1}function i(e){return function(){return this[e].apply(this,arguments)}}function r(e,t){for(var n=t||{},i=2;i<arguments.length;++i){var o=arguments[i];if(void 0!==o&&null!==o)for(var c in o){var u=s(o,c),h=s(n,c);if(u!==t&&void 0!==u)if(e&&"object"==typeof u&&null!==u)if(u instanceof Array)n[c]=r(e,h instanceof Array?h:[],u);else{var a="object"!=typeof h||h instanceof Array?{}:h;n[c]=r(e,a,u)}else n[c]=u}}return n}function s(e,t){try{return e[t]}catch(n){return void 0}}function o(e,t){return-1!==e.indexOf(t,e.length-t.length)}function c(e){return"/"==e.substring(e.length-1)&&(e=e.substring(0,e.length-1)),e}function u(e){return void 0===e||null===e?!1:"string"==typeof e||e instanceof String}function h(e){return void 0===e||null===e?!1:"function"==typeof e}function a(e,t){if(window.console){var n=window.console[e];h(n)&&n.apply(window.console,t)}}function l(e){this._sockjs=!1,this._status="disconnected",this._reconnect=!0,this._transport=null,this._messageId=0,this._clientId=null,this._subscriptions={},this._messages=[],this._isBatching=!1,this._config={retry:3e3,info:null,debug:!1,server:null,protocols_whitelist:["websocket","xdr-streaming","xhr-streaming","iframe-eventsource","iframe-htmlfile","xdr-polling","xhr-polling","iframe-xhr-polling","jsonp-polling"]},e&&this.configure(e)}function g(e,t){this._centrifuge=e,this.channel=t}Object.create||(Object.create=function(){function e(){}return function(t){if(1!=arguments.length)throw new Error("Object.create implementation only accepts one parameter.");return e.prototype=t,new e}}()),Array.prototype.indexOf||(Array.prototype.indexOf=function(e){if(null==this)throw new TypeError;var t,n,i=Object(this),r=i.length>>>0;if(0===r)return-1;if(t=0,arguments.length>1&&(t=Number(arguments[1]),t!=t?t=0:0!=t&&1/0!=t&&t!=-1/0&&(t=(t>0||-1)*Math.floor(Math.abs(t)))),t>=r)return-1;for(n=t>=0?t:Math.max(r-Math.abs(t),0);r>n;n++)if(n in i&&i[n]===e)return n;return-1});var f=t.prototype;f.getListeners=function(e){var t,n,i=this._getEvents();if("object"==typeof e){t={};for(n in i)i.hasOwnProperty(n)&&e.test(n)&&(t[n]=i[n])}else t=i[e]||(i[e]=[]);return t},f.flattenListeners=function(e){var t,n=[];for(t=0;t<e.length;t+=1)n.push(e[t].listener);return n},f.getListenersAsObject=function(e){var t,n=this.getListeners(e);return n instanceof Array&&(t={},t[e]=n),t||n},f.addListener=function(e,t){var i,r=this.getListenersAsObject(e),s="object"==typeof t;for(i in r)r.hasOwnProperty(i)&&-1===n(r[i],t)&&r[i].push(s?t:{listener:t,once:!1});return this},f.on=i("addListener"),f.addOnceListener=function(e,t){return this.addListener(e,{listener:t,once:!0})},f.once=i("addOnceListener"),f.defineEvent=function(e){return this.getListeners(e),this},f.defineEvents=function(e){for(var t=0;t<e.length;t+=1)this.defineEvent(e[t]);return this},f.removeListener=function(e,t){var i,r,s=this.getListenersAsObject(e);for(r in s)s.hasOwnProperty(r)&&(i=n(s[r],t),-1!==i&&s[r].splice(i,1));return this},f.off=i("removeListener"),f.addListeners=function(e,t){return this.manipulateListeners(!1,e,t)},f.removeListeners=function(e,t){return this.manipulateListeners(!0,e,t)},f.manipulateListeners=function(e,t,n){var i,r,s=e?this.removeListener:this.addListener,o=e?this.removeListeners:this.addListeners;if("object"!=typeof t||t instanceof RegExp)for(i=n.length;i--;)s.call(this,t,n[i]);else for(i in t)t.hasOwnProperty(i)&&(r=t[i])&&("function"==typeof r?s.call(this,i,r):o.call(this,i,r));return this},f.removeEvent=function(e){var t,n=typeof e,i=this._getEvents();if("string"===n)delete i[e];else if("object"===n)for(t in i)i.hasOwnProperty(t)&&e.test(t)&&delete i[t];else delete this._events;return this},f.emitEvent=function(e,t){var n,i,r,s,o=this.getListenersAsObject(e);for(r in o)if(o.hasOwnProperty(r))for(i=o[r].length;i--;)n=o[r][i],n.once===!0&&this.removeListener(e,n.listener),s=n.listener.apply(this,t||[]),s===this._getOnceReturnValue()&&this.removeListener(e,n.listener);return this},f.trigger=i("emitEvent"),f.emit=function(e){var t=Array.prototype.slice.call(arguments,1);return this.emitEvent(e,t)},f.setOnceReturnValue=function(e){return this._onceReturnValue=e,this},f._getOnceReturnValue=function(){return this.hasOwnProperty("_onceReturnValue")?this._onceReturnValue:!0},f._getEvents=function(){return this._events||(this._events={})},e(l,t);var p=l.prototype;p._debug=function(){this._config.debug===!0&&a("debug",arguments)},p._configure=function(e){if(this._debug("Configuring centrifuge object with",e),e||(e={}),this._config=r(!1,this._config,e),!this._config.url)throw"Missing required configuration parameter 'url' specifying the Centrifuge server URL";if(!this._config.token)throw"Missing required configuration parameter 'token' specifying the sign of authorization request";if(!this._config.project)throw"Missing required configuration parameter 'project' specifying project ID in Centrifuge";if(!this._config.user&&""!==this._config.user)throw"Missing required configuration parameter 'user' specifying user's unique ID in your application";if(!this._config.timestamp)throw"Missing required configuration parameter 'timestamp'";if(this._config.url=c(this._config.url),o(this._config.url,"connection")){if("undefined"==typeof window.SockJS)throw"You need to include SockJS client library before Centrifuge javascript client library or use pure Websocket connection endpoint";this._sockjs=!0}},p._setStatus=function(e){this._status!==e&&(this._debug("Status",this._status,"->",e),this._status=e)},p._isDisconnected=function(){return this._isConnected()===!1},p._isConnected=function(){return"connected"===this._status},p._nextMessageId=function(){return++this._messageId},p._clearSubscriptions=function(){this._subscriptions={}},p._send=function(e){for(var t=0;t<e.length;++t){var n=e[t];n.uid=""+this._nextMessageId(),this._clientId&&(n.clientId=this._clientId),this._debug("Send",n),this._transport.send(JSON.stringify(n))}},p._connect=function(e){this._clientId=null,this._reconnect=!0,this._clearSubscriptions(),this._setStatus("connecting");var t=this;if(e&&this.on("connect",e),this._sockjs===!0){var n={protocols_whitelist:this._config.protocols_whitelist};null!==this._config.server&&(n.server=this._config.server),this._transport=new SockJS(this._config.url,null,n)}else this._transport=new WebSocket(this._config.url);this._setStatus("connecting"),this._transport.onopen=function(){var e={method:"connect",params:{token:t._config.token,user:t._config.user,project:t._config.project,timestamp:t._config.timestamp}};null!==t._config.info?(t._debug("connect using additional info"),e.params.info=t._config.info):t._debug("connect without additional info"),t.send(e)},this._transport.onerror=function(e){t._debug(e)},this._transport.onclose=function(){t._setStatus("disconnected"),t.trigger("disconnect"),t._reconnect===!0&&window.setTimeout(function(){t._reconnect===!0&&t._connect.call(t)},t._config.retry)},this._transport.onmessage=function(e){var n;n=JSON.parse(e.data),t._debug("Received",n),t._receive(n)}},p._disconnect=function(){this._clientId=null,this._setStatus("disconnected"),this._subscriptions={},this._reconnect=!1,this._transport.close()},p._getSubscription=function(e){var t;return t=this._subscriptions[e],t?t:null},p._removeSubscription=function(e){try{delete this._subscriptions[e]}catch(t){this._debug("nothing to delete for channel ",e)}},p._connectResponse=function(e){null===e.error?(this._clientId=e.body,this._setStatus("connected"),this.trigger("connect",[e])):(this.trigger("error",[e]),this.trigger("connect:error",[e]))},p._disconnectResponse=function(e){null===e.error?this.disconnect():(this.trigger("error",[e]),this.trigger("disconnect:error",[e.error]))},p._subscribeResponse=function(e){null!==e.error&&this.trigger("error",[e]);var t=e.body;if(null!==t){var n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("subscribe:success",[t]),i.trigger("ready",[t])):(i.trigger("subscribe:error",[e.error]),i.trigger("error",[e])))}},p._unsubscribeResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&null===e.error&&(i.trigger("unsubscribe",[t]),this._centrifuge._removeSubscription(n))},p._publishResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?i.trigger("publish:success",[t]):(i.trigger("publish:error",[e.error]),this.trigger("error",[e])))},p._presenceResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("presence",[t]),i.trigger("presence:success",[t])):(i.trigger("presence:error",[e.error]),this.trigger("error",[e])))},p._historyResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("history",[t]),i.trigger("history:success",[t])):(i.trigger("history:error",[e.error]),this.trigger("error",[e])))},p._joinResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&i.trigger("join",[t])},p._leaveResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&i.trigger("leave",[t])},p._messageResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);null!==i&&i.trigger("message",[t])},p._dispatchMessage=function(e){if(void 0!==e&&null!==e){var t=e.method;if(t)switch(t){case"connect":this._connectResponse(e);break;case"disconnect":this._disconnectResponse(e);break;case"subscribe":this._subscribeResponse(e);break;case"unsubscribe":this._unsubscribeResponse(e);break;case"publish":this._publishResponse(e);break;case"presence":this._presenceResponse(e);break;case"history":this._historyResponse(e);break;case"join":this._joinResponse(e);break;case"leave":this._leaveResponse(e);break;case"ping":break;case"message":this._messageResponse(e)}}},p._receive=function(e){if(Object.prototype.toString.call(e)===Object.prototype.toString.call([])){for(var t in e)if(e.hasOwnProperty(t)){var n=e[t];this._dispatchMessage(n)}}else Object.prototype.toString.call(e)===Object.prototype.toString.call({})&&this._dispatchMessage(e)},p._flush=function(){var e=this._messages.slice(0);this._messages=[],this._send(e)},p._ping=function(){var e={method:"ping",params:{}};this.send(e)},p.getClientId=function(){return this._clientId},p.isConnected=p._isConnected,p.isDisconnected=p._isDisconnected,p.configure=function(e){this._configure.call(this,e)},p.connect=p._connect,p.disconnect=p._disconnect,p.getSubscription=p._getSubscription,p.ping=p._ping,p.send=function(e){this._isBatching===!0?this._messages.push(e):this._send([e])},p.startBatching=function(){this._isBatching=!0},p.stopBatching=function(e){e=e||!1,this._isBatching=!1,e===!0&&this.flush()},p.flush=function(){this._flush()},p.subscribe=function(e,t){if(arguments.length<1)throw"Illegal arguments number: required 1, got "+arguments.length;if(!u(e))throw"Illegal argument type: channel must be a string";if(this.isDisconnected())throw"Illegal state: already disconnected";var n=this.getSubscription(e);if(null!==n)return n;var i=new g(this,e);return this._subscriptions[e]=i,i.subscribe(t),i},p.unsubscribe=function(e){if(arguments.length<1)throw"Illegal arguments number: required 1, got "+arguments.length;if(!u(e))throw"Illegal argument type: channel must be a string";if(!this.isDisconnected()){var t=this.getSubscription(e);null!==t&&t.unsubscribe()}},p.publish=function(e,t,n){var i=this.getSubscription(e);return null===i?(this._debug("subscription not found for channel "+e),null):(i.publish(t,n),i)},p.presence=function(e,t){var n=this.getSubscription(e);return null===n?(this._debug("subscription not found for channel "+e),null):(n.presence(t),n)},p.history=function(e,t){var n=this.getSubscription(e);return null===n?(this._debug("subscription not found for channel "+e),null):(n.history(t),n)},e(g,t);var _=g.prototype;_.getChannel=function(){return this.channel},_.getCentrifuge=function(){return this._centrifuge},_.subscribe=function(e){var t={method:"subscribe",params:{channel:this.channel}};this._centrifuge.send(t),e&&this.on("message",e)},_.unsubscribe=function(){this._centrifuge._removeSubscription(this.channel);var e={method:"unsubscribe",params:{channel:this.channel}};this._centrifuge.send(e)},_.publish=function(e,t){var n={method:"publish",params:{channel:this.channel,data:e}};t&&this.on("publish:success",t),this._centrifuge.send(n)},_.presence=function(e){var t={method:"presence",params:{channel:this.channel}};e&&this.on("presence",e),this._centrifuge.send(t)},_.history=function(e){var t={method:"history",params:{channel:this.channel}};e&&this.on("history",e),this._centrifuge.send(t)},"function"==typeof define&&define.amd?define(function(){return l}):"object"==typeof module&&module.exports?module.exports=l:this.Centrifuge=l}).call(this); | ||
(function(){"use strict";function e(e,t){return e.prototype=Object.create(t.prototype),e.prototype.constructor=e,t.prototype}function t(){}function n(e,t){for(var n=e.length;n--;)if(e[n].listener===t)return n;return-1}function i(e){return function(){return this[e].apply(this,arguments)}}function s(e,t){for(var n=t||{},i=2;i<arguments.length;++i){var o=arguments[i];if(void 0!==o&&null!==o)for(var c in o){var u=r(o,c),h=r(n,c);if(u!==t&&void 0!==u)if(e&&"object"==typeof u&&null!==u)if(u instanceof Array)n[c]=s(e,h instanceof Array?h:[],u);else{var a="object"!=typeof h||h instanceof Array?{}:h;n[c]=s(e,a,u)}else n[c]=u}}return n}function r(e,t){try{return e[t]}catch(n){return void 0}}function o(e,t){return-1!==e.indexOf(t,e.length-t.length)}function c(e,t){return 0===e.lastIndexOf(t,0)}function u(e){return"/"==e.substring(e.length-1)&&(e=e.substring(0,e.length-1)),e}function h(e){return void 0===e||null===e?!1:"string"==typeof e||e instanceof String}function a(e){return void 0===e||null===e?!1:"function"==typeof e}function f(e,t){if(window.console){var n=window.console[e];a(n)&&n.apply(window.console,t)}}function l(e){this._sockjs=!1,this._status="disconnected",this._reconnect=!0,this._transport=null,this._messageId=0,this._clientId=null,this._subscriptions={},this._messages=[],this._isBatching=!1,this._isAuthBatching=!1,this._authChannels={},this._refreshTimeout=null,this._config={retry:3e3,info:null,debug:!1,insecure:!1,server:null,protocols_whitelist:["websocket","xdr-streaming","xhr-streaming","iframe-eventsource","iframe-htmlfile","xdr-polling","xhr-polling","iframe-xhr-polling","jsonp-polling"],privateChannelPrefix:"$",refreshEndpoint:"/centrifuge/refresh",authEndpoint:"/centrifuge/auth",authHeaders:{},refreshHeaders:{}},e&&this.configure(e)}function g(e,t){this._centrifuge=e,this.channel=t}Object.create||(Object.create=function(){function e(){}return function(t){if(1!=arguments.length)throw new Error("Object.create implementation only accepts one parameter.");return e.prototype=t,new e}}()),Array.prototype.indexOf||(Array.prototype.indexOf=function(e){if(null==this)throw new TypeError;var t,n,i=Object(this),s=i.length>>>0;if(0===s)return-1;if(t=0,arguments.length>1&&(t=Number(arguments[1]),t!=t?t=0:0!=t&&1/0!=t&&t!=-1/0&&(t=(t>0||-1)*Math.floor(Math.abs(t)))),t>=s)return-1;for(n=t>=0?t:Math.max(s-Math.abs(t),0);s>n;n++)if(n in i&&i[n]===e)return n;return-1});var p=t.prototype;p.getListeners=function(e){var t,n,i=this._getEvents();if("object"==typeof e){t={};for(n in i)i.hasOwnProperty(n)&&e.test(n)&&(t[n]=i[n])}else t=i[e]||(i[e]=[]);return t},p.flattenListeners=function(e){var t,n=[];for(t=0;t<e.length;t+=1)n.push(e[t].listener);return n},p.getListenersAsObject=function(e){var t,n=this.getListeners(e);return n instanceof Array&&(t={},t[e]=n),t||n},p.addListener=function(e,t){var i,s=this.getListenersAsObject(e),r="object"==typeof t;for(i in s)s.hasOwnProperty(i)&&-1===n(s[i],t)&&s[i].push(r?t:{listener:t,once:!1});return this},p.on=i("addListener"),p.addOnceListener=function(e,t){return this.addListener(e,{listener:t,once:!0})},p.once=i("addOnceListener"),p.defineEvent=function(e){return this.getListeners(e),this},p.defineEvents=function(e){for(var t=0;t<e.length;t+=1)this.defineEvent(e[t]);return this},p.removeListener=function(e,t){var i,s,r=this.getListenersAsObject(e);for(s in r)r.hasOwnProperty(s)&&(i=n(r[s],t),-1!==i&&r[s].splice(i,1));return this},p.off=i("removeListener"),p.addListeners=function(e,t){return this.manipulateListeners(!1,e,t)},p.removeListeners=function(e,t){return this.manipulateListeners(!0,e,t)},p.manipulateListeners=function(e,t,n){var i,s,r=e?this.removeListener:this.addListener,o=e?this.removeListeners:this.addListeners;if("object"!=typeof t||t instanceof RegExp)for(i=n.length;i--;)r.call(this,t,n[i]);else for(i in t)t.hasOwnProperty(i)&&(s=t[i])&&("function"==typeof s?r.call(this,i,s):o.call(this,i,s));return this},p.removeEvent=function(e){var t,n=typeof e,i=this._getEvents();if("string"===n)delete i[e];else if("object"===n)for(t in i)i.hasOwnProperty(t)&&e.test(t)&&delete i[t];else delete this._events;return this},p.emitEvent=function(e,t){var n,i,s,r,o=this.getListenersAsObject(e);for(s in o)if(o.hasOwnProperty(s))for(i=o[s].length;i--;)n=o[s][i],n.once===!0&&this.removeListener(e,n.listener),r=n.listener.apply(this,t||[]),r===this._getOnceReturnValue()&&this.removeListener(e,n.listener);return this},p.trigger=i("emitEvent"),p.emit=function(e){var t=Array.prototype.slice.call(arguments,1);return this.emitEvent(e,t)},p.setOnceReturnValue=function(e){return this._onceReturnValue=e,this},p._getOnceReturnValue=function(){return this.hasOwnProperty("_onceReturnValue")?this._onceReturnValue:!0},p._getEvents=function(){return this._events||(this._events={})};var d={request:function(e,t,n){n.data=n.data||{};var i=function(e,t){var n,i=[];for(var s in e)if("object"==typeof e[s])for(var r in e[s])i.push(s+"="+encodeURIComponent(e[s][r]));else i.push(s+"="+encodeURIComponent(e[s]));return n=i.join("&"),""!=n?t?t.indexOf("?")<0?"?"+n:"&"+n:n:""},s={host:{},setHeaders:function(e){for(var t in e)this.xhr&&this.xhr.setRequestHeader(t,e[t])},process:function(n){var s=this;return this.xhr=null,window.ActiveXObject?this.xhr=new ActiveXObject("Microsoft.XMLHTTP"):window.XMLHttpRequest&&(this.xhr=new XMLHttpRequest),this.xhr&&(this.xhr.onreadystatechange=function(){if(4==s.xhr.readyState&&200==s.xhr.status){var e=s.xhr.responseText;if("undefined"==typeof JSON)throw"JSON undefined";e=JSON.parse(e),s.doneCallback&&s.doneCallback.apply(s.host,[e,s.xhr])}else 4==s.xhr.readyState&&s.failCallback&&s.failCallback.apply(s.host,[s.xhr]);s.alwaysCallback&&s.alwaysCallback.apply(s.host,[s.xhr])}),"get"==t?this.xhr.open("GET",e+i(n.data,e),!0):(this.xhr.open(t,e,!0),this.setHeaders({"X-Requested-With":"XMLHttpRequest","Content-type":"application/x-www-form-urlencoded"})),n.headers&&"object"==typeof n.headers&&this.setHeaders(n.headers),setTimeout(function(){"get"==t?s.xhr.send():s.xhr.send(i(n.data))},20),this},done:function(e){return this.doneCallback=e,this},fail:function(e){return this.failCallback=e,this},always:function(e){return this.alwaysCallback=e,this}};return s.process(n)}};e(l,t);var _=l.prototype;_._debug=function(){this._config.debug===!0&&f("debug",arguments)},_._configure=function(e){if(this._debug("Configuring centrifuge object with",e),e||(e={}),this._config=s(!1,this._config,e),!this._config.url)throw"Missing required configuration parameter 'url' specifying the Centrifuge server URL";if(!this._config.project)throw"Missing required configuration parameter 'project' specifying project ID in Centrifuge";if(!this._config.user&&""!==this._config.user){if(!this._config.insecure)throw"Missing required configuration parameter 'user' specifying user's unique ID in your application";this._debug("user not found but this is OK for insecure mode - anonymous access will be used"),this._config.user=""}if(!this._config.timestamp){if(!this._config.insecure)throw"Missing required configuration parameter 'timestamp'";this._debug("token not found but this is OK for insecure mode")}if(!this._config.token){if(!this._config.insecure)throw"Missing required configuration parameter 'token' specifying the sign of authorization request";this._debug("timestamp not found but this is OK for insecure mode")}if(this._config.url=u(this._config.url),o(this._config.url,"connection")){if("undefined"==typeof window.SockJS)throw"You need to include SockJS client library before Centrifuge javascript client library or use pure Websocket connection endpoint";this._sockjs=!0}},_._setStatus=function(e){this._status!==e&&(this._debug("Status",this._status,"->",e),this._status=e)},_._isDisconnected=function(){return this._isConnected()===!1},_._isConnected=function(){return"connected"===this._status},_._isConnecting=function(){return"connecting"===this._status},_._nextMessageId=function(){return++this._messageId},_._clearSubscriptions=function(){this._subscriptions={}},_._send=function(e){for(var t=0;t<e.length;++t){var n=e[t];n.uid=""+this._nextMessageId(),this._clientId&&(n.clientId=this._clientId),this._debug("Send",n),this._transport.send(JSON.stringify(n))}},_._connect=function(e){if(!this.isConnected()){this._clientId=null,this._reconnect=!0,this._clearSubscriptions(),this._setStatus("connecting");var t=this;if(e&&this.on("connect",e),this._sockjs===!0){var n={protocols_whitelist:this._config.protocols_whitelist};null!==this._config.server&&(n.server=this._config.server),this._transport=new SockJS(this._config.url,null,n)}else this._transport=new WebSocket(this._config.url);this._setStatus("connecting"),this._transport.onopen=function(){var e={method:"connect",params:{user:t._config.user,project:t._config.project}};null!==t._config.info&&(e.params.info=t._config.info),t._config.insecure||(e.params.timestamp=t._config.timestamp,e.params.token=t._config.token),t.send(e)},this._transport.onerror=function(e){t._debug(e)},this._transport.onclose=function(){t._setStatus("disconnected"),t.trigger("disconnect"),t._reconnect===!0&&window.setTimeout(function(){t._reconnect===!0&&t._connect.call(t)},t._config.retry)},this._transport.onmessage=function(e){var n;n=JSON.parse(e.data),t._debug("Received",n),t._receive(n)}}},_._disconnect=function(){this._clientId=null,this._setStatus("disconnected"),this._subscriptions={},this._reconnect=!1,this._transport.close()},_._getSubscription=function(e){var t;return t=this._subscriptions[e],t?t:null},_._removeSubscription=function(e){try{delete this._subscriptions[e]}catch(t){this._debug("nothing to delete for channel ",e)}try{delete this._authChannels[e]}catch(t){this._debug("nothing to delete from authChannels for channel ",e)}},_._connectResponse=function(e){if(!this.isConnected())if(null===e.error){if(!e.body)return;var t=e.body.expired;if(t)return void this.refresh();if(this._clientId=e.body.client,this._setStatus("connected"),this.trigger("connect",[e]),this._refreshTimeout&&window.clearTimeout(this._refreshTimeout),null!==e.body.ttl){var n=this;this._refreshTimeout=window.setTimeout(function(){n.refresh.call(n)},1e3*e.body.ttl)}}else this.trigger("error",[e]),this.trigger("connect:error",[e])},_._disconnectResponse=function(e){null===e.error?this.disconnect():(this.trigger("error",[e]),this.trigger("disconnect:error",[e.error]))},_._subscribeResponse=function(e){null!==e.error&&this.trigger("error",[e]);var t=e.body;if(null!==t){var n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("subscribe:success",[t]),i.trigger("ready",[t])):(i.trigger("subscribe:error",[e.error]),i.trigger("error",[e])))}},_._unsubscribeResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&null===e.error&&(i.trigger("unsubscribe",[t]),this._centrifuge._removeSubscription(n))},_._publishResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?i.trigger("publish:success",[t]):(i.trigger("publish:error",[e.error]),this.trigger("error",[e])))},_._presenceResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("presence",[t]),i.trigger("presence:success",[t])):(i.trigger("presence:error",[e.error]),this.trigger("error",[e])))},_._historyResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&(null===e.error?(i.trigger("history",[t]),i.trigger("history:success",[t])):(i.trigger("history:error",[e.error]),this.trigger("error",[e])))},_._joinResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&i.trigger("join",[t])},_._leaveResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);i&&i.trigger("leave",[t])},_._messageResponse=function(e){var t=e.body,n=t.channel,i=this.getSubscription(n);null!==i&&i.trigger("message",[t])},_._refreshResponse=function(e){if(this._refreshTimeout&&window.clearTimeout(this._refreshTimeout),null!==e.body.ttl){var t=this;t._refreshTimeout=window.setTimeout(function(){t.refresh.call(t)},1e3*e.body.ttl)}},_._dispatchMessage=function(e){if(void 0!==e&&null!==e){var t=e.method;if(t)switch(t){case"connect":this._connectResponse(e);break;case"disconnect":this._disconnectResponse(e);break;case"subscribe":this._subscribeResponse(e);break;case"unsubscribe":this._unsubscribeResponse(e);break;case"publish":this._publishResponse(e);break;case"presence":this._presenceResponse(e);break;case"history":this._historyResponse(e);break;case"join":this._joinResponse(e);break;case"leave":this._leaveResponse(e);break;case"ping":break;case"refresh":this._refreshResponse(e);break;case"message":this._messageResponse(e)}}},_._receive=function(e){if(Object.prototype.toString.call(e)===Object.prototype.toString.call([])){for(var t in e)if(e.hasOwnProperty(t)){var n=e[t];this._dispatchMessage(n)}}else Object.prototype.toString.call(e)===Object.prototype.toString.call({})&&this._dispatchMessage(e)},_._flush=function(){var e=this._messages.slice(0);this._messages=[],this._send(e)},_._ping=function(){var e={method:"ping",params:{}};this.send(e)},_.getClientId=function(){return this._clientId},_.isConnected=_._isConnected,_.isConnecting=_._isConnecting,_.isDisconnected=_._isDisconnected,_.configure=function(e){this._configure.call(this,e)},_.connect=_._connect,_.disconnect=_._disconnect,_.getSubscription=_._getSubscription,_.ping=_._ping,_.send=function(e){this._isBatching===!0?this._messages.push(e):this._send([e])},_.startBatching=function(){this._isBatching=!0},_.stopBatching=function(e){e=e||!1,this._isBatching=!1,e===!0&&this.flush()},_.flush=function(){this._flush()},_.startAuthBatching=function(){this._isAuthBatching=!0},_.stopAuthBatching=function(e){this._isAuthBatching=!1;var t=this._authChannels;this._authChannels={};var n=[];for(var i in t){var s=this.getSubscription(i);s&&n.push(i)}if(0==n.length)return void(e&&e());var r={client:this.getClientId(),channels:n},o=this;d.request(this._config.authEndpoint,"post",{headers:this._config.authHeaders,data:r}).done(function(e){for(var t in n){var i=n[t],s=e[i];if(s)if(s.status&&200!==s.status)o._subscribeResponse({error:s.status,body:{channel:i}});else{var r={method:"subscribe",params:{channel:i,client:o.getClientId(),info:s.info,sign:s.sign}};o.send(r)}else o._subscribeResponse({error:404,body:{channel:i}})}}).fail(function(){return f("info","authorization request failed"),!1}).always(function(){e&&e()})},_.subscribe=function(e,t){if(arguments.length<1)throw"Illegal arguments number: required 1, got "+arguments.length;if(!h(e))throw"Illegal argument type: channel must be a string";if(this.isDisconnected())throw"Illegal state: already disconnected";var n=this.getSubscription(e);if(null!==n)return n;var i=new g(this,e);return this._subscriptions[e]=i,i.subscribe(t),i},_.unsubscribe=function(e){if(arguments.length<1)throw"Illegal arguments number: required 1, got "+arguments.length;if(!h(e))throw"Illegal argument type: channel must be a string";if(!this.isDisconnected()){var t=this.getSubscription(e);null!==t&&t.unsubscribe()}},_.publish=function(e,t,n){var i=this.getSubscription(e);return null===i?(this._debug("subscription not found for channel "+e),null):(i.publish(t,n),i)},_.presence=function(e,t){var n=this.getSubscription(e);return null===n?(this._debug("subscription not found for channel "+e),null):(n.presence(t),n)},_.history=function(e,t){var n=this.getSubscription(e);return null===n?(this._debug("subscription not found for channel "+e),null):(n.history(t),n)},_.refresh=function(){var e=this;this._debug("refresh"),d.request(this._config.refreshEndpoint,"post",{headers:this._config.refreshHeaders,data:{}}).done(function(t){if(e._config.user=t.user,e._config.project=t.project,e._config.timestamp=t.timestamp,e._config.info=t.info,e._config.token=t.token,e._reconnect&&e.isDisconnected())e.connect();else{var n={method:"refresh",params:{user:e._config.user,project:e._config.project,timestamp:e._config.timestamp,info:e._config.info,token:e._config.token}};e.send(n)}}).fail(function(t){e._debug(t),e._debug("error getting connect parameters"),e._refreshTimeout&&window.clearTimeout(e._refreshTimeout),e._refreshTimeout=window.setTimeout(function(){e.refresh.call(e)},3e3)})},e(g,t);var b=g.prototype;b.getChannel=function(){return this.channel},b.getCentrifuge=function(){return this._centrifuge},b.subscribe=function(e){var t={method:"subscribe",params:{channel:this.channel}};c(this.channel,this._centrifuge._config.privateChannelPrefix)?this._centrifuge._isAuthBatching?this._centrifuge._authChannels[this.channel]=!0:(this._centrifuge.startAuthBatching(),this.subscribe(e),this._centrifuge.stopAuthBatching()):this._centrifuge.send(t),e&&this.on("message",e)},b.unsubscribe=function(){this._centrifuge._removeSubscription(this.channel);var e={method:"unsubscribe",params:{channel:this.channel}};this._centrifuge.send(e)},b.publish=function(e,t){var n={method:"publish",params:{channel:this.channel,data:e}};t&&this.on("publish:success",t),this._centrifuge.send(n)},b.presence=function(e){var t={method:"presence",params:{channel:this.channel}};e&&this.on("presence",e),this._centrifuge.send(t)},b.history=function(e){var t={method:"history",params:{channel:this.channel}};e&&this.on("history",e),this._centrifuge.send(t)},"function"==typeof define&&define.amd?define(function(){return l}):"object"==typeof module&&module.exports?module.exports=l:this.Centrifuge=l}).call(this); |
{ | ||
"name": "centrifuge", | ||
"version": "0.5.2", | ||
"version": "0.7.0", | ||
"description": "Centrifuge javascript client", | ||
@@ -9,5 +9,6 @@ "main": "centrifuge.js", | ||
"scripts": { | ||
"build-js": "cat centrifuge.js | uglifyjs -cm > centrifuge.min.js", | ||
"build-js": "cat src/centrifuge.js > centrifuge.js", | ||
"build-minified-js": "cat src/centrifuge.js | uglifyjs -cm > centrifuge.min.js", | ||
"build-dom-plugin": "cat plugins/centrifuge-dom/centrifuge.dom.js | uglifyjs -cm > plugins/centrifuge-dom/centrifuge.dom.min.js", | ||
"build": "npm run build-js", | ||
"build": "npm run build-js && npm run build-minified-js", | ||
"build-plugins": "npm run build-dom-plugin" | ||
@@ -14,0 +15,0 @@ }, |
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
144308
11
3013
0
20