github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/integration/messagebus/glue/client/src/ajaxsocket.js (about)

     1  /*
     2   *  Glue - Robust Go and Javascript Socket Library
     3   *  Copyright (C) 2015  Roland Singer <roland.singer[at]desertbit.com>
     4   *
     5   *  This program is free software: you can redistribute it and/or modify
     6   *  it under the terms of the GNU General Public License as published by
     7   *  the Free Software Foundation, either version 3 of the License, or
     8   *  (at your option) any later version.
     9   *
    10   *  This program is distributed in the hope that it will be useful,
    11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   *  GNU General Public License for more details.
    14   *
    15   *  You should have received a copy of the GNU General Public License
    16   *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   */
    18  
    19  /*
    20   *  This code lives inside the glue function.
    21   */
    22  
    23  
    24  var newAjaxSocket = function () {
    25      /*
    26       * Constants
    27       */
    28  
    29      var ajaxHost = host + options.baseURL + "ajax",
    30          sendTimeout = 8000,
    31          pollTimeout = 45000;
    32  
    33      var PollCommands = {
    34          Timeout:    "t",
    35          Closed:     "c"
    36      };
    37  
    38      var Commands = {
    39          Delimiter:  "&",
    40          Init:       "i",
    41          Push:       "u",
    42          Poll:       "o"
    43      };
    44  
    45  
    46  
    47      /*
    48       * Variables
    49       */
    50  
    51      var s = {},
    52          uid, pollToken,
    53          pollXhr = false,
    54          sendXhr = false,
    55          poll;
    56  
    57  
    58  
    59      /*
    60       * Methods
    61       */
    62  
    63      var stopRequests = function() {
    64          // Set the poll function to a dummy function.
    65          // This will prevent further poll calls.
    66          poll = function() {};
    67  
    68          // Kill the ajax requests.
    69          if (pollXhr) {
    70              pollXhr.abort();
    71          }
    72          if (sendXhr) {
    73              sendXhr.abort();
    74          }
    75      };
    76  
    77      var postAjax = function(url, timeout, data, success, error) {
    78          var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    79  
    80          xhr.onload = function() {
    81            success(xhr.response);
    82          };
    83  
    84          xhr.onerror = function() {
    85            error();
    86          };
    87  
    88          xhr.ontimeout = function() {
    89            error("timeout");
    90          };
    91  
    92          xhr.open('POST', url, true);
    93          xhr.responseType = "text";
    94          xhr.timeout = timeout;
    95          xhr.send(data);
    96  
    97          return xhr;
    98      };
    99  
   100      var triggerClosed = function() {
   101          // Stop the ajax requests.
   102          stopRequests();
   103  
   104          // Trigger the event.
   105          s.onClose();
   106      };
   107  
   108      var triggerError = function(msg) {
   109          // Stop the ajax requests.
   110          stopRequests();
   111  
   112          // Create the error message.
   113          if (msg) {
   114              msg = "the ajax socket closed the connection with the error: " + msg;
   115          }
   116          else {
   117              msg = "the ajax socket closed the connection with an error.";
   118          }
   119  
   120          // Trigger the event.
   121          s.onError(msg);
   122      };
   123  
   124      var send = function (data, callback) {
   125          sendXhr = postAjax(ajaxHost, sendTimeout, data, function (data) {
   126              sendXhr = false;
   127  
   128              if (callback) {
   129                  callback(data);
   130              }
   131          }, function (msg) {
   132              sendXhr = false;
   133              triggerError(msg);
   134          });
   135      };
   136  
   137      poll = function () {
   138          var data = Commands.Poll + uid + Commands.Delimiter + pollToken;
   139  
   140          pollXhr = postAjax(ajaxHost, pollTimeout, data, function (data) {
   141            pollXhr = false;
   142  
   143            // Check if this jax request has reached the server's timeout.
   144            if (data == PollCommands.Timeout) {
   145                // Just start the next poll request.
   146                poll();
   147                return;
   148            }
   149  
   150            // Check if this ajax connection was closed.
   151            if (data == PollCommands.Closed) {
   152                // Trigger the closed event.
   153                triggerClosed();
   154                return;
   155            }
   156  
   157            // Split the new token from the rest of the data.
   158            var i = data.indexOf(Commands.Delimiter);
   159            if (i < 0) {
   160                triggerError("ajax socket: failed to split poll token from data!");
   161                return;
   162            }
   163  
   164            // Set the new token and the data variable.
   165            pollToken = data.substring(0, i);
   166            data = data.substr(i + 1);
   167  
   168            // Start the next poll request.
   169            poll();
   170  
   171            // Call the event.
   172            s.onMessage(data);
   173          }, function (msg) {
   174              pollXhr = false;
   175              triggerError(msg);
   176          });
   177      };
   178  
   179  
   180  
   181      /*
   182       * Socket layer implementation.
   183       */
   184  
   185      s.open = function () {
   186          // Initialize the ajax socket session
   187          send(Commands.Init, function (data) {
   188              // Get the uid and token string
   189              var i = data.indexOf(Commands.Delimiter);
   190              if (i < 0) {
   191                  triggerError("ajax socket: failed to split uid and poll token from data!");
   192                  return;
   193              }
   194  
   195              // Set the uid and token.
   196              uid = data.substring(0, i);
   197              pollToken = data.substr(i + 1);
   198  
   199              // Start the long polling process.
   200              poll();
   201  
   202              // Trigger the event.
   203              s.onOpen();
   204          });
   205      };
   206  
   207      s.send = function (data) {
   208          // Always prepend the command with the uid to the data.
   209          send(Commands.Push + uid + Commands.Delimiter + data);
   210      };
   211  
   212  	s.reset = function() {
   213          // Stop the ajax requests.
   214          stopRequests();
   215      };
   216  
   217  	return s;
   218  };