@chat21/chat21-ionic 3.0.58 → 3.0.59-rc15

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.
Files changed (96) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/README.md +9 -0
  3. package/config.xml +11 -2
  4. package/env.sample +2 -0
  5. package/package.json +1 -1
  6. package/resources/Android/splash/drawable-land-hdpi-screen.png +0 -0
  7. package/resources/Android/splash/drawable-land-ldpi-screen.png +0 -0
  8. package/resources/Android/splash/drawable-land-mdpi-screen.png +0 -0
  9. package/resources/Android/splash/drawable-land-xhdpi-screen.png +0 -0
  10. package/resources/Android/splash/drawable-land-xxhdpi-screen.png +0 -0
  11. package/resources/Android/splash/drawable-land-xxxhdpi-screen.png +0 -0
  12. package/resources/Android/splash/drawable-port-hdpi-screen.png +0 -0
  13. package/resources/Android/splash/drawable-port-ldpi-screen.png +0 -0
  14. package/resources/Android/splash/drawable-port-mdpi-screen.png +0 -0
  15. package/resources/Android/splash/drawable-port-xhdpi-screen.png +0 -0
  16. package/resources/Android/splash/drawable-port-xxhdpi-screen.png +0 -0
  17. package/resources/Android/splash/drawable-port-xxxhdpi-screen.png +0 -0
  18. package/resources/Android/splash.png +0 -0
  19. package/src/app/app-routing.module.ts +21 -17
  20. package/src/app/app.component.html +6 -3
  21. package/src/app/app.component.ts +259 -55
  22. package/src/app/app.module.ts +16 -8
  23. package/src/app/chatlib/conversation-detail/conversation-content/conversation-content.component.html +1 -1
  24. package/src/app/chatlib/conversation-detail/ion-conversation-detail/ion-conversation-detail.component.html +4 -4
  25. package/src/app/chatlib/conversation-detail/ion-conversation-detail/ion-conversation-detail.component.scss +8 -2
  26. package/src/app/chatlib/conversation-detail/ion-conversation-detail/ion-conversation-detail.component.ts +6 -1
  27. package/src/app/chatlib/conversation-detail/message/bubble-message/bubble-message.component.html +7 -2
  28. package/src/app/chatlib/conversation-detail/message/bubble-message/bubble-message.component.ts +47 -2
  29. package/src/app/chatlib/conversation-detail/message/image/image.component.html +7 -6
  30. package/src/app/chatlib/conversation-detail/message/image/image.component.ts +20 -1
  31. package/src/app/chatlib/list-conversations-component/ion-list-conversations/ion-list-conversations.component.html +39 -9
  32. package/src/app/chatlib/list-conversations-component/ion-list-conversations/ion-list-conversations.component.scss +8 -144
  33. package/src/app/chatlib/list-conversations-component/ion-list-conversations/ion-list-conversations.component.ts +14 -17
  34. package/src/app/chatlib/list-conversations-component/list-conversations/list-conversations.component.html +1 -1
  35. package/src/app/components/authentication/login/login.component.html +10 -10
  36. package/src/app/components/authentication/login/login.component.ts +2 -1
  37. package/src/app/components/conversation-detail/bubble-my-message/bubble-my-message.component.ts +1 -1
  38. package/src/app/components/conversation-detail/bubble-others-message/bubble-others-message.component.ts +1 -1
  39. package/src/app/components/conversation-detail/header-conversation-detail/header-conversation-detail.component.html +17 -12
  40. package/src/app/components/conversation-detail/header-conversation-detail/header-conversation-detail.component.scss +12 -0
  41. package/src/app/components/conversation-detail/header-conversation-detail/header-conversation-detail.component.ts +21 -5
  42. package/src/app/components/conversation-detail/message-text-area/message-text-area.component.html +3 -2
  43. package/src/app/components/conversation-detail/message-text-area/message-text-area.component.scss +22 -17
  44. package/src/app/components/conversation-detail/message-text-area/message-text-area.component.ts +46 -5
  45. package/src/app/components/ddp-header/ddp-header.component.html +1 -1
  46. package/src/app/components/ddp-header/ddp-header.component.ts +4 -2
  47. package/src/app/components/image-viewer/image-viewer.component.html +23 -0
  48. package/src/app/components/image-viewer/image-viewer.component.scss +107 -0
  49. package/src/app/components/image-viewer/image-viewer.component.spec.ts +24 -0
  50. package/src/app/components/image-viewer/image-viewer.component.ts +38 -0
  51. package/src/app/components/project-item/project-item.component.html +147 -0
  52. package/src/app/components/project-item/project-item.component.scss +669 -0
  53. package/src/app/components/project-item/project-item.component.spec.ts +24 -0
  54. package/src/app/components/project-item/project-item.component.ts +317 -0
  55. package/src/app/components/utils/avatar-profile/avatar-profile.component.html +7 -0
  56. package/src/app/components/utils/avatar-profile/avatar-profile.component.ts +22 -5
  57. package/src/app/pages/authentication/login/login.page.html +1 -2
  58. package/src/app/pages/authentication/login/login.page.ts +1 -1
  59. package/src/app/pages/conversation-detail/conversation-detail.page.html +5 -3
  60. package/src/app/pages/conversation-detail/conversation-detail.page.ts +122 -30
  61. package/src/app/pages/conversations-list/conversations-list.page.html +44 -23
  62. package/src/app/pages/conversations-list/conversations-list.page.scss +290 -127
  63. package/src/app/pages/conversations-list/conversations-list.page.ts +172 -12
  64. package/src/app/pages/unassigned-conversations/unassigned-conversations-routing.module.ts +17 -0
  65. package/src/app/pages/unassigned-conversations/unassigned-conversations.module.ts +22 -0
  66. package/src/app/pages/unassigned-conversations/unassigned-conversations.page.html +22 -0
  67. package/src/app/pages/unassigned-conversations/unassigned-conversations.page.scss +79 -0
  68. package/src/app/pages/unassigned-conversations/unassigned-conversations.page.spec.ts +24 -0
  69. package/src/app/pages/unassigned-conversations/unassigned-conversations.page.ts +108 -0
  70. package/src/app/services/nav-proxy.service.ts +1 -1
  71. package/src/app/services/tiledesk/tiledesk.service.ts +22 -1
  72. package/src/app/services/websocket/websocket-js.ts +557 -0
  73. package/src/app/services/websocket/websocket.service.spec.ts +12 -0
  74. package/src/app/services/websocket/websocket.service.ts +274 -0
  75. package/src/app/shared/shared.module.ts +7 -1
  76. package/src/assets/i18n/en.json +9 -1
  77. package/src/assets/i18n/it.json +9 -1
  78. package/src/assets/js/chat21client.js +141 -67
  79. package/src/assets/transparent.png +0 -0
  80. package/src/chat-config-pre-test.json +3 -1
  81. package/src/chat-config-template.json +3 -1
  82. package/src/chat-config.json +3 -1
  83. package/src/chat21-core/providers/chat-manager.ts +3 -3
  84. package/src/chat21-core/providers/firebase/firebase-archivedconversations-handler.ts +1 -1
  85. package/src/chat21-core/providers/firebase/firebase-auth-service.ts +6 -6
  86. package/src/chat21-core/providers/firebase/firebase-conversation-handler.ts +1 -1
  87. package/src/chat21-core/providers/firebase/firebase-notifications.ts +1 -1
  88. package/src/chat21-core/providers/firebase/firebase-presence.service.ts +2 -2
  89. package/src/chat21-core/providers/firebase/firebase-typing.service.ts +1 -1
  90. package/src/chat21-core/providers/firebase/firebase-upload.service.ts +1 -1
  91. package/src/chat21-core/providers/mqtt/mqtt-auth-service.ts +27 -27
  92. package/src/chat21-core/providers/mqtt/mqtt-conversations-handler.ts +13 -5
  93. package/src/chat21-core/providers/mqtt/mqtt-notifications.ts +101 -11
  94. package/src/chat21-core/providers/tiledesk/tiledesk-auth.service.ts +13 -4
  95. package/src/chat21-core/utils/utils.ts +1 -1
  96. package/src/firebase-messaging-sw-template.js +1 -1
@@ -0,0 +1,557 @@
1
+
2
+ //import { Injectable } from '@angular/core';
3
+ import { forwardRef, Inject } from '@angular/core';
4
+
5
+ import { LoggerService } from 'src/chat21-core/providers/abstract/logger.service';
6
+ import { LoggerInstance } from 'src/chat21-core/providers/logger/loggerInstance';
7
+ //@Injectable()
8
+ export class WebSocketJs {
9
+
10
+ public url;
11
+ public onCreate;
12
+ public onUpdate;
13
+ public onOpen;
14
+ public onData;
15
+ public ws;
16
+ public topics;
17
+ public callbacks;
18
+ public readyState: number;
19
+ // public userHasClosed: boolean;
20
+
21
+ // HEARTBEAT https://github.com/zimv/websocket-heartbeat-js/blob/master/lib/index.js
22
+ private pingTimeoutId;
23
+ private pongTimeoutId;
24
+
25
+ public pingMsg = { action: "heartbeat", payload: { message: { text: "ping" } } }
26
+
27
+ public pongMsg = { action: "heartbeat", payload: { message: { text: "pong" } } }
28
+
29
+
30
+ public pongTimeout = 10000;
31
+ public pingTimeout = 15000;
32
+
33
+ // nktest
34
+ // startTimeMS = 0;
35
+
36
+ private logger: LoggerService = LoggerInstance.getInstance();
37
+
38
+ constructor() {
39
+
40
+ // this.logger.log("[WEBSOCKET-JS] HELLO !!!");
41
+ this.topics = [];
42
+ this.callbacks = new Map();
43
+
44
+ // this.sendingMessages = [];//new Map();
45
+ }
46
+
47
+
48
+ /**
49
+ * readyState: A read-only attribute. It represents the state of the connection. It can have the following values:
50
+ * 0: Connection is in progress and has not yet been established.
51
+ * 1: Connection is established and messages can be sent between the client and the server.
52
+ * 2: Connection is going through the closing handshake.
53
+ * 3: Connection has been closed or could not be opened.
54
+ */
55
+
56
+ // -----------------------------------------------------------------------------------------------------
57
+ // @ ref - called by 'WsRequestsService' and 'WsMsgsService' & call 'subscribe'
58
+ // -----------------------------------------------------------------------------------------------------
59
+ // WsRequestsService:
60
+ // - in getCurrentProjectAndSubscribeTo_WsRequests()
61
+ // - in subscribeTo_wsRequestById() called when in
62
+ // WsRequestsMsgsComponent onInit() is got the request id from url params
63
+ // WsMsgsService
64
+ // - in subsToWS_MsgsByRequestId() called when in
65
+ // WsRequestsMsgsComponent onInit() is got the request id from url params
66
+
67
+ ref(topic, calledby, onCreate, onUpdate, onData) {
68
+ // this.logger.log('[WEBSOCKET-JS] ****** CALLING REF ****** ');
69
+ this.logger.log('[WEBSOCKET-JS] - REF - calledby ', calledby);
70
+ this.logger.log('[WEBSOCKET-JS] - REF - TOPIC ', topic);
71
+ this.logger.log('[WEBSOCKET-JS] - REF - CALLBACKS', this.callbacks);
72
+
73
+ if (!this.callbacks) {
74
+ this.logger.log('[WEBSOCKET-JS] - REF OOOOPS! NOT CALLBACKS ***', this.callbacks);
75
+ return
76
+ }
77
+
78
+ this.callbacks.set(topic, { onCreate: onCreate, onUpdate: onUpdate, onData: onData });
79
+ this.logger.log('[WEBSOCKET-JS] - CALLBACK-SET - callbacks', this.callbacks);
80
+
81
+ if (this.ws && this.ws.readyState == 1) {
82
+ this.logger.log('[WEBSOCKET-JS] - REF - READY STATE ', this.ws.readyState);
83
+ this.logger.log('[WEBSOCKET-JS] - REF - READY STATE = 1 > SUBSCRIBE TO TOPICS ');
84
+
85
+ this.subscribe(topic);
86
+
87
+ } else {
88
+ // this.ws = new WebSocket("wss://tiledesk-server-pre.herokuapp.com/?token=JWT eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI1ZGRkMzBiZmYwMTk1ZjAwMTdmNzJjNmQiLCJlbWFpbCI6InByZWdpbm9AZjIxdGVzdC5pdCIsImZpcnN0bmFtZSI6Ikdpbm8iLCJsYXN0bmFtZSI6IlByZSIsImVtYWlsdmVyaWZpZWQiOnRydWUsImlhdCI6MTYwODgwNjY0MCwiYXVkIjoiaHR0cHM6Ly90aWxlZGVzay5jb20iLCJpc3MiOiJodHRwczovL3RpbGVkZXNrLmNvbSIsInN1YiI6InVzZXIiLCJqdGkiOiI1YmVmMDcxYy00ODBlLTQzYzQtOTRhYS05ZjQxYzMyNDcxMGQifQ.wv6uBn2P6H9wGb5WCYQkpPEScMU9PB1pBUzFouhJk20");
89
+
90
+ this.logger.log('[WEBSOCKET-JS] - REF - READY STATE ≠ 1 > OPEN WS AND THEN SUBSCRIBE TO TOPICS');
91
+ // this.logger.log('% »»» WebSocketJs WF *** REF *** WS 2 ', this.ws);
92
+
93
+ var that = this;
94
+ if (this.ws) {
95
+ this.ws.addEventListener("open", function (event) {
96
+ that.logger.log('[WEBSOCKET-JS] - REF - OPEN EVENT *** ', event);
97
+ that.subscribe(topic);
98
+ });
99
+ }
100
+
101
+ if (this.topics.indexOf(topic) === -1) {
102
+ this.topics.push(topic);
103
+ }
104
+ }
105
+ }
106
+
107
+ // -----------------------------------------------------------------------------------------------------
108
+ // @ subscribe - is called by 'ref' & call 'send'
109
+ // -----------------------------------------------------------------------------------------------------
110
+ subscribe(topic) {
111
+
112
+ if (this.topics.indexOf(topic) === -1) {
113
+ this.topics.push(topic);
114
+ }
115
+ this.logger.log('[WEBSOCKET-JS] - SUBSCRIBE TO TOPIC ', topic);
116
+
117
+ var message = {
118
+ action: 'subscribe',
119
+ payload: {
120
+ //topic: '/' + project_id + '/requests',
121
+ topic: topic,
122
+ message: undefined,
123
+ method: undefined
124
+ },
125
+ };
126
+ var str = JSON.stringify(message);
127
+ this.logger.log("[WEBSOCKET-JS] - SUBSCRIBE TO TOPIC - STRING TO SEND " + str, " FOR SUBSCRIBE TO TOPIC: ", topic);
128
+
129
+ this.send(str, `SUBSCRIBE to ${topic}`);
130
+ }
131
+
132
+ // -----------------------------------------------------------------------------------------------------
133
+ // @ unsubscribe
134
+ // - called by: 'WsRequestsService' > getCurrentProjectAndSubscribeTo_WsRequests()
135
+ // > unsubscribeTo_wsRequestById() called by WsRequestsMsgsComponent > On Init & On Destroy
136
+ // 'WsMsgsService' > unsubsToWS_MsgsByRequestId() > On Init & On Destroy
137
+ // - call 'send'
138
+ // -----------------------------------------------------------------------------------------------------
139
+ unsubscribe(topic) {
140
+ this.logger.log("[WEBSOCKET-JS] - UN-SUBSCRIBE - FROM TOPIC: ", topic);
141
+ // this.logger.log("% »»» WebSocketJs WF *** UNSUBSCRIBE *** - this.topics ", this.topics);
142
+ // this.logger.log("% »»» WebSocketJs WF *** UNSUBSCRIBE *** - topic ", topic);
143
+ // this.logger.log("% »»» WebSocketJs WF *** UNSUBSCRIBE *** - callbacks ", this.callbacks);
144
+
145
+
146
+ var index = this.topics.indexOf(topic);
147
+
148
+ if (index > -1) {
149
+ this.topics.splice(index, 1);
150
+ }
151
+
152
+ this.logger.log("[WEBSOCKET-JS] - UN-SUBSCRIBE - TOPICS AFTER SPLICE THE TOPIC ", this.topics);
153
+ this.logger.log("[WEBSOCKET-JS] - UN-SUBSCRIBE - DELETE TOPIC FROM CALLBACKS - CALLBACKS SIZE ", this.callbacks.size);
154
+
155
+ // if (this.callbacks.length > 0) {
156
+ if (this.callbacks.size > 0) {
157
+ this.callbacks.delete(topic);
158
+ this.logger.log("[WEBSOCKET-JS] - UN-SUBSCRIBE - CALLBACKS AFTER DELETE TOPIC ", this.callbacks);
159
+ }
160
+
161
+ var message = {
162
+ action: 'unsubscribe',
163
+ payload: {
164
+ //topic: '/' + project_id + '/requests',
165
+ topic: topic,
166
+ message: undefined,
167
+ method: undefined
168
+ },
169
+ };
170
+ var str = JSON.stringify(message);
171
+ this.logger.log("[WEBSOCKET-JS] - UN-SUBSCRIBE str " + str);
172
+
173
+ if (this.ws.readyState == 1) {
174
+ this.logger.log("[WEBSOCKET-JS] - UN-SUBSCRIBE TO TOPIC - STRING TO SEND " + str, " FOR UNSUBSCRIBE TO TOPIC: ", topic);
175
+ this.send(str, `UNSUSCRIBE from ${topic}`);
176
+
177
+ } else {
178
+ this.logger.log("[WEBSOCKET-JS] - UN-SUBSCRIBE TRY 'SEND' BUT READY STASTE IS : ", this.ws.readyState);
179
+ }
180
+ }
181
+
182
+ // -----------------------------------------------------------------------------------------------------
183
+ // @ send -
184
+ // -----------------------------------------------------------------------------------------------------
185
+ send(initialMessage, calling_method) {
186
+ // this.logger.log("[WEBSOCKET-JS] - SEND - INIZIAL-MSG ", initialMessage, " CALLED BY ", calling_method);
187
+
188
+ this.ws.send(initialMessage);
189
+ }
190
+
191
+
192
+ // -----------------------------------------------------------------------------------------------------
193
+ // @ close (to find where is used search for x webSocketClose())
194
+ // -----------------------------------------------------------------------------------------------------
195
+ close() {
196
+ this.topics = [];
197
+ this.callbacks = [];
198
+ this.logger.log("[WEBSOCKET-JS] - CALLED CLOSE - TOPICS ", this.topics, ' - CALLLBACKS ', this.callbacks);
199
+
200
+ if (this.ws) {
201
+ this.ws.onclose = function () { }; // disable onclose handler first
202
+ this.ws.close();
203
+ }
204
+ this.heartReset();
205
+ }
206
+
207
+ // -----------------------------------------------------------------------------------------------------
208
+ // @ Resubscribe
209
+ // -----------------------------------------------------------------------------------------------------
210
+ resubscribe() {
211
+ this.logger.log("[WEBSOCKET-JS] - RESUBSCRIBE - TO TOPICS ", this.topics);
212
+ this.logger.log("[WEBSOCKET-JS] - RESUBSCRIBE - CALLBACKS ", this.callbacks);
213
+
214
+ if (this.topics.length > 0) {
215
+ this.topics.forEach(topic => {
216
+ this.logger.log("[WEBSOCKET-JS] - RESUBSCRIBE - SUBDCRIBE TO TOPICS ", topic);
217
+ this.subscribe(topic); // nn fa sudbcribe
218
+ });
219
+ }
220
+ }
221
+
222
+ // -----------------------------------------------------------------------------------------------------
223
+ // @ HeartCheck
224
+ // -----------------------------------------------------------------------------------------------------
225
+ heartCheck() {
226
+ this.heartReset();
227
+ this.heartStart();
228
+ }
229
+
230
+ // _heartStart() {
231
+ // // if(this.forbidReconnect) return;//Non ricollegare o eseguire il battito cardiaco
232
+ // this.pingTimeoutId = setTimeout(() => {
233
+ // // Qui viene inviato un battito cardiaco Dopo averlo ricevuto, viene restituito un messaggio di battito cardiaco.
234
+ // // onmessage Ottieni il battito cardiaco restituito per indicare che la connessione è normale
235
+ // try {
236
+ // this.logger.log('% »»» WebSocketJs - HEARTBEAT send MSG ', JSON.stringify(this.pingMsg));
237
+ // this.ws.send(JSON.stringify(this.pingMsg));
238
+ // // Se non viene ripristinato dopo un determinato periodo di tempo, il backend viene attivamente disconnesso
239
+ // this.pongTimeoutId = setTimeout(() => {
240
+ // // se onclose Si esibirà reconnect,Eseguiamo ws.close() Bene, se lo esegui direttamente reconnect Si innescherà onclose Causa riconnessione due volte
241
+ // this.ws.close();
242
+ // }, this.pongTimeout);
243
+
244
+ // } catch (e) {
245
+
246
+ // this.logger.log('% »»» WebSocketJs - HEARTBEAT err ', e);
247
+ // }
248
+
249
+ // }, this.pingTimeout);
250
+ // }
251
+
252
+
253
+ // getRemainingTime() {
254
+ // var milliSecondsTime = 15000;
255
+ // var timer;
256
+
257
+ // this.logger.log('% »»» WebSocketJs - heartStart timer 1', milliSecondsTime / 1000);
258
+ // timer = setInterval(function () {
259
+ // milliSecondsTime = milliSecondsTime - 1000;
260
+ // if (milliSecondsTime / 1000 == 0) {
261
+ // clearTimeout(timer);
262
+ // }
263
+ // else {
264
+ // this.logger.log('% »»» WebSocketJs - heartStart timer 2 ', milliSecondsTime / 1000);
265
+ // }
266
+ // }, 1000);
267
+ // }
268
+
269
+ // -----------------------------------------------------------------------------------------------------
270
+ // @ HeartStart
271
+ // -----------------------------------------------------------------------------------------------------
272
+ heartStart() {
273
+ // this.getRemainingTime();
274
+ this.pingTimeoutId = setTimeout(() => {
275
+
276
+ // Qui viene inviato un battito cardiaco Dopo averlo ricevuto, viene restituito un messaggio di battito cardiaco.
277
+ // onmessage Ottieni il battito cardiaco restituito per indicare che la connessione è normale
278
+ if (this.ws.readyState == 1) {
279
+
280
+ // this.logger.log("[WEBSOCKET-JS] - HEART-START - SEND PING-MSG");
281
+
282
+ this.send(JSON.stringify(this.pingMsg), 'HEART-START')
283
+
284
+ } else {
285
+
286
+ this.logger.log("[WEBSOCKET-JS] - HEART-START - TRY TO SEND PING-MSG BUT READY STATE IS ", this.ws.readyState);
287
+
288
+ }
289
+
290
+ // Se non viene ripristinato dopo un determinato periodo di tempo, il backend viene attivamente disconnesso
291
+ this.pongTimeoutId = setTimeout(() => {
292
+ this.logger.log("[WEBSOCKET-JS] - HEART-START - PONG-TIMEOUT-ID - CLOSE WS ");
293
+ // se onclose Si esibirà reconnect,Eseguiamo ws.close() Bene, se lo esegui direttamente reconnect Si innescherà onclose Causa riconnessione due volte
294
+ this.ws.close();
295
+ }, this.pongTimeout);
296
+
297
+ }, this.pingTimeout);
298
+ }
299
+
300
+ // -----------------------------------------------------------------------------------------------------
301
+ // @ heartReset
302
+ // -----------------------------------------------------------------------------------------------------
303
+ heartReset() {
304
+ // this.logger.log("[WEBSOCKET-JS] - HEART-RESET");
305
+ clearTimeout(this.pingTimeoutId);
306
+ clearTimeout(this.pongTimeoutId);
307
+ }
308
+
309
+ // -----------------------------------------------------------------------------------------------------
310
+ // @ init
311
+ // -----------------------------------------------------------------------------------------------------
312
+ init(url, onCreate, onUpdate, onData, onOpen = undefined, onOpenCallback = undefined, _topics = [], _callbacks = new Map()) {
313
+
314
+
315
+
316
+ this.url = url;
317
+ this.onCreate = onCreate;
318
+ this.onUpdate = onUpdate;
319
+ this.onData = onData;
320
+ this.onOpen = onOpen;
321
+ this.topics = _topics//[];//new Map();
322
+ this.callbacks = _callbacks// new Map();
323
+ // this.userHasClosed = false;
324
+ // this.logger.log('% »»» WebSocketJs WF - closeWebsocket this.userHasClosed ' , this.userHasClosed);
325
+
326
+ // this.sendingMessages = [];//new Map();
327
+ // this.data = [];
328
+ // this.init(this.sendMesagesInSendingArray);
329
+
330
+ this.logger.log("[WEBSOCKET-JS] - CALLING INIT - topics ", this.topics);
331
+ this.logger.log("[WEBSOCKET-JS] - CALLING INIT - url ", this.url);
332
+ this.logger.log("[WEBSOCKET-JS] - CALLING INIT - callbacks ", this.callbacks);
333
+
334
+
335
+ var that = this;
336
+ return new Promise(function (resolve, reject) {
337
+ // var options = {
338
+ // // headers: {
339
+ // // "Authorization" : "JWT " + token
340
+ // // }
341
+ // };
342
+ // this.logger.log('options', options);
343
+ // var ws = new WebSocket('ws://localhost:3000');
344
+ // var ws = new WebSocket('ws://localhost:3000/public/requests');
345
+ // var ws = new WebSocket('ws://localhost:3000/5bae41325f03b900401e39e8/messages');
346
+
347
+
348
+ // -----------------------------------------------------------------------------------------------------
349
+ // @ new WebSocket
350
+ // -----------------------------------------------------------------------------------------------------
351
+ that.ws = new WebSocket(that.url);
352
+
353
+
354
+ // -----------------------------------------------------------------------------------------------------
355
+ // @ onopen
356
+ // -----------------------------------------------------------------------------------------------------
357
+ that.ws.onopen = function (e) {
358
+ that.logger.log('[WEBSOCKET-JS] - websocket is connected ...', e);
359
+
360
+ // -----------------
361
+ // @ heartCheck
362
+ // -----------------
363
+ that.heartCheck();
364
+
365
+
366
+ if (onOpenCallback) {
367
+ onOpenCallback();
368
+ }
369
+
370
+ if (that.onOpen) {
371
+ that.onOpen();
372
+ }
373
+
374
+ }
375
+
376
+ // -----------------------------------------------------------------------------------------------------
377
+ // @ onclose
378
+ // -----------------------------------------------------------------------------------------------------
379
+ that.ws.onclose = function (e) {
380
+ that.logger.log('[WEBSOCKET-JS] websocket IS CLOSED ... Try to reconnect in 3 seconds ', e);
381
+
382
+ // this.logger.log('% »»» WebSocketJs - websocket onclose this.userHasClosed ', that.userHasClosed);
383
+ // https://stackoverflow.com/questions/3780511/reconnection-of-client-when-server-reboots-in-websocket
384
+ // Try to reconnect in 3 seconds
385
+
386
+ // --------------------
387
+ // @ init > resubscribe
388
+ // --------------------
389
+
390
+ setTimeout(function () {
391
+ that.init(url, onCreate, onUpdate, onData, onOpen, function () {
392
+ that.logger.log('[WEBSOCKET-JS] websocket IS CLOSED ... CALLING RESUSCRIBE ');
393
+ that.resubscribe();
394
+ }, that.topics, that.callbacks);
395
+ }, 3000);
396
+ }
397
+
398
+ // -----------------------------------------------------------------------------------------------------
399
+ // @ onerror
400
+ // -----------------------------------------------------------------------------------------------------
401
+ that.ws.onerror = function (err) {
402
+ that.logger.error('[WEBSOCKET-JS] websocket IS CLOSED - websocket error ...', err)
403
+ }
404
+
405
+ // -----------------------------------------------------------------------------------------------------
406
+ // @ onmessage
407
+ // -----------------------------------------------------------------------------------------------------
408
+ that.ws.onmessage = function (message) {
409
+ // that.logger.log('[WEBSOCKET-JS] websocket onmessage ', message);
410
+ // that.logger.log('[WEBSOCKET-JS] websocket onmessage data', message.data);
411
+
412
+ // let test = '{ "action": "publish","payload": {"topic": "/5df26badde7e1c001743b63c/requests", "method": "CREATE", "message": [ { "_id": "5f29372d690e6f0034edf100", "status": 200, "preflight": false, "hasBot": true, "participants": ["bot_5df272e8de7e1c001743b645"], "participantsAgents": [], "participantsBots": ["5df272e8de7e1c001743b645"], "request_id": "support-group-MDszsSJlwqQn1_WCh6u", "requester": "5f29371b690e6f0034edf0f5", "lead": "5f29372d690e6f0034edf0ff", "first_text": "ocourse the email is valid ","department": "5df26badde7e1c001743b63e", "agents": [{"user_available": true,"online_status": "online", "number_assigned_requests": 35, "_id": "5e0f2119705a35001725714d","id_project": "5df26badde7e1c001743b63c", "id_user": "5aaa99024c3b110014b478f0", "role": "admin", "createdBy": "5df26ba1de7e1c001743b637","createdAt": "2020-01-03T11:10:17.123Z", "updatedAt": "2020-01-03T11:10:17.123Z", "__v": 0 }, { "user_available": false, "online_status": "offline", "number_assigned_requests": 0, "_id": "5e1a13824437eb0017f712b4", "id_project": "5df26badde7e1c001743b63c","id_user": "5ac7521787f6b50014e0b592", "role": "admin", "createdBy": "5df26ba1de7e1c001743b637", "createdAt": "2020-01-11T18:27:14.657Z","updatedAt": "2020-01-11T18:27:14.657Z", "__v": 0}, { "user_available": false,"online_status": "offline", "number_assigned_requests": 0, "_id": "5df26bdfde7e1c001743b640", "id_project": "5df26badde7e1c001743b63c", "id_user": "5de9200d6722370017731969","role": "admin","createdBy": "5df26ba1de7e1c001743b637", "createdAt": "2019-12-12T16:33:35.244Z", "updatedAt": "2019-12-12T16:33:35.244Z","__v": 0 }, {"user_available": true, "online_status": "online","number_assigned_requests": -11, "_id": "5eb1a3647ac005003480f54d", "id_project": "5df26badde7e1c001743b63c","id_user": "5e09d16d4d36110017506d7f","role": "owner", "createdBy": "5aaa99024c3b110014b478f0","createdAt": "2020-05-05T17:33:24.328Z", "updatedAt": "2020-05-05T17:33:24.328Z","__v": 0}], "sourcePage": "https://www.tiledesk.com/pricing-self-managed/", "language": "en","userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36","attributes": { "departmentId": "5df26badde7e1c001743b63e","departmentName": "Default Department","ipAddress": "115.96.30.154","client": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36","sourcePage": "https://www.tiledesk.com/pricing-self-managed/", "projectId": "5df26badde7e1c001743b63c", "requester_id": "ce31d3fd-a358-49c7-9b9f-5aead8330063", "subtype": "info","decoded_jwt": {"_id": "ce31d3fd-a358-49c7-9b9f-5aead8330063","firstname": "Guest", "id": "ce31d3fd-a358-49c7-9b9f-5aead8330063", "fullName": "Guest ","iat": 1596536604,"aud": "https://tiledesk.com","iss": "https://tiledesk.com","sub": "guest","jti": "702a4a7e-e56a-43cf-aadd-376f7c12f633"}},"id_project": "5df26badde7e1c001743b63c","createdBy": "ce31d3fd-a358-49c7-9b9f-5aead8330063","tags": [], "notes": [],"channel": {"name": "chat21"},"createdAt": "2020-08-04T10:23:41.641Z","updatedAt": "2021-03-25T18:01:13.371Z","__v": 3,"assigned_at": "2020-08-04T10:25:26.059Z","channelOutbound": {"name": "chat21"},"snapshot": {"agents": [{"id_user": "5aaa99024c3b110014b478f0"}, {"id_user": "5ac7521787f6b50014e0b592"}, {"id_user": "5de9200d6722370017731969"}, { "id_user": "5e09d16d4d36110017506d7f"}]},"id": "5f29372d690e6f0034edf100","requester_id": "5f29372d690e6f0034edf0ff"}]}}'
413
+ // let test_due = '{ "action": "publish","payload": {"topic": "/5df26badde7e1c001743b63c/requests", "method": "CREATE", "message": [ { "_id": "5f29372d690e6f0034edf100", "status": 200, "preflight": false, "hasBot": true, "participants": ["bot_5df272e8de7e1c001743b645"], "participantsAgents": [], "participantsBots": ["5df272e8de7e1c001743b645"], "request_id": "support-group-MDszsSJlwqQn1_WCh6u", "requester": "5f29371b690e6f0034edf0f5", "lead": "5f29372d690e6f0034edf0ff", "first_text": "ocourse the email is valid ","department": "5df26badde7e1c001743b63e", "agents": [{"user_available": true,"online_status": "online", "number_assigned_requests": 35, "_id": "5e0f2119705a35001725714d","id_project": "5df26badde7e1c001743b63c", "id_user": "5aaa99024c3b110014b478f0", "role": "admin", "createdBy": "5df26ba1de7e1c001743b637","createdAt": "2020-01-03T11:10:17.123Z", "updatedAt": "2020-01-03T11:10:17.123Z", "__v": 0 }, { "user_available": false, "online_status": "offline", "number_assigned_requests": 0, "_id": "5e1a13824437eb0017f712b4", "id_project": "5df26badde7e1c001743b63c","id_user": "5ac7521787f6b50014e0b592", "role": "admin", "createdBy": "5df26ba1de7e1c001743b637", "createdAt": "2020-01-11T18:27:14.657Z","updatedAt": "2020-01-11T18:27:14.657Z", "__v": 0}, { "user_available": false,"online_status": "offline", "number_assigned_requests": 0, "_id": "5df26bdfde7e1c001743b640", "id_project": "5df26badde7e1c001743b63c", "id_user": "5de9200d6722370017731969","role": "admin","createdBy": "5df26ba1de7e1c001743b637", "createdAt": "2019-12-12T16:33:35.244Z", "updatedAt": "2019-12-12T16:33:35.244Z","__v": 0 }, {"user_available": true, "online_status": "online","number_assigned_requests": -11, "_id": "5eb1a3647ac005003480f54d", "id_project": "5df26badde7e1c001743b63c","id_user": "5e09d16d4d36110017506d7f","role": "owner", "createdBy": "5aaa99024c3b110014b478f0","createdAt": "2020-05-05T17:33:24.328Z", "updatedAt": "2020-05-05T17:33:24.328Z","__v": 0}], "sourcePage": "https://www.tiledesk.com/pricing-self-managed/", "language": "en","userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36","attributes": { "departmentId": "5df26badde7e1c001743b63e","departmentName": "Default Department","ipAddress": "115.96.30.154","client": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36","sourcePage": "https://www.tiledesk.com/pricing-self-managed/", "projectId": "5df26badde7e1c001743b63c", "requester_id": "ce31d3fd-a358-49c7-9b9f-5aead8330063", "subtype": "info","decoded_jwt": {"_id": "ce31d3fd-a358-49c7-9b9f-5aead8330063","firstname": "Guest", "id": "ce31d3fd-a358-49c7-9b9f-5aead8330063", "fullName": "Guest ","iat": 1596536604,"aud": "https://tiledesk.com","iss": "https://tiledesk.com","sub": "guest","jti": "702a4a7e-e56a-43cf-aadd-376f7c12f633"}},"id_project": "5df26badde7e1c001743b63c","createdBy": "ce31d3fd-a358-49c7-9b9f-5aead8330063","tags": [], "notes": [],"channel": {"name": "chat21"},"createdAt": "2020-08-04T10:23:41.641Z","updatedAt": "2021-03-25T18:01:13.371Z","__v": 3,"assigned_at": "2020-08-04T10:25:26.059Z","channelOutbound": {"name": "chat21"},"_snapshot": {"agents": [{"id_user": "5aaa99024c3b110014b478f0"}, {"id_user": "5ac7521787f6b50014e0b592"}, {"id_user": "5de9200d6722370017731969"}, { "id_user": "5e09d16d4d36110017506d7f"}]},"id": "5f29372d690e6f0034edf100","requester_id": "5f29372d690e6f0034edf0ff"}]}}'
414
+
415
+ try {
416
+ var json = JSON.parse(message.data);
417
+ // var json = JSON.parse(test_due);
418
+ // this.logger.log('% »»» WebSocketJs - websocket onmessage JSON.parse(message.data) json payload', json.payload);
419
+ // this.logger.log('% »»» WebSocketJs - websocket onmessage JSON.parse(message.data) json payload topic', json.payload.topic);
420
+ // this.logger.log('% »»» WebSocketJs - websocket onmessage JSON.parse(message.data) json ', json);
421
+ } catch (e) {
422
+ that.logger.error('[WEBSOCKET-JS] - This doesn\'t look like a valid JSON: ', message.data);
423
+ return;
424
+ }
425
+
426
+
427
+ // -------------------
428
+ // @ heartCheck
429
+ // -------------------
430
+ that.heartCheck();
431
+
432
+ // --------------------------------------------------------------------------------------------------------------------
433
+ // @ check the action and the message's text - if action is 'heartbeat' and text is ping send the PONG message and return
434
+ // --------------------------------------------------------------------------------------------------------------------
435
+
436
+ if (json.action == "heartbeat") {
437
+
438
+ if (json.payload.message.text == "ping") {
439
+ // -------------------
440
+ // @ send PONG
441
+ // -------------------
442
+ // that.logger.log('[WEBSOCKET-JS] - RECEIVED PING -> SEND PONG MSG');
443
+
444
+ that.send(JSON.stringify(that.pongMsg), 'ON-MESSAGE')
445
+
446
+ } else {
447
+ // nk
448
+ // this.logger.log('[WEBSOCKET-JS] - NOT RECEIVED PING ');
449
+ }
450
+ return;
451
+ }
452
+
453
+ var object = { event: json.payload, data: json };
454
+ if (that.onData) {
455
+ that.onData(json.payload.message, object);
456
+ }
457
+
458
+ var callbackObj = that.callbacks.get(object.event.topic);
459
+ if (callbackObj && callbackObj.onData) {
460
+ callbackObj.onData(json.payload.message, object);
461
+ }
462
+
463
+ if (json && json.payload && json.payload.message && that.isArray(json.payload.message)) {
464
+
465
+ json.payload.message.forEach(element => {
466
+
467
+ //let insUp = that.insertOrUpdate(element);
468
+ let insUp = json.payload.method;
469
+
470
+ var object = { event: json.payload, data: element };
471
+
472
+ var callbackObj = that.callbacks.get(object.event.topic);
473
+
474
+
475
+ if (insUp == "CREATE") {
476
+ if (that.onCreate) {
477
+ that.onCreate(element, object);
478
+ }
479
+
480
+ if (callbackObj) {
481
+ callbackObj.onCreate(element, object);
482
+ }
483
+ }
484
+
485
+ if (insUp == "UPDATE") {
486
+ if (that.onUpdate) {
487
+ that.onUpdate(element, object);
488
+ }
489
+
490
+ if (callbackObj && callbackObj.onUpdate) {
491
+ callbackObj.onUpdate(element, object);
492
+ }
493
+ }
494
+ resolve({ element: element, object: object });
495
+ });
496
+
497
+ } else {
498
+ //let insUp = that.insertOrUpdate(json.payload.message);
499
+ let insUp = json.payload.method;
500
+
501
+ var object = { event: json.payload, data: json };
502
+ var callbackObj = that.callbacks.get(object.event.topic);
503
+
504
+
505
+ if (insUp == "CREATE") {
506
+ if (that.onCreate) {
507
+ that.onCreate(json.payload.message, object);
508
+ }
509
+
510
+ if (callbackObj && callbackObj.onCreate) {
511
+ callbackObj.onCreate(json.payload.message, object);
512
+ }
513
+ }
514
+
515
+ if (insUp == "UPDATE") {
516
+ if (that.onUpdate) {
517
+ that.onUpdate(json.payload.message, object);
518
+ }
519
+
520
+ if (callbackObj && callbackObj.onUpdate) {
521
+ callbackObj.onUpdate(json.payload.message, object);
522
+ }
523
+
524
+ }
525
+
526
+ resolve({ element: json.payload.message, object: object });
527
+ // resolve:
528
+ // $('#messages').after(json.text + '<br>');
529
+ }
530
+ }
531
+ }); // .PROMISE
532
+ }
533
+
534
+ // -------------------------------------
535
+ // !! not use this but the above close()
536
+ // -------------------------------------
537
+
538
+ // closeWebsocket() {
539
+ // this.topics = [];
540
+ // this.callbacks = [];
541
+ // this.logger.log('% »»» WebSocketJs WF - closeWebsocket this.ws ', this.ws)
542
+
543
+ // if (this.ws) {
544
+ // this.ws.close();
545
+ // this.logger.log('% »»» WebSocketJs WF - closeWebsocket ')
546
+ // }
547
+ // }
548
+
549
+
550
+
551
+
552
+ isArray(what) {
553
+ return Object.prototype.toString.call(what) === '[object Array]';
554
+ }
555
+
556
+
557
+ }
@@ -0,0 +1,12 @@
1
+ import { TestBed } from '@angular/core/testing';
2
+
3
+ import { WebsocketService } from './websocket.service';
4
+
5
+ describe('WebsocketServiceService', () => {
6
+ beforeEach(() => TestBed.configureTestingModule({}));
7
+
8
+ it('should be created', () => {
9
+ const service: WebsocketService = TestBed.get(WebsocketService);
10
+ expect(service).toBeTruthy();
11
+ });
12
+ });