Commit e1f3dd26 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

refactor on processing messages in long polling javascript class

parent 14680518
...@@ -377,6 +377,7 @@ ...@@ -377,6 +377,7 @@
this.etag = 0; this.etag = 0;
this.connectionEnabled = false; this.connectionEnabled = false;
this.opentimer = null; this.opentimer = null;
this.messagesQueue = [];
this.xhrSettings = { this.xhrSettings = {
url: null, url: null,
success: linker(this.onmessage, this), success: linker(this.onmessage, this),
...@@ -390,6 +391,7 @@ ...@@ -390,6 +391,7 @@
LongPollingWrapper.prototype = { LongPollingWrapper.prototype = {
connect: function() { connect: function() {
this.messagesQueue = [];
this._closeCurrentConnection(); this._closeCurrentConnection();
this.connectionEnabled = true; this.connectionEnabled = true;
this.xhrSettings.url = getSubscriberUrl(this.pushstream, this.pushstream.urlPrefixLongpolling); this.xhrSettings.url = getSubscriberUrl(this.pushstream, this.pushstream.urlPrefixLongpolling);
...@@ -399,11 +401,7 @@ ...@@ -399,11 +401,7 @@
}, },
_listen: function() { _listen: function() {
if (this.connectionEnabled) { if (this.connectionEnabled && !this.connection) {
if (this.connection) {
try { this.connection.abort(); } catch (e) { /* ignore error on closing */ }
this.connection = null;
}
this.connection = Ajax.load(this.xhrSettings); this.connection = Ajax.load(this.xhrSettings);
} }
}, },
...@@ -440,6 +438,7 @@ ...@@ -440,6 +438,7 @@
afterReceive: function(xhr) { afterReceive: function(xhr) {
this.etag = xhr.getResponseHeader('Etag'); this.etag = xhr.getResponseHeader('Etag');
this.lastModified = xhr.getResponseHeader('Last-Modified'); this.lastModified = xhr.getResponseHeader('Last-Modified');
this.connection = null;
}, },
onerror: function(status) { onerror: function(status) {
...@@ -456,15 +455,20 @@ ...@@ -456,15 +455,20 @@
onmessage: function(responseText) { onmessage: function(responseText) {
Log4js.info("[LongPolling] message received", responseText); Log4js.info("[LongPolling] message received", responseText);
this._listen();
var messages = responseText.split("\r\n"); var messages = responseText.split("\r\n");
for ( var i = 0; i < messages.length; i++) { for (var i = 0; i < messages.length; i++) {
var message = messages[i]; if (messages[i]) {
if (message) { this.messagesQueue.push(messages[i]);
var match = message.match((message.indexOf('"eventid":"') > 0) ? PATTERN_MESSAGE_WITH_EVENT_ID : PATTERN_MESSAGE);
this.pushstream._onmessage(match[3], match[1], match[2], match[4]);
} }
} }
this._listen();
while (this.messagesQueue.length > 0) {
var message = this.messagesQueue.shift();
var match = message.match((message.indexOf('"eventid":"') > 0) ? PATTERN_MESSAGE_WITH_EVENT_ID : PATTERN_MESSAGE);
this.pushstream._onmessage(match[3], match[1], match[2], match[4]);
}
} }
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment