Class: GDSocket

GDSocket()

Implements the secure Socket communications APIs.

Constructor

new GDSocket()

Properties:
Name Type Description
url string The address of the server. Can be either an Internet Protocol address (IP address, for example "192.168.1.10"), or a fully qualified domain name (for example "www.example.com").
port number Number of the server port to which the socket will connect.
useSSL boolean This value determines whether or not to use SSL/TLS security.
payloadMessageType string Optional parameter that represents the type of payload message of responseData property in "message" event. Possible values are "string" and "binary". "string" value is by default.
disableHostVerification boolean Disable host name verification, when making an HTTPS request. Host name verification is an SSL/TLS security option.
disablePeerVerification boolean Disable certificate authenticity verification, when making an SSL/TLS connection. Authenticity verification is an SSL/TLS security option.
onSocketResponse function This function is the callback handler that is called whenever a response is returned from the socket connection. This function should check the value of the responseType returned and determine the required action to take. If the responseType = "open", then the socketID returned in the response should be used to send data in subsequent calls for this socket connection (see GDSocket.send). NOTE: This function is required to be a non-null value.
onSocketError function This function is the callback handler that is called whenever a socket error occurs. This function should check the value of the responseType returned and determine the required action to take.
Source:

Methods

close(socketID)

Call this function to close the socket connection. send() and close() methods work asynchronously. This means that good place to close the connection is when data is received in "message" event.
Parameters:
Name Type Description
socketID string The identifier for the open socket connection. This value is returned from a successful call to GDSocket.connect.
Source:
Example
See the example below (it is added to GDSocket.send() method).

connect() → {GDSocketResponse}

Open a new socket connection.
Source:
Returns:
A socket response object in JSON format. The result should be parsed and saved as a GDSocketResponse object in the callback handler. If the connection was successful then the response object will be initialize with a socketID property that can be used to send data using this socket connection (see GDSocket.send). Since this is an asynchronous call, the response will be returned via the onSocketResponse callback or the onSocketError callback (whichever is applicable).
Type
GDSocketResponse
Example
var mySocketStreamBinary = function () {
  if(console)
     console.log('>> Socket');

  var url = '10.0.2.2'; // this ip is the ip used by the android studio emulator to connect to it's host.
  var port = '11511';
  var useSSL = false;

  var aSocket = window.plugins.GDSocket.createSocket(url, port, useSSL, 'binary'); // payloadMessageType is 'binary' in this case

  aSocket.onSocketResponse = function (obj) {
    var socketResponse = window.plugins.GDSocket.parseSocketResponse(obj);
    if(console)
      console.log (socketResponse.responseType)

    switch (socketResponse.responseType) {
      case 'open':
        break;

      case 'message':
        if(console)
             console.log('message:' , socketResponse.responseData); // Uint8Array data is going to be here because of 'binary' value of payloadMessageType
        break;

      case 'error':
        console.log('Received an error status from the socket connection.');
        break;

      case 'close':
        console.log('Socket connection closed successfully.');
        break;

      default:
        console.log('Unknown Socket response type: ' + socketResponse.responseType);
   }
  };
  // error
  aSocket.onSocketError = function (error) {
     if(console)
      console.log('The socket connection failed: ' + error);
  };

  // connect!
  aSocket.connect();
};

var mySocketStreamString = function () {
  if(console)
     console.log('>> Socket');

  var url = '10.0.2.2'; // this ip is the ip used by the android studio emulator to connect to it's host.
  var port = '11511';
  var useSSL = false;

  var aSocket = window.plugins.GDSocket.createSocket(url, port, useSSL); // payloadMessageType is 'string' in this case by default

  aSocket.onSocketResponse = function (obj) {
    var socketResponse = window.plugins.GDSocket.parseSocketResponse(obj);
    if(console)
      console.log (socketResponse.responseType)

    switch (socketResponse.responseType) {
      case 'open':
        break;

      case 'message':
        if(console)
             console.log('message:' , socketResponse.responseData); // String data is going to be here because 'string' is a default value of payloadMessageType
        break;

      case 'error':
        console.log('Received an error status from the socket connection.');
        break;

      case 'close':
        console.log('Socket connection closed successfully.');
        break;

      default:
        console.log('Unknown Socket response type: ' + socketResponse.responseType);
   }
  };
  // error
  aSocket.onSocketError = function (error) {
     if(console)
      console.log('The socket connection failed: ' + error);
  };

  // connect!
  aSocket.connect();
};

createSocket(url, port, useSSL, payloadMessageType, streamMode) → {GDSocket}

Call this function to create a socket and set the main parameters. NOTE: This funtion only initializes the socket parameters; it does not initiate data transfer nor does it make the initial socket connection (see GDSocket.connect).
Parameters:
Name Type Description
url string The address of the server. Can be either an Internet Protocol address (IP address, for example "192.168.1.10"), or a fully qualified domain name (for example "www.example.com").
port number Number of the server port to which the socket will connect.
useSSL boolean his value determines whether or not to use SSL/TLS security.
payloadMessageType string Optional parameter that represents the type of payload message of responseData property in "message" event. Possible values are "string" and "binary". "string" value is by default.
streamMode boolean Optional parameter that indicates if socket is created in stream mode. true value is by default.
Source:
Returns:
Type
GDSocket
Example
See the example below (it is added to GDSocket.send() method).

parseHttpResponse(responseText) → {Object}

Call this function to transform the HTTP response text from GDSocket into an object. Applicable only in case of 'string' payloadMessageType. Following properties are available:
  • status - status string, for example, 'HTTP/1.1 200 OK'.
  • statusCode - status code number, for example, 200.
  • responseHeaders - an array of response headers, each array item is itself an object where key is response header name and value is response header value.
  • responseBody - response body, depending on content type response body will be different.
Parameters:
Name Type Description
responseText string A string representing the HTTP response text from GDSocket.
Source:
Returns:
The socket HTTP response object.
Type
Object
Example
See the example below (it is added to GDSocket.send() method).

parseSocketResponse(responseText) → {GDSocketResponse}

Call this function to transform the socket response text into a GDSocketResponse object.
Parameters:
Name Type Description
responseText string A string representing the socket response text.
Source:
Returns:
The socket response object.
Type
GDSocketResponse
Example
See the example below (it is added to GDSocket.send() method).

send(data, socketID)

Call this function to send data using the open socket connection. send() method works asynchronously. This means that good place to close the connection is when data is received in "message" event. If socket is not closed connections is kept alive waiting for new data coming.
Parameters:
Name Type Description
data string | UInt8Array Optional parameters that indicates what data will be transmitted using the open socket.
socketID string The identifier for the open socket connection. This value is returned from a successful call to GDSocket.connect.
Source:
Example
function mySocketSend(){
var url = "httpbin.org";
var aSocket = window.plugins.GDSocket.createSocket(url, 80, true, 'string', false); // payloadMessageType is by default 'string' in this case

aSocket.onSocketResponse = function (obj) {
    var socketResponse = aSocket.parseSocketResponse(obj);

    switch (socketResponse.responseType) {
        case "open":
            var httpRequest = "GET / HTTP/1.1\r\n" +
                "Host: httpbin.org:80\r\n" +
                "Connection: close\r\n" +
                "\r\n";

            aSocket.send(socketResponse.socketID, httpRequest);
            break;

        case "message":
            var httpRespObj = aSocket.parseHttpResponse(socketResponse.responseData);
            console.log(httpRespObj.status)
            console.log(httpRespObj.statusCode)
            console.log(httpRespObj.responseHeaders)
            console.log(httpRespObj.responseBody)
            aSocket.close(socketResponse.socketID);
            break;

        case "error":
            // handle error
            break;

        case "close":
            console.log("Socket connection closed");
            break;

        default:
            // handle default case
    }
};

// error
aSocket.onSocketError = function (error) {
    // handle error
};

// connect!
aSocket.connect();

}