chat4app-client-angular 1.0.18 → 1.0.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1779,7 +1779,7 @@
1779
1779
  this.debug = false;
1780
1780
  this.chatUserExternalId = "";
1781
1781
  this.configId = "";
1782
- this.timeoutReconnect = 500;
1782
+ this.isConnected = false;
1783
1783
  }
1784
1784
  Chat4appClientAngularService.prototype.ngOnInit = function () {
1785
1785
  };
@@ -1804,7 +1804,7 @@
1804
1804
  if (chatCallback && connectionClientId) {
1805
1805
  _this.chatUserExternalId = connectionClientId;
1806
1806
  _this.chatCallback = chatCallback;
1807
- _this.connectWebsocket();
1807
+ _this.setIntervalConnection();
1808
1808
  }
1809
1809
  subscriber.next(securityChatToken);
1810
1810
  subscriber.complete();
@@ -2104,41 +2104,50 @@
2104
2104
  };
2105
2105
  Chat4appClientAngularService.prototype.ensureConn = function () {
2106
2106
  var _this = this;
2107
- if (this.debug) {
2108
- console.log("chatServer: " + this.chatServer);
2109
- console.log("userId: " + this.chatUserExternalId);
2110
- console.log("getBearerToken: " + this.getBearerToken());
2111
- }
2112
- var that = this;
2113
- var uriStep1 = this.chatServer.replace(/^https/, 'wss');
2114
- var server = uriStep1.replace(/(\/\/.*?)\//, '$1\/ws/');
2115
- if (this.debug) {
2116
- console.log("ws server: " + server);
2117
- }
2118
- that.disconnectInternal(true);
2119
- nats_min.connect({ url: server, user: this.chatUserExternalId, pass: this.getBearerToken(), reconnect: false })
2120
- .then(function (nc) {
2121
- that.nc = nc;
2122
- var subject = "chat4app.config." + _this.configId + ".user." + _this.chatUserExternalId;
2123
- that.nc.subscribe(subject, function (msg) {
2107
+ if (!this.isConnected) {
2108
+ var that_1 = this;
2109
+ this.configId = this.chatServer.split("/")[3];
2110
+ if (this.debug) {
2111
+ console.log("chatServer: " + this.chatServer);
2112
+ console.log("userId: " + this.chatUserExternalId);
2113
+ console.log("getBearerToken: " + this.getBearerToken());
2114
+ }
2115
+ try {
2116
+ if (this.nc) {
2117
+ this.nc.close();
2118
+ }
2119
+ }
2120
+ catch (e) {
2121
+ }
2122
+ var uriStep1 = this.chatServer.replace(/^https/, 'wss');
2123
+ var server = uriStep1.replace(/(\/\/.*?)\//, '$1\/ws/');
2124
+ if (this.debug) {
2125
+ console.log("ws server: " + server);
2126
+ }
2127
+ nats_min.connect({ url: server, user: this.chatUserExternalId, pass: this.getBearerToken(), reconnect: false })
2128
+ .then(function (nc) {
2129
+ that_1.nc = nc;
2130
+ that_1.isConnected = true;
2131
+ var subject = "chat4app.config." + _this.configId + ".user." + _this.chatUserExternalId;
2132
+ that_1.nc.subscribe(subject, function (msg) {
2133
+ if (_this.debug) {
2134
+ console.log(msg);
2135
+ }
2136
+ _this.processAndCallback(JSON.parse(msg.data));
2137
+ });
2124
2138
  if (_this.debug) {
2125
- console.log(msg);
2139
+ console.log("Subscribe online chat4app.user." + _this.chatUserExternalId + ".online");
2126
2140
  }
2127
- _this.processAndCallback(JSON.parse(msg.data));
2141
+ })
2142
+ .catch(function (err) {
2143
+ // Notifica o chamador sobre o erro na conexão
2144
+ console.log("NATS Error:");
2145
+ console.log(err);
2146
+ that_1.isConnected = false;
2128
2147
  });
2129
- _this.setIntervalPing();
2130
- if (_this.debug) {
2131
- console.log("Subscribe online chat4app.user." + _this.chatUserExternalId + ".online");
2132
- }
2133
- })
2134
- .catch(function (err) {
2135
- // Notifica o chamador sobre o erro na conexão
2136
- console.log("NATS Error:");
2137
- console.log(err);
2138
- setTimeout(_this.ensureConn, 1000);
2139
- });
2148
+ }
2140
2149
  };
2141
- Chat4appClientAngularService.prototype.setIntervalPing = function () {
2150
+ Chat4appClientAngularService.prototype.setIntervalConnection = function () {
2142
2151
  var _this = this;
2143
2152
  if (this.interval) {
2144
2153
  try {
@@ -2148,6 +2157,7 @@
2148
2157
  }
2149
2158
  }
2150
2159
  this.interval = setInterval(function () {
2160
+ _this.ensureConn();
2151
2161
  if (_this.debug) {
2152
2162
  console.log("publish chat4app.user.online " + _this.chatUserExternalId + " ");
2153
2163
  }
@@ -2155,17 +2165,14 @@
2155
2165
  _this.nc.publish("chat4app.user.online", _this.chatUserExternalId);
2156
2166
  }
2157
2167
  catch (e) {
2168
+ _this.isConnected = false;
2158
2169
  if (_this.debug) {
2159
2170
  console.log("try reconnect...");
2160
2171
  }
2161
- _this.ensureConn();
2162
2172
  }
2163
2173
  }, 1000);
2164
2174
  };
2165
2175
  Chat4appClientAngularService.prototype.disconnect = function () {
2166
- return this.disconnectInternal(false);
2167
- };
2168
- Chat4appClientAngularService.prototype.disconnectInternal = function (ignoreOffLine) {
2169
2176
  // Check how we can disconnect
2170
2177
  if (this.interval) {
2171
2178
  try {
@@ -2174,7 +2181,7 @@
2174
2181
  catch (e) {
2175
2182
  }
2176
2183
  }
2177
- if (!ignoreOffLine && this.nc && this.chatUserExternalId && this.chatUserExternalId.length > 0 && this.getBearerToken() && this.getBearerToken().length > 0) {
2184
+ if (this.nc && this.chatUserExternalId && this.chatUserExternalId.length > 0 && this.getBearerToken() && this.getBearerToken().length > 0) {
2178
2185
  try {
2179
2186
  this.setOffLine(this.chatUserExternalId).toPromise();
2180
2187
  if (this.debug)
@@ -2236,20 +2243,6 @@
2236
2243
  }
2237
2244
  //}
2238
2245
  };
2239
- Chat4appClientAngularService.prototype.connectWebsocket = function () {
2240
- var that = this;
2241
- this.disconnectInternal(true);
2242
- var securityToken = this.getBearerToken();
2243
- var tokenAuth = "";
2244
- if (securityToken && securityToken.startsWith("Bearer ")) {
2245
- tokenAuth = securityToken;
2246
- }
2247
- else {
2248
- tokenAuth = "Bearer " + securityToken;
2249
- }
2250
- this.configId = this.chatServer.split("/")[3];
2251
- this.ensureConn();
2252
- };
2253
2246
  Chat4appClientAngularService.prototype.convertChat4AppMessageReceiveToChatMessage4App = function (msg) {
2254
2247
  var _a;
2255
2248
  var message = new ChatMessage4App();