More reliably close the socket on shutdown(). Make logging include namespace.
This commit is contained in:
@@ -57,15 +57,13 @@ Cosmopolite = function(callbacks, urlPrefix, namespace) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Cosmopolite.prototype.shutdown = function() {
|
Cosmopolite.prototype.shutdown = function() {
|
||||||
|
console.log(this.loggingPrefix_(), 'shutdown');
|
||||||
this.shutdown_ = true;
|
this.shutdown_ = true;
|
||||||
if (this.socket_) {
|
if (this.socket_) {
|
||||||
var socket = this.socket_;
|
this.socket_.close();
|
||||||
this.socket_ = null;
|
|
||||||
socket.close();
|
|
||||||
}
|
}
|
||||||
if (this.messageHandler_) {
|
if (this.messageHandler_) {
|
||||||
window.removeEventListener('message', this.messageHandler_);
|
window.removeEventListener('message', this.messageHandler_);
|
||||||
this.messageHandler_ = null;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -84,7 +82,9 @@ Cosmopolite.prototype.subscribe = function(subject, messages, keys) {
|
|||||||
}
|
}
|
||||||
keys = keys || [];
|
keys = keys || [];
|
||||||
if (subject in this.subscriptions_) {
|
if (subject in this.subscriptions_) {
|
||||||
console.log('cosmopolite: not sending duplication subscription request for subject:', subject);
|
console.log(
|
||||||
|
this.loggingPrefix_(),
|
||||||
|
'not sending duplication subscription request for subject:', subject);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.subscriptions_[subject] = {
|
this.subscriptions_[subject] = {
|
||||||
@@ -164,6 +164,10 @@ Cosmopolite.prototype.getKeyMessage = function(subject, key) {
|
|||||||
return this.subscriptions_[subject].keys[key];
|
return this.subscriptions_[subject].keys[key];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Cosmopolite.prototype.loggingPrefix_ = function() {
|
||||||
|
return 'cosmopolite (' + this.namespace_ + '):';
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback when a script loads.
|
* Callback when a script loads.
|
||||||
*/
|
*/
|
||||||
@@ -204,7 +208,7 @@ Cosmopolite.prototype.onReceiveMessage_ = function(data) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log('cosmopolite: unknown event type:', data);
|
console.log(this.loggingPrefix_(), 'unknown event type:', data);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -218,10 +222,10 @@ Cosmopolite.prototype.onReceiveMessage_ = function(data) {
|
|||||||
Cosmopolite.prototype.registerMessageHandlers_ = function() {
|
Cosmopolite.prototype.registerMessageHandlers_ = function() {
|
||||||
this.messageHandler_ = function(e) {
|
this.messageHandler_ = function(e) {
|
||||||
if (e.origin != window.location.origin) {
|
if (e.origin != window.location.origin) {
|
||||||
console.log('cosmopolite: received message from bad origin:', e.origin);
|
console.log(this.loggingPrefix_(), 'received message from bad origin:', e.origin);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log('cosmopolite: received browser message:', e.data);
|
console.log(this.loggingPrefix_(), 'received browser message:', e.data);
|
||||||
this.onReceiveMessage_(e.data);
|
this.onReceiveMessage_(e.data);
|
||||||
}.bind(this);
|
}.bind(this);
|
||||||
window.addEventListener('message', this.messageHandler_);
|
window.addEventListener('message', this.messageHandler_);
|
||||||
@@ -296,7 +300,8 @@ Cosmopolite.prototype.sendRPCs_ = function(commands, delay) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data['status'] != 'ok') {
|
if (data['status'] != 'ok') {
|
||||||
console.log('cosmopolite: server returned unknown status:', data['status']);
|
console.log(this.loggingPrefix_(),
|
||||||
|
'server returned unknown status:', data['status']);
|
||||||
// TODO(flamingcow): Refresh the page? Show an alert?
|
// TODO(flamingcow): Refresh the page? Show an alert?
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -314,7 +319,8 @@ Cosmopolite.prototype.sendRPCs_ = function(commands, delay) {
|
|||||||
xhr.getResponseHeader('Retry-After') ||
|
xhr.getResponseHeader('Retry-After') ||
|
||||||
Math.min(32, Math.max(2, delay || 2));
|
Math.min(32, Math.max(2, delay || 2));
|
||||||
console.log(
|
console.log(
|
||||||
'cosmopolite: RPC failed; will retry in ' + intDelay + ' seconds');
|
this.loggingPrefix_(),
|
||||||
|
'RPC failed; will retry in ' + intDelay + ' seconds');
|
||||||
function retry() {
|
function retry() {
|
||||||
this.sendRPCs_(commands, Math.pow(intDelay, 2));
|
this.sendRPCs_(commands, Math.pow(intDelay, 2));
|
||||||
}
|
}
|
||||||
@@ -363,7 +369,7 @@ Cosmopolite.prototype.onCreateChannel_ = function(data) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
var channel = new goog.appengine.Channel(data['token']);
|
var channel = new goog.appengine.Channel(data['token']);
|
||||||
console.log('cosmopolite: opening channel:', data['token']);
|
console.log(this.loggingPrefix_(), 'opening channel:', data['token']);
|
||||||
this.socket_ = channel.open({
|
this.socket_ = channel.open({
|
||||||
onopen: this.onSocketOpen_.bind(this),
|
onopen: this.onSocketOpen_.bind(this),
|
||||||
onclose: this.onSocketClose_.bind(this),
|
onclose: this.onSocketClose_.bind(this),
|
||||||
@@ -376,18 +382,20 @@ Cosmopolite.prototype.onCreateChannel_ = function(data) {
|
|||||||
* Callback from channel library for successful open
|
* Callback from channel library for successful open
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.onSocketOpen_ = function() {
|
Cosmopolite.prototype.onSocketOpen_ = function() {
|
||||||
console.log('cosmopolite: channel opened');
|
console.log(this.loggingPrefix_(), 'channel opened');
|
||||||
|
if (this.shutdown_ && this.socket_) {
|
||||||
|
this.socket_.close();
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback from channel library for closure; reopen.
|
* Callback from channel library for closure; reopen.
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.onSocketClose_ = function() {
|
Cosmopolite.prototype.onSocketClose_ = function() {
|
||||||
if (!this.socket_) {
|
console.log(this.loggingPrefix_(), 'channel closed');
|
||||||
|
if (this.shutdown_) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
console.log('cosmopolite: channel closed');
|
|
||||||
this.socket_ = null;
|
|
||||||
this.createChannel_();
|
this.createChannel_();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -426,14 +434,16 @@ Cosmopolite.prototype.onServerEvent_ = function(e) {
|
|||||||
case 'message':
|
case 'message':
|
||||||
var subscription = this.subscriptions_[e['subject']];
|
var subscription = this.subscriptions_[e['subject']];
|
||||||
if (!subscription) {
|
if (!subscription) {
|
||||||
console.log('cosmopolite: message from unrecognized subject:', e);
|
console.log(
|
||||||
|
this.loggingPrefix_(),
|
||||||
|
'message from unrecognized subject:', e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
var duplicate = subscription.messages.some(function(message) {
|
var duplicate = subscription.messages.some(function(message) {
|
||||||
return message['id'] == e.id;
|
return message['id'] == e.id;
|
||||||
});
|
});
|
||||||
if (duplicate) {
|
if (duplicate) {
|
||||||
console.log('cosmopolite: duplicate message:', e);
|
console.log(this.loggingPrefix_(), 'duplicate message:', e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
e['message'] = JSON.parse(e['message']);
|
e['message'] = JSON.parse(e['message']);
|
||||||
@@ -447,7 +457,7 @@ Cosmopolite.prototype.onServerEvent_ = function(e) {
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// Client out of date? Force refresh?
|
// Client out of date? Force refresh?
|
||||||
console.log('cosmopolite: unknown channel event:', e);
|
console.log(this.loggingPrefix_(), 'unknown channel event:', e);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -458,7 +468,7 @@ Cosmopolite.prototype.onServerEvent_ = function(e) {
|
|||||||
* @param {!string} msg Descriptive text
|
* @param {!string} msg Descriptive text
|
||||||
*/
|
*/
|
||||||
Cosmopolite.prototype.onSocketError_ = function(msg) {
|
Cosmopolite.prototype.onSocketError_ = function(msg) {
|
||||||
console.log('cosmopolite: socket error:', msg);
|
console.log(this.loggingPrefix_(), 'socket error:', msg);
|
||||||
if (this.socket_) {
|
if (this.socket_) {
|
||||||
this.socket_.close();
|
this.socket_.close();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user