6cb0abfd by Ean Schuessler

Merge branch 'BF-5084' of /home/git/repositories/brainfood/ofbiz-comet-messenger

2 parents b8a609a2 565ebb2e
......@@ -79,6 +79,7 @@ public class CometMessengerServlet extends HttpServlet implements CometProcessor
response.setHeader("X-Accel-Buffering", "No");
if (event.getEventType() == CometEvent.EventType.BEGIN) {
Debug.logInfo("Begin for session: " + request.getSession(true).getId(), module);
event.getHttpServletRequest().setAttribute("org.apache.tomcat.comet.timeout", new Integer(0xFFFFFFFF));
PrintWriter writer = response.getWriter();
writer.println("{\"channel\":\"system\", \"message\":\"CONNECT\"}");
writer.flush();
......
define([], function() {
var listeners = [];
var channels = {};
var messenger = {
process: function() {
var self = this;
var url = "/comet/messenger"
var request = new XMLHttpRequest();
var responseLength = 0;
......@@ -10,22 +11,19 @@ define([], function() {
request.onreadystatechange = function() {
if (request.readyState == 3 || request.readyState == 4) {
if (request.status == 200){
if (request.onreadystatechange) {
request.onreadystatechange = null;
}
if (request.responseText) {
var newText = request.responseText.substr(responseLength);
if (newText.length > 0) {
for (var i=0; i < listeners.length; i++) {
listeners[i](newText);
}
self.deliver(newText);
}
responseLength = request.responseText.length;
}
}
if (request.readyState == 4) {
// Request is complete. Remove existing callback.
request.onreadystatechange = null;
if (request.status == 200) {
messenger.process();
setTimeout(function() { messenger.process(); }, 500);
} else {
console.log('Error, retrying in 5 seconds');
setTimeout(function() { messenger.process(); }, 5000);
......@@ -35,14 +33,33 @@ define([], function() {
};
request.send(null);
},
deliver: function(newText) {
try {
var json = JSON.parse(newText);
if (json.channel && channels[json.channel]) {
for (var i=0; i < channels[json.channel].length; i++) {
channels[json.channel][i](json);
}
}
} catch (err) {
console.log('Error processing COMETd message', newText, err);
}
},
addListener: function(listener) {
listeners.push(listener);
this.addListener('ALL', listener);
},
removeListener: function(listener) {
var index = listeners.indexOf(listener);
if (index > -1) {
array.splice(index, 1);
addListener: function(channel, listener) {
if (!channels[channel]) {
channels[channel] = [];
}
channels[channel].push(listener);
},
removeListener: function(listener) {
this.removeListener('ALL', listener);
},
removeListener: function(channel, listener) {
console.log('Remove listener from channel', listener, channel);
}
}
messenger.process();
......