I'm trying to implement a chat client using JavaScript. The client is constructed using the following constructor:
function ChatClient(endpointUrl) {
this.xmlHttp = createXmlHttpRequest();
this.endpointUrl = endpointUrl;
me = this;
setInterval('me.receiveMessages()', FETCH_MESSAGES_INTERVAL);
}
function createXmlHttpRequest() {
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/* @cc_on @ */
/*
* @if (@_jscript_version >= 5) try { xmlHttp = new
* ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new
* ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlHttp = false; } }
* @end @
*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
The chat client should be able to request messages from the server in a interval defined by the FETCH_MESSAGES_INTERVAL
. Here's the code:
ChatClient.prototype.receiveMessages = function() {
this.xmlHttp.open('GET', this.endpointUrl, true);
this.xmlHttp.onreadystatechange = this.handleReceiveMessagesResponse();
this.xmlHttp.send(null);
}
ChatClient.prototype.handleReceiveMessagesResponse = function() {
console.log("readyState = " + this.xmlHttp.readyState);
if (this.xmlHttp.readyState == 4) {
var rawResponse = this.xmlHttp.responseText;
document.getElementById('OutputArea').textContent = rawResponse;
}
}
The problem is that when the handleReceiveMessagesReponse is called, the FireBug cosole shows that this.xmlHttp.readyState is always 1 (loading). FireBug also shows that my GET requests are receiving the expect responses from the server (status 200 with the string 'Hello' as body). Does anyone knows what's wrong wrong with this code?