O'Reilly logo

Getting Started with NativeScript by Nathanael J. Anderson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Communication with the server

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required