Commit 73100936 authored by Wandenberg Peixoto's avatar Wandenberg Peixoto

using common callback on javascript library

parent c634d747
...@@ -27,8 +27,8 @@ ...@@ -27,8 +27,8 @@
/* prevent duplicate declaration */ /* prevent duplicate declaration */
if (window.PushStream) { return; } if (window.PushStream) { return; }
var PATTERN_MESSAGE = /\{\"id\":(\d*),\"channel\":\"(.*)\",\"text\":\"(.*)\"\}/; var PATTERN_MESSAGE = /\{\"id\":([\-\d]*),\"channel\":\"(.*)\",\"text\":\"(.*)\"\}/;
var PATTERN_MESSAGE_WITH_EVENT_ID = /\{\"id\":(\d*),\"channel\":\"(.*)\",\"text\":\"(.*)\",\"eventid\":\"(.*)\"\}/; var PATTERN_MESSAGE_WITH_EVENT_ID = /\{\"id\":([\-\d]*),\"channel\":\"(.*)\",\"text\":\"(.*)\",\"eventid\":\"(.*)\"\}/;
var streamWrappersCount = 0; var streamWrappersCount = 0;
...@@ -135,24 +135,44 @@ ...@@ -135,24 +135,44 @@
return null; return null;
} }
/* common callbacks */
var onmessageCallback = function(event) {
Log4js.info("[" + this.type + "] message received", arguments);
var match = event.data.match((event.data.indexOf('"eventid":"') > 0) ? PATTERN_MESSAGE_WITH_EVENT_ID : PATTERN_MESSAGE);
this.pushstream._onmessage(match[3], match[1], match[2], match[4]);
};
var onopenCallback = function() {
this.pushstream._onopen();
Log4js.info("[" + this.type + "] connection opened");
};
var onerrorCallback = function(event) {
Log4js.info("[" + this.type + "] error (disconnected by server):", event);
this._closeCurrentConnection();
this.pushstream._onerror({type: ((event && (event.type === "load")) || (this.pushstream.readyState === PushStream.CONNECTING)) ? "load" : "timeout"});
}
/* wrappers */ /* wrappers */
var EventSourceWrapper = function(pushstream) { var EventSourceWrapper = function(pushstream) {
if (!window.EventSource) throw "EventSource not supported"; if (!window.EventSource) throw "EventSource not supported";
this.type = "eventsource"; this.type = EventSourceWrapper.TYPE;
this.pushstream = pushstream; this.pushstream = pushstream;
this.connection = null; this.connection = null;
}; };
EventSourceWrapper.TYPE = "EventSource";
EventSourceWrapper.prototype = { EventSourceWrapper.prototype = {
connect: function() { connect: function() {
this._closeCurrentConnection(); this._closeCurrentConnection();
var url = getSubscriberUrl(this.pushstream, this.pushstream.urlPrefixEventsource); var url = getSubscriberUrl(this.pushstream, this.pushstream.urlPrefixEventsource);
this.connection = new window.EventSource(url); this.connection = new window.EventSource(url);
this.connection.onerror = linker(this.onerror, this); this.connection.onerror = linker(onerrorCallback, this);
this.connection.onopen = linker(this.onopen, this); this.connection.onopen = linker(onopenCallback, this);
this.connection.onmessage = linker(this.onmessage, this); this.connection.onmessage = linker(onmessageCallback, this);
Log4js.debug("[EventSource] connecting to:", url); Log4js.info("[EventSource] connecting to:", url);
}, },
disconnect: function() { disconnect: function() {
...@@ -168,28 +188,11 @@ ...@@ -168,28 +188,11 @@
try { this.connection.close(); } catch (e) { /* ignore error on closing */ } try { this.connection.close(); } catch (e) { /* ignore error on closing */ }
this.connection = null; this.connection = null;
} }
},
onerror: function(event) {
Log4js.info("[EventSource] error (disconnected by server):", event);
this._closeCurrentConnection();
this.pushstream._onerror({type: (this.pushstream.readyState === PushStream.CONNECTING) ? "load" : "timeout"});
},
onopen: function() {
this.pushstream._onopen();
Log4js.info("[EventSource] connection opened");
},
onmessage: function(event) {
Log4js.info("[EventSource] message received", arguments);
var match = event.data.match((event.data.indexOf('"eventid":"') > 0) ? PATTERN_MESSAGE_WITH_EVENT_ID : PATTERN_MESSAGE);
this.pushstream._onmessage(match[3], match[1], match[2], match[4]);
} }
}; };
var StreamWrapper = function(pushstream) { var StreamWrapper = function(pushstream) {
this.type = "stream"; this.type = StreamWrapper.TYPE;
this.pushstream = pushstream; this.pushstream = pushstream;
this.connection = null; this.connection = null;
this.url = null; this.url = null;
...@@ -199,6 +202,8 @@ ...@@ -199,6 +202,8 @@
window[this.streamId] = this; window[this.streamId] = this;
}; };
StreamWrapper.TYPE = "Stream";
StreamWrapper.prototype = { StreamWrapper.prototype = {
connect: function() { connect: function() {
this._closeCurrentConnection(); this._closeCurrentConnection();
...@@ -244,7 +249,7 @@ ...@@ -244,7 +249,7 @@
transferDoc.appendChild(ifrDiv); transferDoc.appendChild(ifrDiv);
ifrDiv.innerHTML = "<iframe src=\""+url+"\"></iframe>"; ifrDiv.innerHTML = "<iframe src=\""+url+"\"></iframe>";
this.connection = ifrDiv.getElementsByTagName("IFRAME")[0]; this.connection = ifrDiv.getElementsByTagName("IFRAME")[0];
this.connection.onload = linker(this.frameerror, this); this.connection.onload = linker(onerrorCallback, this);
this.transferDoc = transferDoc; this.transferDoc = transferDoc;
} catch (e) { } catch (e) {
var ifr = document.createElement("IFRAME"); var ifr = document.createElement("IFRAME");
...@@ -258,10 +263,10 @@ ...@@ -258,10 +263,10 @@
ifr.PushStream = PushStream; ifr.PushStream = PushStream;
document.body.appendChild(ifr); document.body.appendChild(ifr);
ifr.setAttribute("src", url); ifr.setAttribute("src", url);
ifr.onload = linker(this.frameerror, this); ifr.onload = linker(onerrorCallback, this);
this.connection = ifr; this.connection = ifr;
} }
this.frameloadtimer = setTimeout(linker(this.frameerror, this), this.pushstream.timeout); this.frameloadtimer = setTimeout(linker(onerrorCallback, this), this.pushstream.timeout);
}, },
register: function(iframeWindow) { register: function(iframeWindow) {
...@@ -286,26 +291,14 @@ ...@@ -286,26 +291,14 @@
this.disconnect(); this.disconnect();
}, },
frameerror: function(event) {
Log4js.info("[Stream] " + (event && (event.type === "load")) ? "frame loaded whitout streaming" : "frame load timeout");
this._closeCurrentConnection();
this.pushstream._onerror({type: (event && (event.type === "load")) ? "load" : "timeout"});
},
pingerror: function() {
Log4js.info("[Stream] ping timeout");
this._closeCurrentConnection();
this.pushstream._onerror({type: "timeout"});
},
setPingTimer: function() { setPingTimer: function() {
if (this.pingtimer) clearTimer(this.pingtimer); if (this.pingtimer) clearTimer(this.pingtimer);
this.pingtimer = setTimeout(linker(this.pingerror, this), this.pushstream.pingtimeout); this.pingtimer = setTimeout(linker(onerrorCallback, this), this.pushstream.pingtimeout);
} }
}; };
var LongPollingWrapper = function(pushstream) { var LongPollingWrapper = function(pushstream) {
this.type = "longpolling"; this.type = LongPollingWrapper.TYPE;
this.pushstream = pushstream; this.pushstream = pushstream;
this.connection = null; this.connection = null;
this.lastModified = null; this.lastModified = null;
...@@ -320,13 +313,14 @@ ...@@ -320,13 +313,14 @@
} }
}; };
LongPollingWrapper.TYPE = "LongPolling";
LongPollingWrapper.prototype = { LongPollingWrapper.prototype = {
connect: function() { connect: function() {
this._closeCurrentConnection(); this._closeCurrentConnection();
this.connectionEnabled = true; this.connectionEnabled = true;
this._listen(); this._listen();
this.onopen(); Log4js.info("[LongPolling] connecting to:", this.xhrSettings.url);
Log4js.debug("[LongPolling] connecting to:", this.xhrSettings.url);
}, },
_listen: function() { _listen: function() {
...@@ -373,6 +367,7 @@ ...@@ -373,6 +367,7 @@
if (this.connectionEnabled) { /* abort(), called by disconnect(), call this callback, but should be ignored */ if (this.connectionEnabled) { /* abort(), called by disconnect(), call this callback, but should be ignored */
if (status === 304) { if (status === 304) {
this._listen(); this._listen();
if (this.pushstream.readyState === PushStream.CONNECTING) onopenCallback.apply(this);
} else { } else {
Log4js.info("[LongPolling] error (disconnected by server):", status); Log4js.info("[LongPolling] error (disconnected by server):", status);
this._closeCurrentConnection(); this._closeCurrentConnection();
...@@ -381,13 +376,9 @@ ...@@ -381,13 +376,9 @@
} }
}, },
onopen: function() {
this.pushstream._onopen();
Log4js.info("[LongPolling] connection opened");
},
onmessage: function(responseText) { onmessage: function(responseText) {
Log4js.info("[LongPolling] message received", responseText); Log4js.info("[LongPolling] message received", responseText);
if (this.pushstream.readyState === PushStream.CONNECTING) onopenCallback.apply(this);
this._listen(); 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++) {
...@@ -433,7 +424,7 @@ ...@@ -433,7 +424,7 @@
switch (this.modes[i]) { switch (this.modes[i]) {
case "eventsource": wrapper = new EventSourceWrapper(this); break; case "eventsource": wrapper = new EventSourceWrapper(this); break;
case "longpolling": wrapper = new LongPollingWrapper(this); break; case "longpolling": wrapper = new LongPollingWrapper(this); break;
default: wrapper = new StreamWrapper(this); break; case "stream" : wrapper = new StreamWrapper(this); break;
} }
this.wrappers[this.wrappers.length] = wrapper; this.wrappers[this.wrappers.length] = wrapper;
} catch(e) { Log4js.info(e); } } catch(e) { Log4js.info(e); }
...@@ -568,7 +559,7 @@ ...@@ -568,7 +559,7 @@
_reconnect: function(timeout) { _reconnect: function(timeout) {
if (this._keepConnected && !this.reconnecttimer && (this.readyState != PushStream.CONNECTING)) { if (this._keepConnected && !this.reconnecttimer && (this.readyState != PushStream.CONNECTING)) {
Log4js.debug("trying to reconnect in", timeout); Log4js.info("trying to reconnect in", timeout);
this.reconnecttimer = setTimeout(linker(this._connect, this), timeout); this.reconnecttimer = setTimeout(linker(this._connect, this), timeout);
} }
} }
......
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