The code we use will use to talk to the server is the nonvisual component XMLHttpRequest. This component is actually already included in the global namespace, since that is where most normal JavaScript expects it. We will create a simple class wrapper around it to communicate with the server. The first part of the class is just the initialization:
var AJAXSocket = function() { this._host = null; this._counter = 0; this._clientId = Math.floor(Math.random()*100000); this._from = "Unknown_"+this._clientId; this._messageHandler = []; var self = this; setInterval(function () { self._handleListener(); }, 250); }; exports.AJAXSocket = AJAXSocket;
As you can see, we set up a couple of internal variables, and then we made a listener
function fire every 250 milliseconds to ask the server for any new updates. This is not very battery or bandwidth efficient, but with some work, it could be made a lot smarter. Then, we made sure to assign this class to...