@webex/calling 3.12.0-task-refactor.1 → 3.12.0-webex-services-ready.1
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.
- package/dist/CallHistory/CallHistory.js +68 -51
- package/dist/CallHistory/CallHistory.js.map +1 -1
- package/dist/CallRecording/CallRecording.js +160 -0
- package/dist/CallRecording/CallRecording.js.map +1 -0
- package/dist/CallRecording/CallRecording.test.js +257 -0
- package/dist/CallRecording/CallRecording.test.js.map +1 -0
- package/dist/CallRecording/WxcCallRecordingConnector.js +577 -0
- package/dist/CallRecording/WxcCallRecordingConnector.js.map +1 -0
- package/dist/CallRecording/WxcCallRecordingConnector.test.js +652 -0
- package/dist/CallRecording/WxcCallRecordingConnector.test.js.map +1 -0
- package/dist/CallRecording/callRecordingFixtures.js +235 -0
- package/dist/CallRecording/callRecordingFixtures.js.map +1 -0
- package/dist/CallRecording/constants.js +50 -0
- package/dist/CallRecording/constants.js.map +1 -0
- package/dist/CallRecording/types.js +137 -0
- package/dist/CallRecording/types.js.map +1 -0
- package/dist/CallRecording/utils.js +42 -0
- package/dist/CallRecording/utils.js.map +1 -0
- package/dist/CallRecording/utils.test.js +97 -0
- package/dist/CallRecording/utils.test.js.map +1 -0
- package/dist/CallingClient/CallingClient.js +520 -193
- package/dist/CallingClient/CallingClient.js.map +1 -1
- package/dist/CallingClient/CallingClient.test.js +607 -133
- package/dist/CallingClient/CallingClient.test.js.map +1 -1
- package/dist/CallingClient/calling/call.js +599 -454
- package/dist/CallingClient/calling/call.js.map +1 -1
- package/dist/CallingClient/calling/call.test.js +834 -443
- package/dist/CallingClient/calling/call.test.js.map +1 -1
- package/dist/CallingClient/calling/callManager.js +88 -31
- package/dist/CallingClient/calling/callManager.js.map +1 -1
- package/dist/CallingClient/calling/callManager.test.js +123 -0
- package/dist/CallingClient/calling/callManager.test.js.map +1 -1
- package/dist/CallingClient/calling/types.js +2 -0
- package/dist/CallingClient/calling/types.js.map +1 -1
- package/dist/CallingClient/constants.js +30 -3
- package/dist/CallingClient/constants.js.map +1 -1
- package/dist/CallingClient/line/index.js +4 -1
- package/dist/CallingClient/line/index.js.map +1 -1
- package/dist/CallingClient/line/line.test.js +16 -1
- package/dist/CallingClient/line/line.test.js.map +1 -1
- package/dist/CallingClient/registration/register.js +740 -407
- package/dist/CallingClient/registration/register.js.map +1 -1
- package/dist/CallingClient/registration/register.test.js +380 -58
- package/dist/CallingClient/registration/register.test.js.map +1 -1
- package/dist/CallingClient/registration/types.js.map +1 -1
- package/dist/CallingClient/registration/webWorker.js +41 -104
- package/dist/CallingClient/registration/webWorker.js.map +1 -1
- package/dist/CallingClient/registration/webWorker.test.js +39 -153
- package/dist/CallingClient/registration/webWorker.test.js.map +1 -1
- package/dist/CallingClient/registration/webWorkerStr.js +1 -1
- package/dist/CallingClient/registration/webWorkerStr.js.map +1 -1
- package/dist/CallingClient/types.js.map +1 -1
- package/dist/CallingClient/utils/constants.js +58 -0
- package/dist/CallingClient/utils/constants.js.map +1 -0
- package/dist/CallingClient/utils/index.js +63 -0
- package/dist/CallingClient/utils/index.js.map +1 -0
- package/dist/CallingClient/utils/mobiusSocketMapper.js +122 -0
- package/dist/CallingClient/utils/mobiusSocketMapper.js.map +1 -0
- package/dist/CallingClient/utils/mobiusSocketMapper.test.js +221 -0
- package/dist/CallingClient/utils/mobiusSocketMapper.test.js.map +1 -0
- package/dist/CallingClient/utils/request.js +435 -0
- package/dist/CallingClient/utils/request.js.map +1 -0
- package/dist/CallingClient/utils/request.test.js +999 -0
- package/dist/CallingClient/utils/request.test.js.map +1 -0
- package/dist/CallingClient/utils/types.js +7 -0
- package/dist/CallingClient/utils/types.js.map +1 -0
- package/dist/CallingClient/utils/wsFeatureFlag.js +70 -0
- package/dist/CallingClient/utils/wsFeatureFlag.js.map +1 -0
- package/dist/CallingClient/utils/wsFeatureFlag.test.js +139 -0
- package/dist/CallingClient/utils/wsFeatureFlag.test.js.map +1 -0
- package/dist/Contacts/ContactsClient.js +13 -11
- package/dist/Contacts/ContactsClient.js.map +1 -1
- package/dist/Contacts/ContactsClient.test.js +3 -8
- package/dist/Contacts/ContactsClient.test.js.map +1 -1
- package/dist/Events/types.js +38 -11
- package/dist/Events/types.js.map +1 -1
- package/dist/Metrics/index.js +63 -2
- package/dist/Metrics/index.js.map +1 -1
- package/dist/Metrics/index.test.js +357 -1
- package/dist/Metrics/index.test.js.map +1 -1
- package/dist/Metrics/types.js +19 -1
- package/dist/Metrics/types.js.map +1 -1
- package/dist/SDKConnector/types.js.map +1 -1
- package/dist/api.js +56 -0
- package/dist/api.js.map +1 -1
- package/dist/common/Utils.js +161 -53
- package/dist/common/Utils.js.map +1 -1
- package/dist/common/Utils.test.js +207 -46
- package/dist/common/Utils.test.js.map +1 -1
- package/dist/common/testUtil.js +10 -5
- package/dist/common/testUtil.js.map +1 -1
- package/dist/common/types.js +3 -0
- package/dist/common/types.js.map +1 -1
- package/dist/index.js +165 -40
- package/dist/index.js.map +1 -1
- package/dist/mobius-socket/config.js +24 -0
- package/dist/mobius-socket/config.js.map +1 -0
- package/dist/mobius-socket/errors.js +143 -0
- package/dist/mobius-socket/errors.js.map +1 -0
- package/dist/mobius-socket/errors.test.js +20 -0
- package/dist/mobius-socket/errors.test.js.map +1 -0
- package/dist/mobius-socket/index.js +57 -0
- package/dist/mobius-socket/index.js.map +1 -0
- package/dist/mobius-socket/mobius-socket-events.test.js +492 -0
- package/dist/mobius-socket/mobius-socket-events.test.js.map +1 -0
- package/dist/mobius-socket/mobius-socket.js +849 -0
- package/dist/mobius-socket/mobius-socket.js.map +1 -0
- package/dist/mobius-socket/mobius-socket.test.js +1898 -0
- package/dist/mobius-socket/mobius-socket.test.js.map +1 -0
- package/dist/mobius-socket/socket/constants.js +55 -0
- package/dist/mobius-socket/socket/constants.js.map +1 -0
- package/dist/mobius-socket/socket/index.js +15 -0
- package/dist/mobius-socket/socket/index.js.map +1 -0
- package/dist/mobius-socket/socket/socket-base.js +604 -0
- package/dist/mobius-socket/socket/socket-base.js.map +1 -0
- package/dist/mobius-socket/socket/socket.js +19 -0
- package/dist/mobius-socket/socket/socket.js.map +1 -0
- package/dist/mobius-socket/socket/socket.shim.js +26 -0
- package/dist/mobius-socket/socket/socket.shim.js.map +1 -0
- package/dist/mobius-socket/socket/types.js +7 -0
- package/dist/mobius-socket/socket/types.js.map +1 -0
- package/dist/mobius-socket/socket.test.js +727 -0
- package/dist/mobius-socket/socket.test.js.map +1 -0
- package/dist/mobius-socket/test/mocha-helpers.js +23 -0
- package/dist/mobius-socket/test/mocha-helpers.js.map +1 -0
- package/dist/mobius-socket/test/promise-tick.js +28 -0
- package/dist/mobius-socket/test/promise-tick.js.map +1 -0
- package/dist/mobius-socket/types.js +7 -0
- package/dist/mobius-socket/types.js.map +1 -0
- package/dist/module/CallHistory/CallHistory.js +28 -17
- package/dist/module/CallRecording/CallRecording.js +56 -0
- package/dist/module/CallRecording/WxcCallRecordingConnector.js +257 -0
- package/dist/module/CallRecording/callRecordingFixtures.js +201 -0
- package/dist/module/CallRecording/constants.js +28 -0
- package/dist/module/CallRecording/types.js +19 -0
- package/dist/module/CallRecording/utils.js +14 -0
- package/dist/module/CallingClient/CallingClient.js +166 -8
- package/dist/module/CallingClient/calling/call.js +181 -64
- package/dist/module/CallingClient/calling/callManager.js +46 -7
- package/dist/module/CallingClient/calling/types.js +2 -0
- package/dist/module/CallingClient/constants.js +26 -0
- package/dist/module/CallingClient/line/index.js +2 -2
- package/dist/module/CallingClient/registration/register.js +247 -63
- package/dist/module/CallingClient/registration/webWorker.js +42 -61
- package/dist/module/CallingClient/registration/webWorkerStr.js +47 -82
- package/dist/module/CallingClient/utils/constants.js +36 -0
- package/dist/module/CallingClient/utils/index.js +5 -0
- package/dist/module/CallingClient/utils/mobiusSocketMapper.js +72 -0
- package/dist/module/CallingClient/utils/request.js +211 -0
- package/dist/module/CallingClient/utils/types.js +1 -0
- package/dist/module/CallingClient/utils/wsFeatureFlag.js +41 -0
- package/dist/module/Contacts/ContactsClient.js +1 -1
- package/dist/module/Events/types.js +23 -10
- package/dist/module/Metrics/index.js +48 -1
- package/dist/module/Metrics/types.js +18 -0
- package/dist/module/api.js +3 -2
- package/dist/module/common/Utils.js +65 -18
- package/dist/module/common/testUtil.js +6 -1
- package/dist/module/common/types.js +3 -0
- package/dist/module/index.js +7 -3
- package/dist/module/mobius-socket/config.js +15 -0
- package/dist/module/mobius-socket/errors.js +58 -0
- package/dist/module/mobius-socket/index.js +24 -0
- package/dist/module/mobius-socket/mobius-socket.js +593 -0
- package/dist/module/mobius-socket/socket/constants.js +26 -0
- package/dist/module/mobius-socket/socket/index.js +4 -0
- package/dist/module/mobius-socket/socket/socket-base.js +368 -0
- package/dist/module/mobius-socket/socket/socket.js +9 -0
- package/dist/module/mobius-socket/socket/socket.shim.js +12 -0
- package/dist/module/mobius-socket/socket/types.js +1 -0
- package/dist/module/mobius-socket/types.js +1 -0
- package/dist/types/CallHistory/CallHistory.d.ts.map +1 -1
- package/dist/types/CallRecording/CallRecording.d.ts +18 -0
- package/dist/types/CallRecording/CallRecording.d.ts.map +1 -0
- package/dist/types/CallRecording/WxcCallRecordingConnector.d.ts +23 -0
- package/dist/types/CallRecording/WxcCallRecordingConnector.d.ts.map +1 -0
- package/dist/types/CallRecording/callRecordingFixtures.d.ts +61 -0
- package/dist/types/CallRecording/callRecordingFixtures.d.ts.map +1 -0
- package/dist/types/CallRecording/constants.d.ts +29 -0
- package/dist/types/CallRecording/constants.d.ts.map +1 -0
- package/dist/types/CallRecording/types.d.ts +176 -0
- package/dist/types/CallRecording/types.d.ts.map +1 -0
- package/dist/types/CallRecording/utils.d.ts +3 -0
- package/dist/types/CallRecording/utils.d.ts.map +1 -0
- package/dist/types/CallingClient/CallingClient.d.ts +10 -1
- package/dist/types/CallingClient/CallingClient.d.ts.map +1 -1
- package/dist/types/CallingClient/calling/call.d.ts +12 -0
- package/dist/types/CallingClient/calling/call.d.ts.map +1 -1
- package/dist/types/CallingClient/calling/callManager.d.ts +4 -2
- package/dist/types/CallingClient/calling/callManager.d.ts.map +1 -1
- package/dist/types/CallingClient/calling/types.d.ts +34 -9
- package/dist/types/CallingClient/calling/types.d.ts.map +1 -1
- package/dist/types/CallingClient/constants.d.ts +26 -0
- package/dist/types/CallingClient/constants.d.ts.map +1 -1
- package/dist/types/CallingClient/line/index.d.ts +1 -1
- package/dist/types/CallingClient/line/index.d.ts.map +1 -1
- package/dist/types/CallingClient/registration/register.d.ts +9 -2
- package/dist/types/CallingClient/registration/register.d.ts.map +1 -1
- package/dist/types/CallingClient/registration/types.d.ts +6 -3
- package/dist/types/CallingClient/registration/types.d.ts.map +1 -1
- package/dist/types/CallingClient/registration/webWorker.d.ts.map +1 -1
- package/dist/types/CallingClient/registration/webWorkerStr.d.ts +1 -1
- package/dist/types/CallingClient/registration/webWorkerStr.d.ts.map +1 -1
- package/dist/types/CallingClient/types.d.ts +3 -1
- package/dist/types/CallingClient/types.d.ts.map +1 -1
- package/dist/types/CallingClient/utils/constants.d.ts +35 -0
- package/dist/types/CallingClient/utils/constants.d.ts.map +1 -0
- package/dist/types/CallingClient/utils/index.d.ts +6 -0
- package/dist/types/CallingClient/utils/index.d.ts.map +1 -0
- package/dist/types/CallingClient/utils/mobiusSocketMapper.d.ts +5 -0
- package/dist/types/CallingClient/utils/mobiusSocketMapper.d.ts.map +1 -0
- package/dist/types/CallingClient/utils/request.d.ts +28 -0
- package/dist/types/CallingClient/utils/request.d.ts.map +1 -0
- package/dist/types/CallingClient/utils/types.d.ts +34 -0
- package/dist/types/CallingClient/utils/types.d.ts.map +1 -0
- package/dist/types/CallingClient/utils/wsFeatureFlag.d.ts +4 -0
- package/dist/types/CallingClient/utils/wsFeatureFlag.d.ts.map +1 -0
- package/dist/types/Events/types.d.ts +79 -15
- package/dist/types/Events/types.d.ts.map +1 -1
- package/dist/types/Metrics/index.d.ts.map +1 -1
- package/dist/types/Metrics/types.d.ts +19 -2
- package/dist/types/Metrics/types.d.ts.map +1 -1
- package/dist/types/SDKConnector/types.d.ts +39 -12
- package/dist/types/SDKConnector/types.d.ts.map +1 -1
- package/dist/types/api.d.ts +6 -4
- package/dist/types/api.d.ts.map +1 -1
- package/dist/types/common/Utils.d.ts +11 -3
- package/dist/types/common/Utils.d.ts.map +1 -1
- package/dist/types/common/testUtil.d.ts +5 -1
- package/dist/types/common/testUtil.d.ts.map +1 -1
- package/dist/types/common/types.d.ts +9 -1
- package/dist/types/common/types.d.ts.map +1 -1
- package/dist/types/index.d.ts +8 -3
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/mobius-socket/config.d.ts +17 -0
- package/dist/types/mobius-socket/config.d.ts.map +1 -0
- package/dist/types/mobius-socket/errors.d.ts +32 -0
- package/dist/types/mobius-socket/errors.d.ts.map +1 -0
- package/dist/types/mobius-socket/index.d.ts +14 -0
- package/dist/types/mobius-socket/index.d.ts.map +1 -0
- package/dist/types/mobius-socket/mobius-socket.d.ts +48 -0
- package/dist/types/mobius-socket/mobius-socket.d.ts.map +1 -0
- package/dist/types/mobius-socket/socket/constants.d.ts +27 -0
- package/dist/types/mobius-socket/socket/constants.d.ts.map +1 -0
- package/dist/types/mobius-socket/socket/index.d.ts +5 -0
- package/dist/types/mobius-socket/socket/index.d.ts.map +1 -0
- package/dist/types/mobius-socket/socket/socket-base.d.ts +43 -0
- package/dist/types/mobius-socket/socket/socket-base.d.ts.map +1 -0
- package/dist/types/mobius-socket/socket/socket.d.ts +6 -0
- package/dist/types/mobius-socket/socket/socket.d.ts.map +1 -0
- package/dist/types/mobius-socket/socket/socket.shim.d.ts +6 -0
- package/dist/types/mobius-socket/socket/socket.shim.d.ts.map +1 -0
- package/dist/types/mobius-socket/socket/types.d.ts +61 -0
- package/dist/types/mobius-socket/socket/types.d.ts.map +1 -0
- package/dist/types/mobius-socket/types.d.ts +21 -0
- package/dist/types/mobius-socket/types.d.ts.map +1 -0
- package/package.json +20 -5
- package/src/mobius-socket/socket/socket.shim.ts +22 -0
- package/src/mobius-socket/socket/socket.ts +14 -0
|
@@ -0,0 +1,593 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2015-2020 Cisco Systems, Inc. See LICENSE file
|
|
3
|
+
*/
|
|
4
|
+
import { EventEmitter } from 'events';
|
|
5
|
+
import backoff from 'backoff';
|
|
6
|
+
import Socket from './socket';
|
|
7
|
+
import { BadRequest, Forbidden, NotAuthorized, UnknownResponse } from './errors';
|
|
8
|
+
import { MOBIUS_SOCKET_4001_EVENT } from './socket/constants';
|
|
9
|
+
const normalReconnectReasons = ['idle', 'done (forced)'];
|
|
10
|
+
const MOBIUS_SOCKET_NAMESPACE = 'MobiusSocket';
|
|
11
|
+
const TOKEN_REFRESH_INTERVAL_MS = 1 * 60 * 60 * 1000;
|
|
12
|
+
function normalizeMobiusAuthToken(token) {
|
|
13
|
+
return token.replace(/^Bearer\s+/i, '');
|
|
14
|
+
}
|
|
15
|
+
class MobiusSocket extends EventEmitter {
|
|
16
|
+
webex;
|
|
17
|
+
config;
|
|
18
|
+
logger;
|
|
19
|
+
connected;
|
|
20
|
+
connecting;
|
|
21
|
+
hasEverConnected;
|
|
22
|
+
socket;
|
|
23
|
+
backoffCall;
|
|
24
|
+
shutdownSwitchoverBackoffCall;
|
|
25
|
+
seenAsyncEventIds;
|
|
26
|
+
connectPromise;
|
|
27
|
+
socketUrl;
|
|
28
|
+
tokenRefreshTimer;
|
|
29
|
+
tokenRefreshInFlight;
|
|
30
|
+
constructor(webex, config = {}) {
|
|
31
|
+
super();
|
|
32
|
+
if (!webex) {
|
|
33
|
+
throw new Error('A Webex instance is required when initializing MobiusSocket');
|
|
34
|
+
}
|
|
35
|
+
this.webex = webex;
|
|
36
|
+
this.config = config;
|
|
37
|
+
this.logger = webex.logger || console;
|
|
38
|
+
this.connected = false;
|
|
39
|
+
this.connecting = false;
|
|
40
|
+
this.hasEverConnected = false;
|
|
41
|
+
this.socket = undefined;
|
|
42
|
+
this.backoffCall = undefined;
|
|
43
|
+
this.shutdownSwitchoverBackoffCall = undefined;
|
|
44
|
+
this.seenAsyncEventIds = new Map();
|
|
45
|
+
this.connectPromise = undefined;
|
|
46
|
+
this.tokenRefreshTimer = undefined;
|
|
47
|
+
this.tokenRefreshInFlight = undefined;
|
|
48
|
+
}
|
|
49
|
+
off(eventName, listener) {
|
|
50
|
+
if (listener) {
|
|
51
|
+
return super.off(eventName, listener);
|
|
52
|
+
}
|
|
53
|
+
this.removeAllListeners(eventName);
|
|
54
|
+
return this;
|
|
55
|
+
}
|
|
56
|
+
attachSocketEventListeners(socket) {
|
|
57
|
+
socket.on('close', (event) => this.onclose(event, socket));
|
|
58
|
+
socket.on('message', (event) => this.onmessage(event));
|
|
59
|
+
}
|
|
60
|
+
trackAsyncEventAndShouldSuppressDuplicate(envelope) {
|
|
61
|
+
if (envelope?.type !== 'async_event' || !envelope.eventId) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
if (this.seenAsyncEventIds.has(envelope.eventId)) {
|
|
65
|
+
const previousValue = this.seenAsyncEventIds.get(envelope.eventId) || true;
|
|
66
|
+
this.seenAsyncEventIds.delete(envelope.eventId);
|
|
67
|
+
this.seenAsyncEventIds.set(envelope.eventId, previousValue);
|
|
68
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: duplicate async_event suppressed, eventId=${envelope.eventId}`);
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
this.logger.log(`${MOBIUS_SOCKET_NAMESPACE}: tracking async_event, eventId=${envelope.eventId}`);
|
|
72
|
+
this.seenAsyncEventIds.set(envelope.eventId, true);
|
|
73
|
+
if (this.config.dedupCacheMaxSize &&
|
|
74
|
+
this.seenAsyncEventIds.size > this.config.dedupCacheMaxSize) {
|
|
75
|
+
const oldestEventId = this.seenAsyncEventIds.keys().next().value || '';
|
|
76
|
+
this.seenAsyncEventIds.delete(oldestEventId);
|
|
77
|
+
this.logger.log(`${MOBIUS_SOCKET_NAMESPACE}: evicted oldest async_event from dedup cache, eventId=${oldestEventId}`);
|
|
78
|
+
}
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
handleImminentShutdown() {
|
|
82
|
+
const oldSocket = this.socket;
|
|
83
|
+
try {
|
|
84
|
+
if (this.shutdownSwitchoverBackoffCall) {
|
|
85
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: [shutdown] switchover already in progress`);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: [shutdown] switchover start`);
|
|
89
|
+
this.connectWithBackoff(undefined, {
|
|
90
|
+
isShutdownSwitchover: true,
|
|
91
|
+
attemptOptions: {
|
|
92
|
+
isShutdownSwitchover: true,
|
|
93
|
+
onSuccess: (newSocket, webSocketUrl) => {
|
|
94
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: [shutdown] switchover connected, url: ${webSocketUrl}`);
|
|
95
|
+
newSocket.connecting = false;
|
|
96
|
+
newSocket.connected = true;
|
|
97
|
+
this.socket = newSocket;
|
|
98
|
+
this.connected = true;
|
|
99
|
+
this.emitEvent('event:mobius_shutdown_switchover_complete', {
|
|
100
|
+
url: webSocketUrl,
|
|
101
|
+
});
|
|
102
|
+
if (oldSocket) {
|
|
103
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: [shutdown] old socket retained; server will close with 4001`);
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
})
|
|
108
|
+
.then(() => {
|
|
109
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: [shutdown] switchover completed successfully`);
|
|
110
|
+
})
|
|
111
|
+
.catch((err) => {
|
|
112
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: [shutdown] switchover exhausted retries; will fall back to normal reconnection: `, err);
|
|
113
|
+
this.emitEvent('event:mobius_shutdown_switchover_failed', { reason: err });
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
catch (e) {
|
|
117
|
+
this.logger.error(`${MOBIUS_SOCKET_NAMESPACE}: [shutdown] error during switchover`, e);
|
|
118
|
+
this.shutdownSwitchoverBackoffCall = undefined;
|
|
119
|
+
this.emitEvent('event:mobius_shutdown_switchover_failed', { reason: e });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
getConnectedWebSocketUrl() {
|
|
123
|
+
if (!this.socket?.connected) {
|
|
124
|
+
return undefined;
|
|
125
|
+
}
|
|
126
|
+
return this.socket.url;
|
|
127
|
+
}
|
|
128
|
+
sendWssRequest(payload, options = {}) {
|
|
129
|
+
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
|
130
|
+
return Promise.reject(new Error('`payload` is required'));
|
|
131
|
+
}
|
|
132
|
+
if (!this.socket || !this.socket.connected) {
|
|
133
|
+
return Promise.reject(new Error('Mobius socket is not connected'));
|
|
134
|
+
}
|
|
135
|
+
return this.socket.sendRequest(payload, { timeout: options.timeout });
|
|
136
|
+
}
|
|
137
|
+
isConnected() {
|
|
138
|
+
return this.connected;
|
|
139
|
+
}
|
|
140
|
+
connect(webSocketUrl) {
|
|
141
|
+
if (this.connectPromise) {
|
|
142
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: connection already in progress, returning existing promise`);
|
|
143
|
+
return this.connectPromise;
|
|
144
|
+
}
|
|
145
|
+
if (this.socket?.connected || this.socket?.connecting) {
|
|
146
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: already connected, will not connect again`);
|
|
147
|
+
return Promise.resolve();
|
|
148
|
+
}
|
|
149
|
+
if (webSocketUrl && this.socketUrl && webSocketUrl !== this.socketUrl) {
|
|
150
|
+
this.hasEverConnected = false;
|
|
151
|
+
}
|
|
152
|
+
if (webSocketUrl) {
|
|
153
|
+
this.socketUrl = webSocketUrl;
|
|
154
|
+
}
|
|
155
|
+
this.connecting = true;
|
|
156
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: starting connection attempt${Number(this.config.initialConnectionMaxRetries) === 0 && !this.hasEverConnected
|
|
157
|
+
? ' (initial retries disabled)'
|
|
158
|
+
: ''}`);
|
|
159
|
+
const connectPromise = Promise.resolve(this.webex.internal.device.registered || this.webex.internal.device.register?.())
|
|
160
|
+
.then(() => {
|
|
161
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: connecting`);
|
|
162
|
+
return this.connectWithBackoff(this.socketUrl);
|
|
163
|
+
})
|
|
164
|
+
.finally(() => {
|
|
165
|
+
this.connectPromise = undefined;
|
|
166
|
+
});
|
|
167
|
+
this.connectPromise = connectPromise;
|
|
168
|
+
return connectPromise;
|
|
169
|
+
}
|
|
170
|
+
disconnect(options) {
|
|
171
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}#disconnect: connecting state: ${this.connecting},
|
|
172
|
+
connected state: ${this.connected}, socket exists: ${!!this.socket},
|
|
173
|
+
options: ${JSON.stringify(options)}`);
|
|
174
|
+
if (this.backoffCall) {
|
|
175
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: aborting connection`);
|
|
176
|
+
this.backoffCall.abort();
|
|
177
|
+
this.backoffCall = undefined;
|
|
178
|
+
}
|
|
179
|
+
if (this.shutdownSwitchoverBackoffCall) {
|
|
180
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: aborting shutdown switchover connection`);
|
|
181
|
+
this.shutdownSwitchoverBackoffCall.abort();
|
|
182
|
+
this.shutdownSwitchoverBackoffCall = undefined;
|
|
183
|
+
}
|
|
184
|
+
this.connectPromise = undefined;
|
|
185
|
+
this.seenAsyncEventIds.clear();
|
|
186
|
+
if (!this.socket) {
|
|
187
|
+
this.connected = false;
|
|
188
|
+
this.stopTokenRefreshTimer();
|
|
189
|
+
return Promise.resolve();
|
|
190
|
+
}
|
|
191
|
+
this.socket.removeAllListeners('message');
|
|
192
|
+
this.socket.connecting = false;
|
|
193
|
+
this.socket.connected = false;
|
|
194
|
+
return Promise.resolve(this.socket.close(options || undefined)).finally(() => {
|
|
195
|
+
this.connected = false;
|
|
196
|
+
this.stopTokenRefreshTimer();
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
prepareUrl(webSocketUrl) {
|
|
200
|
+
if (!webSocketUrl) {
|
|
201
|
+
webSocketUrl = this.webex.internal.device.webSocketUrl || '';
|
|
202
|
+
}
|
|
203
|
+
return Promise.resolve(webSocketUrl);
|
|
204
|
+
}
|
|
205
|
+
attemptConnection(socketUrl, callback, options = {}) {
|
|
206
|
+
const { isShutdownSwitchover = false, attemptOptions = {} } = options;
|
|
207
|
+
const { onSuccess = null } = attemptOptions;
|
|
208
|
+
const socket = new Socket();
|
|
209
|
+
socket.connecting = true;
|
|
210
|
+
let newWSUrl;
|
|
211
|
+
this.attachSocketEventListeners(socket);
|
|
212
|
+
const backoffCall = isShutdownSwitchover
|
|
213
|
+
? this.shutdownSwitchoverBackoffCall
|
|
214
|
+
: this.backoffCall;
|
|
215
|
+
if (!backoffCall) {
|
|
216
|
+
const mode = isShutdownSwitchover ? 'switchover backoff call' : 'backoffCall';
|
|
217
|
+
const msg = `${MOBIUS_SOCKET_NAMESPACE}: prevent socket open when ${mode} no longer defined`;
|
|
218
|
+
const err = new Error(msg);
|
|
219
|
+
this.logger.info(msg);
|
|
220
|
+
callback(err);
|
|
221
|
+
return Promise.reject(err);
|
|
222
|
+
}
|
|
223
|
+
if (!isShutdownSwitchover) {
|
|
224
|
+
this.socket = socket;
|
|
225
|
+
}
|
|
226
|
+
return this.prepareAndOpenSocket(socket, socketUrl, isShutdownSwitchover)
|
|
227
|
+
.then((webSocketUrl) => {
|
|
228
|
+
newWSUrl = webSocketUrl;
|
|
229
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: ${isShutdownSwitchover ? '[shutdown] switchover' : ''} connected to mobius socket, success, url: ${newWSUrl}`);
|
|
230
|
+
if (onSuccess) {
|
|
231
|
+
onSuccess(socket, webSocketUrl);
|
|
232
|
+
callback();
|
|
233
|
+
return Promise.resolve();
|
|
234
|
+
}
|
|
235
|
+
callback();
|
|
236
|
+
return Promise.resolve();
|
|
237
|
+
})
|
|
238
|
+
.catch((reason) => {
|
|
239
|
+
if (isShutdownSwitchover) {
|
|
240
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: [shutdown] switchover attempt failed`, reason);
|
|
241
|
+
return callback(reason);
|
|
242
|
+
}
|
|
243
|
+
const backoffCallNormal = this.backoffCall;
|
|
244
|
+
if (reason.code !== 1006 && backoffCallNormal && backoffCallNormal?.getNumRetries() > 0) {
|
|
245
|
+
this.emitEvent('connection_failed', reason, {
|
|
246
|
+
retries: backoffCallNormal?.getNumRetries(),
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: connection attempt failed`, reason, backoffCallNormal?.getNumRetries() === 0 ? reason.stack : '');
|
|
250
|
+
if (reason instanceof UnknownResponse) {
|
|
251
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: received unknown response code, refreshing device registration`);
|
|
252
|
+
return this.webex.internal.device
|
|
253
|
+
.refresh?.()
|
|
254
|
+
.then(() => callback(reason));
|
|
255
|
+
}
|
|
256
|
+
if (reason instanceof NotAuthorized) {
|
|
257
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: received authorization error, reauthorizing`);
|
|
258
|
+
return this.webex.credentials
|
|
259
|
+
.refresh?.({ force: true })
|
|
260
|
+
.then(() => callback(reason));
|
|
261
|
+
}
|
|
262
|
+
if (reason instanceof BadRequest || reason instanceof Forbidden) {
|
|
263
|
+
this.logger.warn(`${MOBIUS_SOCKET_NAMESPACE}: received unrecoverable response from ${MOBIUS_SOCKET_NAMESPACE}`);
|
|
264
|
+
backoffCallNormal?.abort();
|
|
265
|
+
return callback(reason);
|
|
266
|
+
}
|
|
267
|
+
return callback(reason);
|
|
268
|
+
})
|
|
269
|
+
.catch((reason) => {
|
|
270
|
+
this.logger.error(`${MOBIUS_SOCKET_NAMESPACE}: failed to handle connection failure`, reason);
|
|
271
|
+
callback(reason);
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
prepareAndOpenSocket(socket, socketUrl, isShutdownSwitchover = false) {
|
|
275
|
+
const logPrefix = isShutdownSwitchover ? '[shutdown] switchover' : 'connection';
|
|
276
|
+
return Promise.all([this.prepareUrl(socketUrl), this.webex.credentials.getUserToken()]).then(([webSocketUrl, token]) => {
|
|
277
|
+
let options = {
|
|
278
|
+
forceCloseDelay: this.config.forceCloseDelay,
|
|
279
|
+
wssResponseTimeout: this.config.wssResponseTimeout,
|
|
280
|
+
token: normalizeMobiusAuthToken(token.toString()),
|
|
281
|
+
refreshToken: () => this.refreshToken(),
|
|
282
|
+
trackingId: `${this.webex.sessionId}_${Date.now()}`,
|
|
283
|
+
logger: this.logger,
|
|
284
|
+
};
|
|
285
|
+
if (this.webex.config.defaultMobiusSocketOptions) {
|
|
286
|
+
const customOptionsMsg = isShutdownSwitchover
|
|
287
|
+
? 'setting custom options for switchover'
|
|
288
|
+
: 'setting custom options';
|
|
289
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: ${customOptionsMsg}`);
|
|
290
|
+
options = { ...options, ...this.webex.config.defaultMobiusSocketOptions };
|
|
291
|
+
}
|
|
292
|
+
if (!isShutdownSwitchover) {
|
|
293
|
+
this.socket = socket;
|
|
294
|
+
}
|
|
295
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE} ${logPrefix} url: ${webSocketUrl}`);
|
|
296
|
+
return socket.open(webSocketUrl, options).then(() => webSocketUrl);
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
connectWithBackoff(webSocketUrl, context = {}) {
|
|
300
|
+
const { isShutdownSwitchover = false, attemptOptions = {} } = context;
|
|
301
|
+
return new Promise((resolve, reject) => {
|
|
302
|
+
let call;
|
|
303
|
+
const isInitialConnect = !isShutdownSwitchover && !this.hasEverConnected;
|
|
304
|
+
const initialRetryLimit = this.config.initialConnectionMaxRetries == null
|
|
305
|
+
? null
|
|
306
|
+
: Number(this.config.initialConnectionMaxRetries);
|
|
307
|
+
const isInitialConnectWithoutRetries = isInitialConnect && initialRetryLimit === 0;
|
|
308
|
+
const onComplete = (err) => {
|
|
309
|
+
if (isShutdownSwitchover) {
|
|
310
|
+
this.shutdownSwitchoverBackoffCall = undefined;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
this.backoffCall = undefined;
|
|
314
|
+
}
|
|
315
|
+
if (err) {
|
|
316
|
+
const msg = isShutdownSwitchover
|
|
317
|
+
? `[shutdown] switchover failed after ${call.getNumRetries()} retries`
|
|
318
|
+
: `failed to connect after ${call.getNumRetries()} retries`;
|
|
319
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: ${msg}; ${err}`);
|
|
320
|
+
if (!isShutdownSwitchover && this.socket) {
|
|
321
|
+
this.socket.connecting = false;
|
|
322
|
+
this.socket.connected = false;
|
|
323
|
+
}
|
|
324
|
+
return reject(err);
|
|
325
|
+
}
|
|
326
|
+
if (!isShutdownSwitchover && this.socket) {
|
|
327
|
+
this.socket.connecting = false;
|
|
328
|
+
this.socket.connected = true;
|
|
329
|
+
}
|
|
330
|
+
if (!isShutdownSwitchover) {
|
|
331
|
+
this.connecting = false;
|
|
332
|
+
this.connected = true;
|
|
333
|
+
this.hasEverConnected = true;
|
|
334
|
+
this.startTokenRefreshTimer();
|
|
335
|
+
this.emitEvent('online');
|
|
336
|
+
}
|
|
337
|
+
return resolve();
|
|
338
|
+
};
|
|
339
|
+
call = backoff.call((callback) => {
|
|
340
|
+
const attemptNum = call.getNumRetries();
|
|
341
|
+
const attemptLogPrefix = isShutdownSwitchover ? '[shutdown] switchover' : 'connection';
|
|
342
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: executing ${attemptLogPrefix} attempt ${attemptNum}`);
|
|
343
|
+
this.attemptConnection(webSocketUrl, callback, attemptOptions);
|
|
344
|
+
}, (err) => onComplete(err));
|
|
345
|
+
call.setStrategy(new backoff.ExponentialStrategy({
|
|
346
|
+
initialDelay: this.config.backoffTimeReset,
|
|
347
|
+
maxDelay: this.config.backoffTimeMax,
|
|
348
|
+
}));
|
|
349
|
+
if (isInitialConnectWithoutRetries) {
|
|
350
|
+
call.retryIf(() => false);
|
|
351
|
+
}
|
|
352
|
+
else if (isInitialConnect && initialRetryLimit !== null && initialRetryLimit > 0) {
|
|
353
|
+
call.failAfter(initialRetryLimit);
|
|
354
|
+
}
|
|
355
|
+
else if (this.config.maxRetries) {
|
|
356
|
+
call.failAfter(this.config.maxRetries);
|
|
357
|
+
}
|
|
358
|
+
if (isShutdownSwitchover) {
|
|
359
|
+
this.shutdownSwitchoverBackoffCall = call;
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
this.backoffCall = call;
|
|
363
|
+
}
|
|
364
|
+
call.on('abort', () => {
|
|
365
|
+
const msg = isShutdownSwitchover ? 'Shutdown Switchover' : 'Connection';
|
|
366
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: ${msg} aborted`);
|
|
367
|
+
reject(new Error(`MobiusSocket ${msg} Aborted`));
|
|
368
|
+
});
|
|
369
|
+
call.on('callback', (err) => {
|
|
370
|
+
if (err) {
|
|
371
|
+
if (isInitialConnectWithoutRetries) {
|
|
372
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: initial connect failed; retries already disabled`);
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
const number = call.getNumRetries();
|
|
376
|
+
const delay = Math.min(call.strategy_.nextBackoffDelay_, this.config.backoffTimeMax || Infinity);
|
|
377
|
+
const callbackLogPrefix = isShutdownSwitchover ? '[shutdown] switchover' : '';
|
|
378
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: ${callbackLogPrefix} failed to connect; attempting retry ${number + 1} in ${delay} ms`);
|
|
379
|
+
if (process.env.NODE_ENV === 'development') {
|
|
380
|
+
this.logger.debug(`${MOBIUS_SOCKET_NAMESPACE}: `, err, err.stack);
|
|
381
|
+
}
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
this.logger.info(`${MOBIUS_SOCKET_NAMESPACE}: connected`);
|
|
385
|
+
});
|
|
386
|
+
call.start();
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
emitEvent(eventName, ...args) {
|
|
390
|
+
try {
|
|
391
|
+
if (!eventName) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
this.emit(eventName, ...args);
|
|
395
|
+
}
|
|
396
|
+
catch (error) {
|
|
397
|
+
this.logger.error(`${MOBIUS_SOCKET_NAMESPACE}: error occurred in event handler:`, error, ' with args: ', [eventName, ...args]);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
startTokenRefreshTimer() {
|
|
401
|
+
if (this.tokenRefreshTimer || !this.connected) {
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
this.tokenRefreshTimer = setInterval(() => {
|
|
405
|
+
this.refreshToken().catch((error) => {
|
|
406
|
+
this.logger.error(`${MOBIUS_SOCKET_NAMESPACE}: periodic token refresh failed`, error);
|
|
407
|
+
});
|
|
408
|
+
}, TOKEN_REFRESH_INTERVAL_MS);
|
|
409
|
+
}
|
|
410
|
+
stopTokenRefreshTimer() {
|
|
411
|
+
if (!this.tokenRefreshTimer) {
|
|
412
|
+
return;
|
|
413
|
+
}
|
|
414
|
+
clearInterval(this.tokenRefreshTimer);
|
|
415
|
+
this.tokenRefreshTimer = undefined;
|
|
416
|
+
}
|
|
417
|
+
refreshToken() {
|
|
418
|
+
if (this.tokenRefreshInFlight) {
|
|
419
|
+
return this.tokenRefreshInFlight;
|
|
420
|
+
}
|
|
421
|
+
if (!this.connected) {
|
|
422
|
+
this.stopTokenRefreshTimer();
|
|
423
|
+
return Promise.resolve();
|
|
424
|
+
}
|
|
425
|
+
const tokenPromise = this.webex.credentials.canRefresh
|
|
426
|
+
? this.webex.credentials
|
|
427
|
+
.refresh?.({ force: true })
|
|
428
|
+
?.then(() => this.webex.credentials.getUserToken())
|
|
429
|
+
: this.webex.credentials.getUserToken();
|
|
430
|
+
this.tokenRefreshInFlight = tokenPromise
|
|
431
|
+
.then((token) => {
|
|
432
|
+
if (!token) {
|
|
433
|
+
throw new Error('Mobius token refresh did not return a token');
|
|
434
|
+
}
|
|
435
|
+
const refreshedToken = normalizeMobiusAuthToken(token.toString());
|
|
436
|
+
if (!this.socket?.connected) {
|
|
437
|
+
this.logger.warn(`${MOBIUS_SOCKET_NAMESPACE}: socket is not connected, skipping token refresh`);
|
|
438
|
+
return undefined;
|
|
439
|
+
}
|
|
440
|
+
return this.socket.refresh(refreshedToken);
|
|
441
|
+
})
|
|
442
|
+
.catch((error) => {
|
|
443
|
+
this.logger.error(`${MOBIUS_SOCKET_NAMESPACE}: failed to refresh/re-auth Mobius socket`, error);
|
|
444
|
+
throw error;
|
|
445
|
+
})
|
|
446
|
+
.finally(() => {
|
|
447
|
+
this.tokenRefreshInFlight = undefined;
|
|
448
|
+
});
|
|
449
|
+
return this.tokenRefreshInFlight;
|
|
450
|
+
}
|
|
451
|
+
async onclose(event, sourceSocket) {
|
|
452
|
+
const loggerContext = {
|
|
453
|
+
file: 'mobius-socket.ts',
|
|
454
|
+
method: 'onclose',
|
|
455
|
+
};
|
|
456
|
+
try {
|
|
457
|
+
const reason = event.reason && event.reason.toLowerCase();
|
|
458
|
+
let socketUrl;
|
|
459
|
+
const isActiveSocket = sourceSocket === this.socket;
|
|
460
|
+
if (sourceSocket) {
|
|
461
|
+
socketUrl = sourceSocket.url;
|
|
462
|
+
}
|
|
463
|
+
if (isActiveSocket) {
|
|
464
|
+
if (this.socket) {
|
|
465
|
+
this.socket.removeAllListeners();
|
|
466
|
+
this.socket = undefined;
|
|
467
|
+
this.emitEvent('offline', event);
|
|
468
|
+
}
|
|
469
|
+
this.connecting = false;
|
|
470
|
+
this.connected = false;
|
|
471
|
+
this.stopTokenRefreshTimer();
|
|
472
|
+
}
|
|
473
|
+
else {
|
|
474
|
+
this.logger.info(`[shutdown] non-active socket closed, code=${event.code}`, loggerContext);
|
|
475
|
+
if (sourceSocket) {
|
|
476
|
+
sourceSocket.removeAllListeners();
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
switch (event.code) {
|
|
480
|
+
case 1003:
|
|
481
|
+
this.logger.info(`service rejected last message; will not reconnect: ${event.reason}`, loggerContext);
|
|
482
|
+
if (isActiveSocket)
|
|
483
|
+
this.emitEvent('offline.permanent', event);
|
|
484
|
+
break;
|
|
485
|
+
case 4000:
|
|
486
|
+
this.logger.info(`socket replaced; will not reconnect`, loggerContext);
|
|
487
|
+
if (isActiveSocket)
|
|
488
|
+
this.emitEvent('offline.replaced', event);
|
|
489
|
+
break;
|
|
490
|
+
case 4001:
|
|
491
|
+
this.logger.info(`socket closed with 4001; will not reconnect`, loggerContext);
|
|
492
|
+
this.emitEvent('event:async_event', MOBIUS_SOCKET_4001_EVENT);
|
|
493
|
+
if (isActiveSocket)
|
|
494
|
+
this.emitEvent('offline.permanent', event);
|
|
495
|
+
break;
|
|
496
|
+
case 1000:
|
|
497
|
+
case 1001:
|
|
498
|
+
this.logger.info(`socket disconnected; ${event.reason}`, loggerContext);
|
|
499
|
+
if (isActiveSocket)
|
|
500
|
+
this.emitEvent('offline.permanent', event);
|
|
501
|
+
break;
|
|
502
|
+
case 1005:
|
|
503
|
+
case 1006:
|
|
504
|
+
case 1011:
|
|
505
|
+
case 1012:
|
|
506
|
+
this.logger.info(`socket disconnected; reconnecting`, loggerContext);
|
|
507
|
+
if (isActiveSocket) {
|
|
508
|
+
this.emitEvent('offline.transient', event);
|
|
509
|
+
this.reconnect(socketUrl);
|
|
510
|
+
}
|
|
511
|
+
break;
|
|
512
|
+
case 3050:
|
|
513
|
+
if (reason && normalReconnectReasons.includes(reason)) {
|
|
514
|
+
this.logger.info(`socket disconnected; reconnecting`, loggerContext);
|
|
515
|
+
if (isActiveSocket) {
|
|
516
|
+
this.emitEvent('offline.transient', event);
|
|
517
|
+
this.reconnect(socketUrl);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
else {
|
|
521
|
+
this.logger.info(`socket disconnected; will not reconnect: ${event.reason}`, loggerContext);
|
|
522
|
+
if (isActiveSocket)
|
|
523
|
+
this.emitEvent('offline.permanent', event);
|
|
524
|
+
}
|
|
525
|
+
break;
|
|
526
|
+
case 4401:
|
|
527
|
+
case 4403:
|
|
528
|
+
case 4404:
|
|
529
|
+
this.logger.error(`onclose, statusCode=${event.code}`, loggerContext);
|
|
530
|
+
await this.refreshToken().catch((error) => {
|
|
531
|
+
this.logger.error(`${MOBIUS_SOCKET_NAMESPACE}: periodic token refresh failed`, error);
|
|
532
|
+
});
|
|
533
|
+
await this.reconnect(this.socket?.url);
|
|
534
|
+
break;
|
|
535
|
+
case 4429:
|
|
536
|
+
this.logger.error(`too many requests, statusCode=${event.code}`, loggerContext);
|
|
537
|
+
if (isActiveSocket)
|
|
538
|
+
this.emitEvent('offline.permanent', event);
|
|
539
|
+
break;
|
|
540
|
+
default:
|
|
541
|
+
this.logger.info(`socket disconnected unexpectedly; will not reconnect`, loggerContext);
|
|
542
|
+
if (isActiveSocket)
|
|
543
|
+
this.emitEvent('offline.permanent', event);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
catch (error) {
|
|
547
|
+
this.logger.error(`error occurred in close handler`, error, loggerContext);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
onmessage(event) {
|
|
551
|
+
const loggerContext = {
|
|
552
|
+
file: 'mobius-socket.ts',
|
|
553
|
+
method: 'onmessage',
|
|
554
|
+
};
|
|
555
|
+
const envelope = event.data;
|
|
556
|
+
this.logger.debug(`message envelope: `, envelope, loggerContext);
|
|
557
|
+
if (envelope && envelope.type === 'shutdown') {
|
|
558
|
+
this.logger.info(`[shutdown] imminent shutdown message received`, loggerContext);
|
|
559
|
+
this.emitEvent('event:mobius_shutdown_imminent', envelope);
|
|
560
|
+
this.handleImminentShutdown();
|
|
561
|
+
return Promise.resolve();
|
|
562
|
+
}
|
|
563
|
+
if (this.trackAsyncEventAndShouldSuppressDuplicate(envelope)) {
|
|
564
|
+
return Promise.resolve();
|
|
565
|
+
}
|
|
566
|
+
if (envelope.type) {
|
|
567
|
+
this.emitEvent(`event:${envelope.type}`, envelope);
|
|
568
|
+
}
|
|
569
|
+
const data = envelope.data || envelope;
|
|
570
|
+
const eventType = data?.eventType || envelope.eventType;
|
|
571
|
+
if (!eventType) {
|
|
572
|
+
this.emitEvent('event', envelope);
|
|
573
|
+
return Promise.resolve();
|
|
574
|
+
}
|
|
575
|
+
try {
|
|
576
|
+
this.emitEvent('event', envelope);
|
|
577
|
+
const [namespace] = eventType.split('.');
|
|
578
|
+
this.emitEvent(`event:${namespace}`, envelope);
|
|
579
|
+
if (namespace !== eventType) {
|
|
580
|
+
this.emitEvent(`event:${eventType}`, envelope);
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
catch (reason) {
|
|
584
|
+
this.logger.error(`error occurred processing socket message`, reason, loggerContext);
|
|
585
|
+
}
|
|
586
|
+
return Promise.resolve();
|
|
587
|
+
}
|
|
588
|
+
reconnect(webSocketUrl) {
|
|
589
|
+
this.logger.info(`reconnecting`, { file: 'mobius-socket.ts', method: 'reconnect' });
|
|
590
|
+
return this.connect(webSocketUrl || this.socketUrl);
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
export default MobiusSocket;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const SOCKET_READY_STATE = Object.freeze({
|
|
2
|
+
CONNECTING: 0,
|
|
3
|
+
OPEN: 1,
|
|
4
|
+
CLOSING: 2,
|
|
5
|
+
CLOSED: 3,
|
|
6
|
+
});
|
|
7
|
+
export const MESSAGE_TYPES = Object.freeze({
|
|
8
|
+
AUTH: 'auth',
|
|
9
|
+
EVENT_ACK: 'event_ack',
|
|
10
|
+
});
|
|
11
|
+
export const MOBIUS_SOCKET_4001_EVENT = {
|
|
12
|
+
type: 'async_event',
|
|
13
|
+
trackingId: '4001-event',
|
|
14
|
+
eventId: '4001-event-id',
|
|
15
|
+
data: {
|
|
16
|
+
eventType: 'registration.down',
|
|
17
|
+
deviceInfo: {
|
|
18
|
+
userId: '4001-user-id',
|
|
19
|
+
device: {
|
|
20
|
+
deviceId: '4001-device-id',
|
|
21
|
+
uri: '4001-device-uri',
|
|
22
|
+
status: 'inactive',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
};
|