2013-09-10 20:39:46 +02:00
|
|
|
function Socket(url) {
|
|
|
|
this.url = url;
|
|
|
|
this._is_open = false;
|
|
|
|
this._is_authenticated = false;
|
2013-11-18 21:43:09 +01:00
|
|
|
this._is_reconnecting = false;
|
|
|
|
this._reconnect_initiation_time = null;
|
2013-09-10 20:39:46 +02:00
|
|
|
this._send_queue = [];
|
|
|
|
this._next_req_id = 0;
|
|
|
|
this._requests = {};
|
|
|
|
this._connection_failures = 0;
|
2013-11-15 21:52:33 +01:00
|
|
|
this._reconnect_timeout_id = null;
|
2013-11-16 00:28:07 +01:00
|
|
|
this._heartbeat_timeout_id = null;
|
2013-09-10 20:39:46 +02:00
|
|
|
|
2013-11-19 23:52:03 +01:00
|
|
|
var that = this;
|
2013-09-10 20:39:46 +02:00
|
|
|
this._is_unloading = false;
|
|
|
|
$(window).on("unload", function () {
|
2013-11-19 23:52:03 +01:00
|
|
|
that._is_unloading = true;
|
2013-09-10 20:39:46 +02:00
|
|
|
});
|
|
|
|
|
2013-11-18 22:05:56 +01:00
|
|
|
$(document).on("unsuspend", function () {
|
2013-11-19 23:52:03 +01:00
|
|
|
that._try_to_reconnect();
|
2013-11-18 22:05:56 +01:00
|
|
|
});
|
|
|
|
|
2013-10-08 22:10:27 +02:00
|
|
|
this._supported_protocols = ['websocket', 'xdr-streaming', 'xhr-streaming',
|
|
|
|
'xdr-polling', 'xhr-polling', 'jsonp-polling'];
|
2013-10-11 21:36:49 +02:00
|
|
|
if (page_params.test_suite) {
|
|
|
|
this._supported_protocols = _.reject(this._supported_protocols,
|
|
|
|
function (x) { return x === 'xhr-streaming'; });
|
|
|
|
}
|
|
|
|
|
2013-10-08 22:10:27 +02:00
|
|
|
this._sockjs = new SockJS(url, null, {protocols_whitelist: this._supported_protocols});
|
2013-09-10 20:39:46 +02:00
|
|
|
this._setup_sockjs_callbacks(this._sockjs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket.prototype = {
|
|
|
|
send: function Socket_send(msg, success, error) {
|
|
|
|
if (! this._can_send()) {
|
|
|
|
this._send_queue.push({msg: msg, success: success, error: error});
|
2013-11-18 21:43:09 +01:00
|
|
|
this._try_to_reconnect();
|
2013-09-10 20:39:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._do_send('request', msg, success, error);
|
|
|
|
},
|
|
|
|
|
|
|
|
_do_send: function Socket__do_send(type, msg, success, error) {
|
|
|
|
var req_id = this._next_req_id;
|
2013-11-18 23:15:35 +01:00
|
|
|
var that = this;
|
2013-09-10 20:39:46 +02:00
|
|
|
this._next_req_id++;
|
2013-11-19 00:07:06 +01:00
|
|
|
this._requests[req_id] = {type: type, request: msg, success: success,
|
|
|
|
error: error};
|
2013-11-18 23:15:35 +01:00
|
|
|
this._requests[req_id].ack_timeout_id = setTimeout(function () {
|
|
|
|
blueslip.info("Timeout on ACK for request " + req_id);
|
|
|
|
that._try_to_reconnect();
|
|
|
|
}, 2000);
|
|
|
|
|
2013-12-05 20:11:19 +01:00
|
|
|
try {
|
|
|
|
this._sockjs.send(JSON.stringify({req_id: req_id,
|
|
|
|
type: type, request: msg}));
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof Error && e.message === 'INVALID_STATE_ERR') {
|
|
|
|
// The connection was somehow closed. Our on-close handler will
|
|
|
|
// be called imminently and we'll retry this request upon reconnect.
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2013-09-10 20:39:46 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
_can_send: function Socket__can_send() {
|
|
|
|
return this._is_open && this._is_authenticated;
|
|
|
|
},
|
|
|
|
|
2013-11-19 00:07:06 +01:00
|
|
|
_resend: function Socket__resend(req_id) {
|
|
|
|
var req_info = this._requests[req_id];
|
|
|
|
if (req_info.ack_timeout_id !== null) {
|
|
|
|
clearTimeout(req_info.ack_timeout_id);
|
|
|
|
req_info.ack_timeout_id = null;
|
|
|
|
}
|
|
|
|
delete this._requests[req_id];
|
|
|
|
|
|
|
|
if (req_info.type !== 'request') {
|
|
|
|
blueslip.error("Cannot resend message of type: " + req_info.type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.send(req_info.request, req_info.success, req_info.error);
|
|
|
|
},
|
|
|
|
|
2013-11-06 18:51:59 +01:00
|
|
|
_drain_queue: function Socket__drain_queue() {
|
2013-09-10 20:39:46 +02:00
|
|
|
var that = this;
|
|
|
|
var queue = this._send_queue;
|
|
|
|
this._send_queue = [];
|
|
|
|
_.each(queue, function (elem) {
|
|
|
|
that.send(elem.msg, elem.success, elem.error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2013-11-06 18:51:59 +01:00
|
|
|
_process_response: function Socket__process_response(req_id, response) {
|
|
|
|
var req_info = this._requests[req_id];
|
|
|
|
if (req_info === undefined) {
|
2013-11-08 20:16:31 +01:00
|
|
|
if (req_id >= this._next_req_id) {
|
|
|
|
blueslip.error("Got a response for an unknown request",
|
|
|
|
{request_id: req_id, next_id: this._next_req_id,
|
|
|
|
outstanding_ids: _.keys(this._requests)});
|
|
|
|
}
|
|
|
|
// There is a small race where we might start reauthenticating
|
|
|
|
// before one of our requests has finished but then have the request
|
|
|
|
// finish and thus receive the finish notification both from the
|
|
|
|
// status inquiry and from the normal response. Therefore, we might
|
|
|
|
// be processing the response for a request where we already got the
|
|
|
|
// response from a status inquiry. In that case, don't process the
|
|
|
|
// response twice.
|
2013-11-06 18:51:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.result === 'success') {
|
|
|
|
req_info.success(response);
|
|
|
|
} else {
|
|
|
|
req_info.error('response', response);
|
|
|
|
}
|
|
|
|
delete this._requests[req_id];
|
2013-09-10 20:39:46 +02:00
|
|
|
},
|
|
|
|
|
2013-11-18 23:15:35 +01:00
|
|
|
_process_ack: function Socket__process_ack(req_id) {
|
|
|
|
var req_info = this._requests[req_id];
|
|
|
|
if (req_info === undefined) {
|
|
|
|
blueslip.error("Got an ACK for an unknown request",
|
|
|
|
{request_id: req_id, next_id: this._next_req_id,
|
|
|
|
outstanding_ids: _.keys(this._requests)});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req_info.ack_timeout_id !== null) {
|
|
|
|
clearTimeout(req_info.ack_timeout_id);
|
|
|
|
req_info.ack_timeout_id = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2013-09-10 20:39:46 +02:00
|
|
|
_setup_sockjs_callbacks: function Socket__setup_sockjs_callbacks(sockjs) {
|
|
|
|
var that = this;
|
|
|
|
sockjs.onopen = function Socket__sockjs_onopen() {
|
2013-10-26 02:58:49 +02:00
|
|
|
blueslip.info("Socket connected.");
|
2013-09-10 20:39:46 +02:00
|
|
|
that._is_open = true;
|
|
|
|
|
2013-11-20 00:05:16 +01:00
|
|
|
// Notify listeners that we've finished the websocket handshake
|
|
|
|
$(document).trigger($.Event('websocket_postopen.zulip', {}));
|
|
|
|
|
2013-09-10 20:39:46 +02:00
|
|
|
// We can only authenticate after the DOM has loaded because we need
|
|
|
|
// the CSRF token
|
|
|
|
$(function () {
|
2013-11-04 23:00:53 +01:00
|
|
|
that._do_send('auth', {csrf_token: csrf_token,
|
2013-11-06 18:51:59 +01:00
|
|
|
queue_id: page_params.event_queue_id,
|
|
|
|
status_inquiries: _.keys(that._requests)},
|
|
|
|
function (resp) {
|
2013-09-10 20:39:46 +02:00
|
|
|
that._is_authenticated = true;
|
2013-11-18 21:43:09 +01:00
|
|
|
that._is_reconnecting = false;
|
|
|
|
that._reconnect_initiation_time = null;
|
2013-09-10 20:39:46 +02:00
|
|
|
that._connection_failures = 0;
|
2013-11-06 18:51:59 +01:00
|
|
|
_.each(resp.status_inquiries, function (status, id) {
|
|
|
|
if (status.status === 'complete') {
|
|
|
|
that._process_response(id, status.response);
|
|
|
|
}
|
|
|
|
if (status.status === 'not_received') {
|
2013-11-19 00:07:06 +01:00
|
|
|
that._resend(id);
|
2013-11-06 18:51:59 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
that._drain_queue();
|
2013-09-10 20:39:46 +02:00
|
|
|
},
|
|
|
|
function (type, resp) {
|
|
|
|
blueslip.info("Could not authenticate with server: " + resp.msg);
|
2013-11-18 21:43:09 +01:00
|
|
|
that._connection_failures++;
|
|
|
|
that._try_to_reconnect(that._reconnect_wait_time());
|
2013-09-10 20:39:46 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
sockjs.onmessage = function Socket__sockjs_onmessage(event) {
|
2013-11-18 23:15:35 +01:00
|
|
|
if (event.data.type === 'ack') {
|
|
|
|
that._process_ack(event.data.req_id);
|
|
|
|
} else {
|
2013-11-18 23:12:56 +01:00
|
|
|
that._process_response(event.data.req_id, event.data.response);
|
|
|
|
}
|
2013-09-10 20:39:46 +02:00
|
|
|
};
|
|
|
|
|
2013-11-16 00:28:07 +01:00
|
|
|
sockjs.onheartbeat = function Socket__socjks_onheartbeat() {
|
|
|
|
if (that._heartbeat_timeout_id !== null) {
|
|
|
|
clearTimeout(that._heartbeat_timeout_id);
|
|
|
|
that._heartbeat_timeout_id = null;
|
|
|
|
}
|
|
|
|
that._heartbeat_timeout_id = setTimeout(function () {
|
|
|
|
that._heartbeat_timeout_id = null;
|
|
|
|
blueslip.info("Missed too many hearbeats");
|
|
|
|
that._try_to_reconnect();
|
|
|
|
}, 60000);
|
|
|
|
};
|
|
|
|
|
2013-09-10 20:39:46 +02:00
|
|
|
sockjs.onclose = function Socket__sockjs_onclose() {
|
|
|
|
if (that._is_unloading) {
|
|
|
|
return;
|
|
|
|
}
|
2013-11-20 00:05:16 +01:00
|
|
|
// We've failed to handshake, but notify that the attempt finished
|
|
|
|
$(document).trigger($.Event('websocket_postopen.zulip', {}));
|
|
|
|
|
2013-09-10 20:39:46 +02:00
|
|
|
blueslip.info("SockJS connection lost. Attempting to reconnect soon.");
|
2013-11-18 21:43:09 +01:00
|
|
|
that._connection_failures++;
|
|
|
|
that._is_reconnecting = false;
|
|
|
|
that._try_to_reconnect(that._reconnect_wait_time());
|
2013-09-10 20:39:46 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2013-11-18 21:43:09 +01:00
|
|
|
_reconnect_wait_time: function Socket__reconnect_wait_time() {
|
2013-09-10 20:39:46 +02:00
|
|
|
if (this._connection_failures === 1) {
|
|
|
|
// We specify a non-zero timeout here so that we don't try to
|
|
|
|
// immediately reconnect when the page is refreshing
|
2013-11-18 21:43:09 +01:00
|
|
|
return 30;
|
2013-09-10 20:39:46 +02:00
|
|
|
} else {
|
2013-11-18 21:43:09 +01:00
|
|
|
return Math.min(90, Math.exp(this._connection_failures/2)) * 1000;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_try_to_reconnect: function Socket__try_to_reconnect(wait_time) {
|
|
|
|
if (wait_time === undefined) {
|
|
|
|
wait_time = 0;
|
2013-09-10 20:39:46 +02:00
|
|
|
}
|
2013-11-18 21:43:09 +01:00
|
|
|
var that = this;
|
|
|
|
|
|
|
|
var now = (new Date()).getTime();
|
|
|
|
if (this._is_reconnecting && now - this._reconnect_initiation_time < 1000) {
|
|
|
|
// Only try to reconnect once a second
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this._reconnect_timeout_id !== null) {
|
|
|
|
clearTimeout(this._reconnect_timeout_id);
|
|
|
|
this._reconnect_timeout_id = null;
|
|
|
|
}
|
|
|
|
|
2013-11-16 00:28:07 +01:00
|
|
|
if (this._heartbeat_timeout_id !== null) {
|
|
|
|
clearTimeout(that._heartbeat_timeout_id);
|
|
|
|
this._heartbeat_timeout_id = null;
|
|
|
|
}
|
|
|
|
|
2013-12-05 20:11:50 +01:00
|
|
|
// Cancel any pending auth requests and any timeouts for ACKs
|
2013-12-05 17:31:20 +01:00
|
|
|
_.each(this._requests, function (val, key) {
|
2013-12-05 20:11:50 +01:00
|
|
|
if (val.ack_timeout_id !== null) {
|
|
|
|
clearTimeout(val.ack_timeout_id);
|
|
|
|
val.ack_timeout_id = null;
|
|
|
|
}
|
|
|
|
|
2013-12-05 17:31:20 +01:00
|
|
|
if (val.type === 'auth') {
|
|
|
|
delete that._requests[key];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-18 21:43:09 +01:00
|
|
|
this._is_open = false;
|
|
|
|
this._is_authenticated = false;
|
|
|
|
this._is_reconnecting = true;
|
|
|
|
this._reconnect_initiation_time = now;
|
2013-12-04 20:41:09 +01:00
|
|
|
this._sockjs.close();
|
2013-09-10 20:39:46 +02:00
|
|
|
|
2013-11-15 21:52:33 +01:00
|
|
|
this._reconnect_timeout_id = setTimeout(function () {
|
|
|
|
that._reconnect_timeout_id = null;
|
2013-11-18 21:43:09 +01:00
|
|
|
blueslip.info("Attempting socket reconnect.");
|
|
|
|
that._sockjs = new SockJS(that.url, null, {protocols_whitelist: that._supported_protocols});
|
|
|
|
that._setup_sockjs_callbacks(that._sockjs);
|
2013-09-10 20:39:46 +02:00
|
|
|
}, wait_time);
|
|
|
|
}
|
|
|
|
};
|