chat4app-client-angular 1.0.17 → 1.0.19

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.
@@ -752,7 +752,7 @@ class Chat4appClientAngularService {
752
752
  this.debug = false;
753
753
  this.chatUserExternalId = "";
754
754
  this.configId = "";
755
- this.timeoutReconnect = 500;
755
+ this.isConnected = false;
756
756
  }
757
757
  ngOnInit() {
758
758
  }
@@ -776,7 +776,7 @@ class Chat4appClientAngularService {
776
776
  if (chatCallback && connectionClientId) {
777
777
  this.chatUserExternalId = connectionClientId;
778
778
  this.chatCallback = chatCallback;
779
- this.connectWebsocket();
779
+ this.setIntervalConnection();
780
780
  }
781
781
  subscriber.next(securityChatToken);
782
782
  subscriber.complete();
@@ -1075,39 +1075,50 @@ class Chat4appClientAngularService {
1075
1075
  window.sessionStorage.setItem("chat4AppSessionToken", chat4AppSessionToken);
1076
1076
  }
1077
1077
  ensureConn() {
1078
- if (this.debug) {
1079
- console.log(`chatServer: ${this.chatServer}`);
1080
- console.log(`userId: ${this.chatUserExternalId}`);
1081
- console.log(`getBearerToken: ${this.getBearerToken()}`);
1082
- }
1083
- const that = this;
1084
- let uriStep1 = this.chatServer.replace(/^https/, 'wss');
1085
- let server = uriStep1.replace(/(\/\/.*?)\//, '$1\/ws/');
1086
- if (this.debug) {
1087
- console.log(`ws server: ${server}`);
1088
- }
1089
- that.disconnectInternal(true);
1090
- nats_min.connect({ url: server, user: this.chatUserExternalId, pass: this.getBearerToken(), reconnect: false })
1091
- .then((nc) => {
1092
- that.nc = nc;
1093
- const subject = `chat4app.config.${this.configId}.user.${this.chatUserExternalId}`;
1094
- that.nc.subscribe(subject, (msg) => {
1095
- if (this.debug) {
1096
- console.log(msg);
1078
+ if (!this.isConnected) {
1079
+ const that = this;
1080
+ this.configId = this.chatServer.split("/")[3];
1081
+ if (this.debug) {
1082
+ console.log(`chatServer: ${this.chatServer}`);
1083
+ console.log(`userId: ${this.chatUserExternalId}`);
1084
+ console.log(`getBearerToken: ${this.getBearerToken()}`);
1085
+ }
1086
+ try {
1087
+ if (this.nc) {
1088
+ this.nc.close();
1097
1089
  }
1098
- this.processAndCallback(JSON.parse(msg.data));
1099
- });
1100
- this.setIntervalPing();
1090
+ }
1091
+ catch (e) {
1092
+ }
1093
+ let uriStep1 = this.chatServer.replace(/^https/, 'wss');
1094
+ let server = uriStep1.replace(/(\/\/.*?)\//, '$1\/ws/');
1101
1095
  if (this.debug) {
1102
- console.log(`Subscribe online chat4app.user.${this.chatUserExternalId}.online`);
1096
+ console.log(`ws server: ${server}`);
1103
1097
  }
1104
- })
1105
- .catch((err) => {
1106
- // Notifica o chamador sobre o erro na conexão
1107
- console.error("NATS Error:", err);
1108
- });
1098
+ nats_min.connect({ url: server, user: this.chatUserExternalId, pass: this.getBearerToken(), reconnect: false })
1099
+ .then((nc) => {
1100
+ that.nc = nc;
1101
+ that.isConnected = true;
1102
+ const subject = `chat4app.config.${this.configId}.user.${this.chatUserExternalId}`;
1103
+ that.nc.subscribe(subject, (msg) => {
1104
+ if (this.debug) {
1105
+ console.log(msg);
1106
+ }
1107
+ this.processAndCallback(JSON.parse(msg.data));
1108
+ });
1109
+ if (this.debug) {
1110
+ console.log(`Subscribe online chat4app.user.${this.chatUserExternalId}.online`);
1111
+ }
1112
+ })
1113
+ .catch((err) => {
1114
+ // Notifica o chamador sobre o erro na conexão
1115
+ console.log("NATS Error:");
1116
+ console.log(err);
1117
+ that.isConnected = false;
1118
+ });
1119
+ }
1109
1120
  }
1110
- setIntervalPing() {
1121
+ setIntervalConnection() {
1111
1122
  if (this.interval) {
1112
1123
  try {
1113
1124
  clearInterval(this.interval);
@@ -1116,6 +1127,7 @@ class Chat4appClientAngularService {
1116
1127
  }
1117
1128
  }
1118
1129
  this.interval = setInterval(() => {
1130
+ this.ensureConn();
1119
1131
  if (this.debug) {
1120
1132
  console.log(`publish chat4app.user.online ${this.chatUserExternalId} `);
1121
1133
  }
@@ -1123,17 +1135,14 @@ class Chat4appClientAngularService {
1123
1135
  this.nc.publish(`chat4app.user.online`, this.chatUserExternalId);
1124
1136
  }
1125
1137
  catch (e) {
1138
+ this.isConnected = false;
1126
1139
  if (this.debug) {
1127
1140
  console.log("try reconnect...");
1128
1141
  }
1129
- this.ensureConn();
1130
1142
  }
1131
1143
  }, 1000);
1132
1144
  }
1133
1145
  disconnect() {
1134
- return this.disconnectInternal(false);
1135
- }
1136
- disconnectInternal(ignoreOffLine) {
1137
1146
  // Check how we can disconnect
1138
1147
  if (this.interval) {
1139
1148
  try {
@@ -1142,7 +1151,7 @@ class Chat4appClientAngularService {
1142
1151
  catch (e) {
1143
1152
  }
1144
1153
  }
1145
- if (!ignoreOffLine && this.nc && this.chatUserExternalId && this.chatUserExternalId.length > 0 && this.getBearerToken() && this.getBearerToken().length > 0) {
1154
+ if (this.nc && this.chatUserExternalId && this.chatUserExternalId.length > 0 && this.getBearerToken() && this.getBearerToken().length > 0) {
1146
1155
  try {
1147
1156
  this.setOffLine(this.chatUserExternalId).toPromise();
1148
1157
  if (this.debug)
@@ -1204,20 +1213,6 @@ class Chat4appClientAngularService {
1204
1213
  }
1205
1214
  //}
1206
1215
  }
1207
- connectWebsocket() {
1208
- const that = this;
1209
- this.disconnectInternal(true);
1210
- let securityToken = this.getBearerToken();
1211
- let tokenAuth = "";
1212
- if (securityToken && securityToken.startsWith("Bearer ")) {
1213
- tokenAuth = securityToken;
1214
- }
1215
- else {
1216
- tokenAuth = "Bearer " + securityToken;
1217
- }
1218
- this.configId = this.chatServer.split("/")[3];
1219
- this.ensureConn();
1220
- }
1221
1216
  convertChat4AppMessageReceiveToChatMessage4App(msg) {
1222
1217
  var _a;
1223
1218
  let message = new ChatMessage4App();