github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/integration/messagebus/glue/client/src/websocket.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 newWebSocket = function () {
    25      /*
    26       * Variables
    27       */
    28  
    29      var s = {},
    30          ws;
    31  
    32  
    33  
    34      /*
    35       * Socket layer implementation.
    36       */
    37  
    38      s.open = function () {
    39          try {
    40              // Generate the websocket url.
    41              var url;
    42              if (host.match("^https://")) {
    43                  url = "wss" + host.substr(5);
    44              } else {
    45                  url = "ws" + host.substr(4);
    46              }
    47              url += options.baseURL + "ws";
    48  
    49              // Open the websocket connection
    50              ws = new WebSocket(url);
    51  
    52              // Set the callback handlers
    53              ws.onmessage = function(event) {
    54                  s.onMessage(event.data.toString());
    55              };
    56  
    57              ws.onerror = function(event) {
    58                  var msg = "the websocket closed the connection with ";
    59                  if (event.code) {
    60                      msg += "the error code: " + event.code;
    61                  }
    62                  else {
    63                      msg += "an error.";
    64                  }
    65  
    66                  s.onError(msg);
    67              };
    68  
    69              ws.onclose = function() {
    70                  s.onClose();
    71              };
    72  
    73              ws.onopen = function() {
    74                  s.onOpen();
    75              };
    76          } catch (e) {
    77              s.onError();
    78          }
    79      };
    80  
    81      s.send = function (data) {
    82          // Send the data to the server
    83          ws.send(data);
    84      };
    85  
    86  	s.reset = function() {
    87          // Close the websocket if defined.
    88          if (ws) {
    89              ws.close();
    90          }
    91  
    92          ws = undefined;
    93      };
    94  
    95  	return s;
    96  };