chat4app-client-angular 1.0.18 → 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,41 +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.log("NATS Error:");
1108
- console.log(err);
1109
- setTimeout(this.ensureConn, 1000);
1110
- });
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
+ }
1111
1120
  }
1112
- setIntervalPing() {
1121
+ setIntervalConnection() {
1113
1122
  if (this.interval) {
1114
1123
  try {
1115
1124
  clearInterval(this.interval);
@@ -1118,6 +1127,7 @@ class Chat4appClientAngularService {
1118
1127
  }
1119
1128
  }
1120
1129
  this.interval = setInterval(() => {
1130
+ this.ensureConn();
1121
1131
  if (this.debug) {
1122
1132
  console.log(`publish chat4app.user.online ${this.chatUserExternalId} `);
1123
1133
  }
@@ -1125,17 +1135,14 @@ class Chat4appClientAngularService {
1125
1135
  this.nc.publish(`chat4app.user.online`, this.chatUserExternalId);
1126
1136
  }
1127
1137
  catch (e) {
1138
+ this.isConnected = false;
1128
1139
  if (this.debug) {
1129
1140
  console.log("try reconnect...");
1130
1141
  }
1131
- this.ensureConn();
1132
1142
  }
1133
1143
  }, 1000);
1134
1144
  }
1135
1145
  disconnect() {
1136
- return this.disconnectInternal(false);
1137
- }
1138
- disconnectInternal(ignoreOffLine) {
1139
1146
  // Check how we can disconnect
1140
1147
  if (this.interval) {
1141
1148
  try {
@@ -1144,7 +1151,7 @@ class Chat4appClientAngularService {
1144
1151
  catch (e) {
1145
1152
  }
1146
1153
  }
1147
- 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) {
1148
1155
  try {
1149
1156
  this.setOffLine(this.chatUserExternalId).toPromise();
1150
1157
  if (this.debug)
@@ -1206,20 +1213,6 @@ class Chat4appClientAngularService {
1206
1213
  }
1207
1214
  //}
1208
1215
  }
1209
- connectWebsocket() {
1210
- const that = this;
1211
- this.disconnectInternal(true);
1212
- let securityToken = this.getBearerToken();
1213
- let tokenAuth = "";
1214
- if (securityToken && securityToken.startsWith("Bearer ")) {
1215
- tokenAuth = securityToken;
1216
- }
1217
- else {
1218
- tokenAuth = "Bearer " + securityToken;
1219
- }
1220
- this.configId = this.chatServer.split("/")[3];
1221
- this.ensureConn();
1222
- }
1223
1216
  convertChat4AppMessageReceiveToChatMessage4App(msg) {
1224
1217
  var _a;
1225
1218
  let message = new ChatMessage4App();