Skip to content

Commit

Permalink
work on longpoll
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Oberstein committed Sep 22, 2014
1 parent 94548bc commit a9be29b
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 66 deletions.
145 changes: 94 additions & 51 deletions package/lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@ Connection.prototype._create_transport = function () {
log.debug("could not create WAMP transport '" + transport_factory.type + "': " + e);
}
}
throw "could not create any WAMP transport";

// could not create any WAMP transport
return null;
};


Expand Down Expand Up @@ -193,36 +195,98 @@ Connection.prototype._init_transport_factories = function () {
};



Connection.prototype.open = function () {
Connection.prototype._autoreconnect_reset_timer = function () {

var self = this;

if (self._transport) {
throw "connection already open (or opening)";
if (self._retry_timer) {
clearTimeout(self._retry_timer);
}
self._retry_timer = null;
}


Connection.prototype._autoreconnect_reset = function () {

var self = this;

self._autoreconnect_reset_timer();

// reset reconnection tracking
self._retry = true;
self._retry_count = 0;
self._retry_delay = self._initial_retry_delay;
self._is_retrying = false;
if (self._retry_timer) {
log.debug("cancelling automatic retry upon manual retry");
clearTimeout(self._retry_timer);
}


Connection.prototype._autoreconnect_advance = function () {

var self = this;

// jitter retry delay
if (self._retry_delay_jitter) {
self._retry_delay = util.rand_normal(self._retry_delay, self._retry_delay * self._retry_delay_jitter);
}
self._retry_timer = null;

// cap the retry delay
if (self._retry_delay > self._max_retry_delay) {
self._retry_delay = self._max_retry_delay;
}

// count number of retries
self._retry_count += 1;

var res;
if (self._retry && self._retry_count <= self._max_retries) {
res = {
count: self._retry_count,
delay: self._retry_delay,
will_retry: true
};
} else {
res = {
count: null,
delay: null,
will_retry: false
}
}

// retry delay growth for next retry cycle
if (self._retry_delay_growth) {
self._retry_delay = self._retry_delay * self._retry_delay_growth;
}

return res;
}


Connection.prototype.open = function () {

var self = this;

if (self._transport) {
throw "connection already open (or opening)";
}

self._autoreconnect_reset();
self._retry = true;

function retry () {

// let the WebSocket factory produce a new WebSocket connection
// which will automatically connect
// create a WAMP transport
self._transport = self._create_transport();

if (!self._transport) {
// failed to create a WAMP transport
self._retry = false;
if (self.onclose) {
self.onclose("unsupported", "WebSocket transport unsupported");
var details = {
reason: null,
message: null,
retry_delay: null,
retry_count: null,
will_retry: false
};
self.onclose("unsupported", details);
}
return;
}
Expand All @@ -234,11 +298,8 @@ Connection.prototype.open = function () {

self._transport.onopen = function () {

// remove any pending reconnect timer
if (self._retry_timer) {
clearTimeout(self._retry_timer);
}
self._retry_timer = null;
// reset auto-reconnect timer and tracking
self._autoreconnect_reset();

// log successful connections
self._connect_successes += 1;
Expand All @@ -263,13 +324,16 @@ Connection.prototype.open = function () {

self._session.onleave = function (reason, details) {
self._session_close_reason = reason;
self._session_close_message = details.message;
self._session_close_message = details.message || "";
self._retry = false;
self._transport.close(1000);
};

self._transport.onclose = function (evt) {

// remove any pending reconnect timer
self._autoreconnect_reset_timer();

self._transport = null;

var reason = null;
Expand All @@ -285,38 +349,22 @@ Connection.prototype.open = function () {
} else {
reason = "closed";
}

// Connection.onclose() allows to cancel any subsequent retry attempt
var stop_retrying = false;

// jitter retry delay
if (self._retry_delay_jitter) {
self._retry_delay = util.rand_normal(self._retry_delay, self._retry_delay * self._retry_delay_jitter);
}

// cap the retry delay
if (self._retry_delay > self._max_retry_delay) {
self._retry_delay = self._max_retry_delay;
}

// count number of retries
self._retry_count += 1;

// flag that indicated if we would retry (if retrying is not stopped manually)
var will_retry = self._retry_count <= self._max_retries;

var next_retry = self._autoreconnect_advance();

// fire app code handler
//
if (self.onclose) {
var details = {
reason: self._session_close_reason,
message: self._session_close_message,
retry_delay: self._retry_delay,
retry_count: self._retry_count,
will_retry: will_retry
retry_delay: next_retry.delay,
retry_count: next_retry.count,
will_retry: next_retry.will_retry
};
try {
stop_retrying = self.onclose(reason, details);
// Connection.onclose() allows to cancel any subsequent retry attempt
var stop_retrying = self.onclose(reason, details);
} catch (e) {
log.debug("Exception raised from app code while firing Connection.onclose()", e);
}
Expand All @@ -335,17 +383,12 @@ Connection.prototype.open = function () {
//
if (self._retry && !stop_retrying) {

if (will_retry) {
if (next_retry.will_retry) {

self._is_retrying = true;

log.debug("retrying in " + self._retry_delay + " s");
self._retry_timer = setTimeout(retry, self._retry_delay * 1000);

// retry delay growth for next retry cycle
if (self._retry_delay_growth) {
self._retry_delay = self._retry_delay * self._retry_delay_growth;
}
log.debug("retrying in " + next_retry.delay + " s");
self._retry_timer = setTimeout(retry, next_retry.delay * 1000);

} else {
log.debug("giving up trying to reconnect");
Expand Down
10 changes: 5 additions & 5 deletions package/lib/transport/longpoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ Factory.prototype.create = function () {
var rxseq = 0;

var options = {'protocols': ['wamp.2.json']};
var http_timeout = 2000;
var request_timeout = self._options.request_timeout || 2000;

util.http_post(self._options.url + '/open', JSON.stringify(options), http_timeout).then(
util.http_post(self._options.url + '/open', JSON.stringify(options), request_timeout).then(

function (payload) {

Expand All @@ -86,7 +86,7 @@ Factory.prototype.create = function () {

is_closing = true;

util.http_post(base_url + '/close', null, http_timeout).then(
util.http_post(base_url + '/close', null, request_timeout).then(

function () {
log.debug("longpoll.Transport: transport closed");
Expand Down Expand Up @@ -116,7 +116,7 @@ Factory.prototype.create = function () {

var payload = JSON.stringify(msg);

util.http_post(base_url + '/send', payload, http_timeout).then(
util.http_post(base_url + '/send', payload, request_timeout).then(

function () {
// ok, message sent
Expand All @@ -143,7 +143,7 @@ Factory.prototype.create = function () {

log.debug("longpoll.Transport: polling for message ...");

util.http_post(base_url + '/receive', null, 0).then(
util.http_post(base_url + '/receive', null, request_timeout).then(

function (payload) {

Expand Down
5 changes: 4 additions & 1 deletion package/lib/transport/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ Factory.prototype.create = function () {
transport.onclose(details);
}

websocket.onerror = websocket.onclose;
// do NOT do the following, since that will make
// transport.onclose() fire twice (browsers already fire
// websocket.onclose() for errors also)
//websocket.onerror = websocket.onclose;

transport.send = function (msg) {
var payload = JSON.stringify(msg);
Expand Down
17 changes: 8 additions & 9 deletions test/longpoll/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<body>
<h1>PubSub Basic Frontend</h1>
<p>Open JavaScript console to watch output.</p>
<button onclick="open_session()">Open Session</button>
<button onclick="close_session()">Close Session</button>
<script>AUTOBAHN_DEBUG = true;</script>
<script>AUTOBAHN_DEBUG = false;</script>
<script src="/build/autobahn.js"></script>
<script>
console.log("Running on AutobahnJS", autobahn.version);
Expand Down Expand Up @@ -37,14 +38,11 @@ <h1>PubSub Basic Frontend</h1>
//
connection.onopen = function (session, details) {

console.log("Connected", connection.transport.info);

session.onleave = function () {
connection.close();
}
console.log("Connected: " + JSON.stringify(connection.transport.info));

close_session = function () {
session.leave();
//session.leave("wamp.close.normal", "I am fed up!!");
}

if (true) {
Expand Down Expand Up @@ -121,8 +119,7 @@ <h1>PubSub Basic Frontend</h1>
// fired when connection was lost (or could not be established)
//
connection.onclose = function (reason, details) {
console.log("Connection lost: " + reason);
console.log(details);
console.log("Connection gone: " + reason + " (" + JSON.stringify(details) + ")");
if (t1) {
clearInterval(t1);
t1 = null;
Expand All @@ -139,7 +136,9 @@ <h1>PubSub Basic Frontend</h1>

// now actually open the connection
//
connection.open();
function open_session() {
connection.open();
}
</script>
</body>
</html>

0 comments on commit a9be29b

Please sign in to comment.