chat4app-client-angular 1.0.20 → 1.0.22

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.
@@ -1790,29 +1790,33 @@
1790
1790
  };
1791
1791
  Chat4appClientAngularService.prototype.init = function (securityChatToken, chatCallback, connectionClientId) {
1792
1792
  var _this = this;
1793
+ console.log("NEW VERSION");
1793
1794
  return new rxjs.Observable(function (subscriber) {
1794
- try {
1795
- if (!_this.chatServer || _this.chatServer.trim().length === 0) {
1796
- subscriber.error(new Error("Endpoint is empty"));
1797
- return;
1798
- }
1799
- if (!securityChatToken || securityChatToken.trim().length === 0) {
1800
- subscriber.error(new Error("SecurityChatToken is empty"));
1801
- return;
1802
- }
1803
- _this.setTokenChat4app(securityChatToken);
1804
- if (chatCallback && connectionClientId) {
1805
- _this.chatUserExternalId = connectionClientId;
1806
- _this.chatCallback = chatCallback;
1807
- _this.setIntervalConnection();
1808
- }
1795
+ if (!_this.chatServer || _this.chatServer.trim().length === 0) {
1796
+ subscriber.error(new Error("Endpoint is empty"));
1797
+ return;
1798
+ }
1799
+ if (!securityChatToken || securityChatToken.trim().length === 0) {
1800
+ subscriber.error(new Error("SecurityChatToken is empty"));
1801
+ return;
1802
+ }
1803
+ _this.setTokenChat4app(securityChatToken);
1804
+ if (!chatCallback || !connectionClientId) {
1809
1805
  subscriber.next(securityChatToken);
1810
1806
  subscriber.complete();
1807
+ return;
1811
1808
  }
1812
- catch (err) {
1813
- // Captura erros inesperados e envia para o canal de erro do Observable
1809
+ _this.chatUserExternalId = connectionClientId;
1810
+ _this.chatCallback = chatCallback;
1811
+ _this.connectNats()
1812
+ .then(function () {
1813
+ _this.setIntervalConnection();
1814
+ subscriber.next(securityChatToken);
1815
+ subscriber.complete();
1816
+ })
1817
+ .catch(function (err) {
1814
1818
  subscriber.error(err);
1815
- }
1819
+ });
1816
1820
  });
1817
1821
  };
1818
1822
  Chat4appClientAngularService.prototype.getChatUrlDownload = function (messageFileId) {
@@ -2102,50 +2106,59 @@
2102
2106
  Chat4appClientAngularService.prototype.setTokenChat4app = function (chat4AppSessionToken) {
2103
2107
  window.sessionStorage.setItem("chat4AppSessionToken", chat4AppSessionToken);
2104
2108
  };
2105
- Chat4appClientAngularService.prototype.ensureConn = function () {
2109
+ Chat4appClientAngularService.prototype.connectNats = function () {
2106
2110
  var _this = this;
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());
2111
+ return new Promise(function (resolve, reject) {
2112
+ if (_this.isConnected && _this.nc) {
2113
+ resolve();
2114
+ return;
2115
+ }
2116
+ _this.configId = _this.chatServer.split("/")[3];
2117
+ if (_this.debug) {
2118
+ console.log("chatServer: " + _this.chatServer);
2119
+ console.log("userId: " + _this.chatUserExternalId);
2120
+ console.log("getBearerToken: " + _this.getBearerToken());
2114
2121
  }
2115
2122
  try {
2116
- if (this.nc) {
2117
- this.nc.close();
2123
+ if (_this.nc) {
2124
+ _this.nc.close();
2118
2125
  }
2119
2126
  }
2120
2127
  catch (e) {
2121
2128
  }
2122
- var uriStep1 = this.chatServer.replace(/^https/, 'wss');
2123
- var server = uriStep1.replace(/(\/\/.*?)\//, '$1\/ws/');
2124
- if (this.debug) {
2129
+ var uriStep1 = _this.chatServer.replace(/^https/, 'wss');
2130
+ var server = uriStep1.replace(/(\/\/.*?)\//, '$1/ws/');
2131
+ if (_this.debug) {
2125
2132
  console.log("ws server: " + server);
2126
2133
  }
2127
- nats_min.connect({ url: server, user: this.chatUserExternalId, pass: this.getBearerToken(), reconnect: false })
2134
+ nats_min.connect({
2135
+ url: server,
2136
+ user: _this.chatUserExternalId,
2137
+ pass: _this.getBearerToken(),
2138
+ reconnect: false
2139
+ })
2128
2140
  .then(function (nc) {
2129
- that_1.nc = nc;
2130
- that_1.isConnected = true;
2141
+ _this.nc = nc;
2142
+ _this.isConnected = true;
2131
2143
  var subject = "chat4app.config." + _this.configId + ".user." + _this.chatUserExternalId;
2132
- that_1.nc.subscribe(subject, function (msg) {
2144
+ _this.nc.subscribe(subject, function (msg) {
2133
2145
  if (_this.debug) {
2134
2146
  console.log(msg);
2135
2147
  }
2136
2148
  _this.processAndCallback(JSON.parse(msg.data));
2137
2149
  });
2138
2150
  if (_this.debug) {
2139
- console.log("Subscribe online chat4app.user." + _this.chatUserExternalId + ".online");
2151
+ console.log("NATS connected and subscribed: " + subject);
2140
2152
  }
2153
+ resolve();
2141
2154
  })
2142
2155
  .catch(function (err) {
2143
- // Notifica o chamador sobre o erro na conexão
2144
2156
  console.log("NATS Error:");
2145
2157
  console.log(err);
2146
- that_1.isConnected = false;
2158
+ _this.isConnected = false;
2159
+ reject(err);
2147
2160
  });
2148
- }
2161
+ });
2149
2162
  };
2150
2163
  Chat4appClientAngularService.prototype.setIntervalConnection = function () {
2151
2164
  var _this = this;
@@ -2157,9 +2170,16 @@
2157
2170
  }
2158
2171
  }
2159
2172
  this.interval = setInterval(function () {
2160
- _this.ensureConn();
2173
+ if (!_this.isConnected) {
2174
+ _this.connectNats().catch(function () {
2175
+ if (_this.debug) {
2176
+ console.log("try reconnect...");
2177
+ }
2178
+ });
2179
+ return;
2180
+ }
2161
2181
  if (_this.debug) {
2162
- console.log("publish chat4app.user.online " + _this.chatUserExternalId + " ");
2182
+ console.log("publish chat4app.user.online " + _this.chatUserExternalId);
2163
2183
  }
2164
2184
  try {
2165
2185
  _this.nc.publish("chat4app.user.online", _this.chatUserExternalId);
@@ -2167,7 +2187,7 @@
2167
2187
  catch (e) {
2168
2188
  _this.isConnected = false;
2169
2189
  if (_this.debug) {
2170
- console.log("try reconnect...");
2190
+ console.log("connection lost, reconnecting...");
2171
2191
  }
2172
2192
  }
2173
2193
  }, 1000);