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 ...@@ -79,6 +79,7 @@ public class CometMessengerServlet extends HttpServlet implements CometProcessor
79 response.setHeader("X-Accel-Buffering", "No"); 79 response.setHeader("X-Accel-Buffering", "No");
80 if (event.getEventType() == CometEvent.EventType.BEGIN) { 80 if (event.getEventType() == CometEvent.EventType.BEGIN) {
81 Debug.logInfo("Begin for session: " + request.getSession(true).getId(), module); 81 Debug.logInfo("Begin for session: " + request.getSession(true).getId(), module);
82 event.getHttpServletRequest().setAttribute("org.apache.tomcat.comet.timeout", new Integer(0xFFFFFFFF));
82 PrintWriter writer = response.getWriter(); 83 PrintWriter writer = response.getWriter();
83 writer.println("{\"channel\":\"system\", \"message\":\"CONNECT\"}"); 84 writer.println("{\"channel\":\"system\", \"message\":\"CONNECT\"}");
84 writer.flush(); 85 writer.flush();
......
1 define([], function() { 1 define([], function() {
2 var listeners = []; 2 var channels = {};
3 var messenger = { 3 var messenger = {
4 process: function() { 4 process: function() {
5 var self = this;
5 var url = "/comet/messenger" 6 var url = "/comet/messenger"
6 var request = new XMLHttpRequest(); 7 var request = new XMLHttpRequest();
7 var responseLength = 0; 8 var responseLength = 0;
...@@ -10,22 +11,19 @@ define([], function() { ...@@ -10,22 +11,19 @@ define([], function() {
10 request.onreadystatechange = function() { 11 request.onreadystatechange = function() {
11 if (request.readyState == 3 || request.readyState == 4) { 12 if (request.readyState == 3 || request.readyState == 4) {
12 if (request.status == 200){ 13 if (request.status == 200){
13 if (request.onreadystatechange) {
14 request.onreadystatechange = null;
15 }
16 if (request.responseText) { 14 if (request.responseText) {
17 var newText = request.responseText.substr(responseLength); 15 var newText = request.responseText.substr(responseLength);
18 if (newText.length > 0) { 16 if (newText.length > 0) {
19 for (var i=0; i < listeners.length; i++) { 17 self.deliver(newText);
20 listeners[i](newText);
21 }
22 } 18 }
23 responseLength = request.responseText.length; 19 responseLength = request.responseText.length;
24 } 20 }
25 } 21 }
26 if (request.readyState == 4) { 22 if (request.readyState == 4) {
23 // Request is complete. Remove existing callback.
24 request.onreadystatechange = null;
27 if (request.status == 200) { 25 if (request.status == 200) {
28 messenger.process(); 26 setTimeout(function() { messenger.process(); }, 500);
29 } else { 27 } else {
30 console.log('Error, retrying in 5 seconds'); 28 console.log('Error, retrying in 5 seconds');
31 setTimeout(function() { messenger.process(); }, 5000); 29 setTimeout(function() { messenger.process(); }, 5000);
...@@ -35,14 +33,33 @@ define([], function() { ...@@ -35,14 +33,33 @@ define([], function() {
35 }; 33 };
36 request.send(null); 34 request.send(null);
37 }, 35 },
36 deliver: function(newText) {
37 try {
38 var json = JSON.parse(newText);
39
40 if (json.channel && channels[json.channel]) {
41 for (var i=0; i < channels[json.channel].length; i++) {
42 channels[json.channel][i](json);
43 }
44 }
45 } catch (err) {
46 console.log('Error processing COMETd message', newText, err);
47 }
48 },
38 addListener: function(listener) { 49 addListener: function(listener) {
39 listeners.push(listener); 50 this.addListener('ALL', listener);
40 }, 51 },
41 removeListener: function(listener) { 52 addListener: function(channel, listener) {
42 var index = listeners.indexOf(listener); 53 if (!channels[channel]) {
43 if (index > -1) { 54 channels[channel] = [];
44 array.splice(index, 1);
45 } 55 }
56 channels[channel].push(listener);
57 },
58 removeListener: function(listener) {
59 this.removeListener('ALL', listener);
60 },
61 removeListener: function(channel, listener) {
62 console.log('Remove listener from channel', listener, channel);
46 } 63 }
47 } 64 }
48 messenger.process(); 65 messenger.process();
......