Hello,
I am writing an web app which needs to subscribe to multiple channels via javascript. I am using Atmosphere and Jersey as backend. However the jQuery plugin they work with only supports 1 channel. I've start buidling my own implementation. Now it works oke, but when i try to subscribe to 2 channels only 1 channel gets notified.
Is the XMLHttpRequest blocking the rest of the XMLHttpRequests?
Here's my code:
function AtmosphereComet(url)
{
this.Connected = new signals.Signal();
this.Disconnected = new signals.Signal();
this.NewMessage = new signals.Signal();
var xhr = null;
var self = this;
var gotWelcomeMessage = false;
var readPosition;
var url = url;
var onIncomingXhr = function()
{
if (xhr.readyState == 3)
{
if (xhr.status==200) // Received a message
{
var message = xhr.responseText;
console.log(message);
if(!gotWelcomeMessage && message.indexOf("") -1)
{
gotWelcomeMessage = true;
self.Connected.dispatch(sprintf("Connected to %s", url));
}
else
{
self.NewMessage.dispatch(message.substr(readPosition));
}
readPosition = this.responseText.length;
}
}
else if (this.readyState == 4)
{
self.disconnect();
}
}
var getXhr = function()
{
if ( window.location.protocol !== "file:" ) {
try {
return new window.XMLHttpRequest();
} catch(xhrError) {}
}
try {
return new window.ActiveXObject("Microsoft.XMLHTTP");
} catch(activeError) {}
}
this.connect = function()
{
xhr = getXhr();
xhr.onreadystatechange = onIncomingXhr;
xhr.open("GET", url, true);
xhr.send(null);
}
this.disconnect = function()
{
xhr.onreadystatechange = null;
xhr.abort();
}
this.send = function(message)
{
}
}
And the test code:
var connection1 = AtmosphereConnection("http://192.168.1.145:9999/botenveiling/b/product/status/2", AtmosphereComet);
var connection2 = AtmosphereConnection("http://192.168.1.145:9999/botenveiling/b/product/status/1", AtmosphereComet);
var output = function(msg)
{
alert(output);
};
connection1.NewMessage.add(output);
connection2.NewMessage.add(output);
connection1.connect();
In AtmosphereConnection I instantiate the given AtmosphereComet with "new". I iterate over the object to check if it has to methods: "send", "connect", "disconnect". The reason for this is that i can switch the implementation later on when i complete the websocket implementation :) However I think the problem rests with the XmlHttpRequest object, or am i mistaken?
P.S.: signals.Signal is a js observer/notifier library: http://millermedeiros.github.com/js-signals/
Testing:
Firefox 3.6.1.3