@voicenter-team/events-sdk 0.0.19 → 0.0.20
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/voicenter-events-sdk.cjs.js +14 -14
- package/dist/voicenter-events-sdk.cjs.js.map +1 -1
- package/dist/voicenter-events-sdk.d.ts +12 -11
- package/dist/voicenter-events-sdk.es.js +1060 -1046
- package/dist/voicenter-events-sdk.es.js.map +1 -1
- package/dist/voicenter-events-sdk.iife.js +13 -13
- package/dist/voicenter-events-sdk.iife.js.map +1 -1
- package/dist/voicenter-events-sdk.umd.js +13 -13
- package/dist/voicenter-events-sdk.umd.js.map +1 -1
- package/docs/src/.vuepress/components/Demo.vue +16 -4
- package/package.json +1 -1
- package/src/classes/auth/auth.class.ts +8 -2
- package/src/classes/events-sdk/events-sdk-default-options.ts +1 -1
- package/src/classes/events-sdk/events-sdk.class.ts +119 -98
- package/src/classes/events-sdk/events-sdk.test.ts +11 -3
- package/src/classes/events-sdk/events-sdk.types.ts +3 -4
- package/src/classes/socket-io/socket-io.class.ts +10 -8
|
@@ -64,12 +64,24 @@ async function login() {
|
|
|
64
64
|
loading.value = true
|
|
65
65
|
|
|
66
66
|
const sdk = new EventsSdkClass({
|
|
67
|
-
loginUrl: "https://
|
|
68
|
-
refreshTokenUrl: "https://
|
|
67
|
+
loginUrl: "https://loginapi.voicenter.co.il/monitorAPI/Login",
|
|
68
|
+
refreshTokenUrl: "https://loginapi.voicenter.co.il/monitorAPI/RefreshIdentityToken",
|
|
69
69
|
loginType: LoginType.TOKEN, // <=== "User" or "Token"
|
|
70
70
|
token: 'QMSVU9dwNYC9Le9VCBqx24AB9TYyWj9Xn5aCPV0GFHIWoShQqfPtnAPmnw24xpJIUSsDDtlac2OPpjx0t3MSkxH3AhiQGHCeGZ8e',
|
|
71
|
-
|
|
72
|
-
|
|
71
|
+
servers: [
|
|
72
|
+
{
|
|
73
|
+
URLID: 1,
|
|
74
|
+
Priority: 1,
|
|
75
|
+
Version: "1.3.7",
|
|
76
|
+
Domain: "monitor5.voicenter.co.il"
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
URLID: 2,
|
|
80
|
+
Priority: 2,
|
|
81
|
+
Version: "1.3.7",
|
|
82
|
+
Domain: "monitor6.voicenter.co.il"
|
|
83
|
+
}
|
|
84
|
+
],
|
|
73
85
|
})
|
|
74
86
|
|
|
75
87
|
await sdk.init()
|
package/package.json
CHANGED
|
@@ -120,17 +120,23 @@ class AuthClass{
|
|
|
120
120
|
(prev.Priority > current.Priority) ? prev : current
|
|
121
121
|
)
|
|
122
122
|
}
|
|
123
|
+
if (!this.eventsSdkClass.options.isNewStack && !this.eventsSdkClass.servers.length && loginSessionData.URLList) {
|
|
124
|
+
this.eventsSdkClass.URLList = loginSessionData.URLList
|
|
125
|
+
}
|
|
123
126
|
if (this.eventsSdkClass.server) {
|
|
124
127
|
this.eventsSdkClass.socketIoClass.getSocketIoFunction(`v=${this.eventsSdkClass.server.Version}`)
|
|
125
128
|
this.eventsSdkClass.loggerClass.init()
|
|
126
129
|
}
|
|
130
|
+
if (!this.eventsSdkClass.server && this.eventsSdkClass.URLList.length) {
|
|
131
|
+
this.eventsSdkClass.socketIoClass.getSocketIoFunction(`v=${loginSessionData.Client}`)
|
|
132
|
+
}
|
|
127
133
|
if (loginSessionData.IdentityCode) {
|
|
128
134
|
this.token = loginSessionData.IdentityCode
|
|
129
|
-
this.eventsSdkClass.connect(ServerParameter.
|
|
135
|
+
this.eventsSdkClass.connect(ServerParameter.MAIN)
|
|
130
136
|
}
|
|
131
137
|
if (loginSessionData.Token) {
|
|
132
138
|
this.token = loginSessionData.Token
|
|
133
|
-
this.eventsSdkClass.connect(ServerParameter.
|
|
139
|
+
this.eventsSdkClass.connect(ServerParameter.MAIN)
|
|
134
140
|
}
|
|
135
141
|
if (loginSessionData.RefreshToken && loginSessionData.IdentityCodeExpiry && this.eventsSdkClass.options.loginType === LoginType.USER) {
|
|
136
142
|
this.eventsSdkClass.options.refreshToken = loginSessionData.RefreshToken
|
|
@@ -26,7 +26,7 @@ export const eventsSdkDefaultOptions: Partial<EventsSdkOptions> = {
|
|
|
26
26
|
reconnection: true,
|
|
27
27
|
reconnectionDelay: 5000,
|
|
28
28
|
reconnectionAttempts: 10,
|
|
29
|
-
perMessageDeflate:
|
|
29
|
+
perMessageDeflate: undefined,
|
|
30
30
|
upgrade: false,
|
|
31
31
|
transports: [ 'websocket' ],
|
|
32
32
|
debug: false
|
|
@@ -5,53 +5,34 @@ import { SocketIoClass } from '@/classes/socket-io/socket-io.class'
|
|
|
5
5
|
import { EventsSdkOptions, ReconnectOptions, Server, ServerParameter } from '@/classes/events-sdk/events-sdk.types'
|
|
6
6
|
import { SocketTyped } from '@/types/socket'
|
|
7
7
|
import {
|
|
8
|
-
GenericEventWrapper,
|
|
9
|
-
EventSpecificCallback,
|
|
10
8
|
EventCallbackListenersMap,
|
|
9
|
+
EventSpecificCallback,
|
|
11
10
|
EventTypeData,
|
|
12
11
|
EventTypeNames,
|
|
12
|
+
GenericEventWrapper,
|
|
13
13
|
} from '@/types/events'
|
|
14
14
|
import { EventsEnum } from '@/enum/events.enum'
|
|
15
15
|
import { LoggerClass } from '@/classes/logger/logger.class'
|
|
16
16
|
|
|
17
17
|
class EventsSdkClass{
|
|
18
|
-
constructor (options: EventsSdkOptions) {
|
|
19
|
-
this.options = {
|
|
20
|
-
...eventsSdkDefaultOptions,
|
|
21
|
-
...options
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
this.server = this.options.fallbackServer
|
|
25
|
-
|
|
26
|
-
this.argumentOptions = {
|
|
27
|
-
...options
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
18
|
private argumentOptions: EventsSdkOptions
|
|
32
|
-
public readonly options: EventsSdkOptions
|
|
33
|
-
...eventsSdkDefaultOptions
|
|
34
|
-
}
|
|
19
|
+
public readonly options: EventsSdkOptions
|
|
35
20
|
public servers: Server[] = []
|
|
21
|
+
public URLList: string[] = []
|
|
36
22
|
public server: Server
|
|
23
|
+
public URL: string | undefined
|
|
37
24
|
public socket: SocketTyped | undefined
|
|
25
|
+
private mainServer: Server | undefined
|
|
26
|
+
|
|
27
|
+
private alreadyAttemptedOtherServers: Array<number | string> = []
|
|
38
28
|
|
|
39
29
|
public authClass = new AuthClass(this)
|
|
40
30
|
public socketIoClass = new SocketIoClass(this)
|
|
41
31
|
public loggerClass = new LoggerClass(this)
|
|
42
32
|
|
|
43
|
-
public reconnectOptions: ReconnectOptions
|
|
44
|
-
retryCount: 1,
|
|
45
|
-
maxReconnectAttempts: this.options.maxReconnectAttempts,
|
|
46
|
-
reconnectionDelay: this.options.reconnectionDelay, // 10 seconds. After each re-connection attempt this number will increase (minReconnectionDelay * attempts) => 10, 20, 30, 40 seconds ... up to 5min
|
|
47
|
-
minReconnectionDelay: this.options.reconnectionDelay, // 10 seconds
|
|
48
|
-
maxReconnectionDelay: 60000 * 5 // 5 minutes
|
|
49
|
-
}
|
|
33
|
+
public reconnectOptions: ReconnectOptions
|
|
50
34
|
|
|
51
|
-
public retryConnection
|
|
52
|
-
leading: true,
|
|
53
|
-
trailing: false
|
|
54
|
-
})
|
|
35
|
+
public retryConnection
|
|
55
36
|
|
|
56
37
|
private listeners: EventCallbackListenersMap = {
|
|
57
38
|
[EventsEnum.ALL_EXTENSION_STATUS]: [],
|
|
@@ -67,6 +48,32 @@ class EventsSdkClass{
|
|
|
67
48
|
}
|
|
68
49
|
private allListeners: Array<(data: GenericEventWrapper) => void> = []
|
|
69
50
|
|
|
51
|
+
constructor (options: EventsSdkOptions) {
|
|
52
|
+
this.options = {
|
|
53
|
+
...eventsSdkDefaultOptions,
|
|
54
|
+
...options
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
this.reconnectOptions = {
|
|
58
|
+
retryCount: 1,
|
|
59
|
+
maxReconnectAttempts: this.options.maxReconnectAttempts,
|
|
60
|
+
reconnectionDelay: this.options.reconnectionDelay, // 10 seconds. After each re-connection attempt this number will increase (minReconnectionDelay * attempts) => 10, 20, 30, 40 seconds ... up to 5min
|
|
61
|
+
minReconnectionDelay: this.options.reconnectionDelay, // 10 seconds
|
|
62
|
+
maxReconnectionDelay: 60000 * 5 // 5 minutes
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
this.server = this.options.fallbackServer
|
|
66
|
+
|
|
67
|
+
this.retryConnection = debounce(this.connect.bind(this), this.reconnectOptions.reconnectionDelay, {
|
|
68
|
+
leading: true,
|
|
69
|
+
trailing: false
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
this.argumentOptions = {
|
|
73
|
+
...options
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
70
77
|
public on<T extends EventTypeNames> (event: T, callback: EventSpecificCallback<T>): void
|
|
71
78
|
public on (event: '*', callback: (data: GenericEventWrapper) => void): void
|
|
72
79
|
public on (event: unknown, callback: unknown) {
|
|
@@ -110,23 +117,25 @@ class EventsSdkClass{
|
|
|
110
117
|
this.allListeners.forEach((callback) => callback(allEventData))
|
|
111
118
|
}
|
|
112
119
|
|
|
113
|
-
public connect (
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
120
|
+
public connect (serverParameter: ServerParameter) {
|
|
121
|
+
// if (server === ServerParameter.DEFAULT) {
|
|
122
|
+
// serverToConnect = this.findCurrentServer()
|
|
123
|
+
// }
|
|
124
|
+
//
|
|
125
|
+
// if (server === ServerParameter.NEXT) {
|
|
126
|
+
// serverToConnect = this.findNextAvailableServer()
|
|
127
|
+
// }
|
|
128
|
+
//
|
|
129
|
+
// if (server === ServerParameter.PREVIOUS) {
|
|
130
|
+
// serverToConnect = this.findMaxPriorityServer()
|
|
131
|
+
// }
|
|
132
|
+
|
|
133
|
+
if (serverParameter === ServerParameter.MAIN) {
|
|
134
|
+
this.findMainServer()
|
|
118
135
|
}
|
|
119
136
|
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
if (server === ServerParameter.PREVIOUS) {
|
|
125
|
-
serverToConnect = this.findMaxPriorityServer()
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (!serverToConnect) {
|
|
129
|
-
this.server = this.findCurrentServer()
|
|
137
|
+
if (serverParameter === ServerParameter.NEXT) {
|
|
138
|
+
this.findNextServer()
|
|
130
139
|
}
|
|
131
140
|
|
|
132
141
|
this.socketIoClass.doReconnect = true
|
|
@@ -147,66 +156,78 @@ class EventsSdkClass{
|
|
|
147
156
|
this.socketIoClass.clearKeepAliveInterval()
|
|
148
157
|
}
|
|
149
158
|
|
|
150
|
-
private
|
|
159
|
+
private findMainServer () {
|
|
151
160
|
if (this.servers.length) {
|
|
152
|
-
this.
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
this.server = this.options.fallbackServer
|
|
156
|
-
}
|
|
157
|
-
return this.server
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
private findNextAvailableServer (): Server {
|
|
161
|
-
// this.log(INFO, 'Failover -> Trying to find another server');
|
|
162
|
-
|
|
163
|
-
const currentServerPriority = this.server.Priority
|
|
164
|
-
|
|
165
|
-
const nextServerPriority = currentServerPriority + 1
|
|
166
|
-
|
|
167
|
-
const nextServer = this.servers.find(server => server.Priority === nextServerPriority) || this.findMinPriorityServer()
|
|
168
|
-
|
|
169
|
-
this.server = { ...nextServer }
|
|
170
|
-
|
|
171
|
-
// this.log(INFO, 'Failover -> Found new server. Connecting to it...', this.server);
|
|
172
|
-
|
|
173
|
-
return this.server
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
private findMaxPriorityServer (): Server {
|
|
177
|
-
// this.log(INFO, 'Fallback -> Trying to find previous server');
|
|
161
|
+
this.mainServer = this.servers.reduce((prev, cur) => {
|
|
162
|
+
return cur.Priority > prev.Priority ? cur : prev
|
|
163
|
+
})
|
|
178
164
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
return this.server
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
if (this.server && maxPriorityServer.Domain !== this.server.Domain) {
|
|
188
|
-
this.server = maxPriorityServer
|
|
189
|
-
|
|
190
|
-
// this.log(INFO, 'Fallback -> Trying to find previous server');
|
|
191
|
-
|
|
192
|
-
return this.server
|
|
165
|
+
this.server = this.mainServer
|
|
166
|
+
} else {
|
|
167
|
+
if (this.URLList.length) {
|
|
168
|
+
this.URL = this.URLList[0]
|
|
169
|
+
}
|
|
193
170
|
}
|
|
194
|
-
|
|
195
|
-
return this.server
|
|
196
171
|
}
|
|
197
172
|
|
|
198
|
-
private
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
173
|
+
private findNextServer () {
|
|
174
|
+
if (this.servers.length) {
|
|
175
|
+
if (this.server.Priority === this.mainServer!.Priority) {
|
|
176
|
+
let filteredServers = this.servers.filter(
|
|
177
|
+
server =>
|
|
178
|
+
server.Priority !== this.mainServer!.Priority &&
|
|
179
|
+
this.alreadyAttemptedOtherServers.indexOf(server.Priority) + 1 === 0
|
|
180
|
+
)
|
|
181
|
+
|
|
182
|
+
if (!filteredServers.length) {
|
|
183
|
+
this.alreadyAttemptedOtherServers = []
|
|
184
|
+
|
|
185
|
+
filteredServers = this.servers.filter(
|
|
186
|
+
server =>
|
|
187
|
+
server.Priority !== this.mainServer!.Priority &&
|
|
188
|
+
this.alreadyAttemptedOtherServers.indexOf(server.Priority) + 1 === 0
|
|
189
|
+
)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
if (filteredServers.length) {
|
|
193
|
+
this.server = filteredServers.reduce((prev, cur) => {
|
|
194
|
+
return cur.Priority > prev.Priority ? cur : prev
|
|
195
|
+
})
|
|
196
|
+
|
|
197
|
+
this.alreadyAttemptedOtherServers.push(this.server.Priority)
|
|
198
|
+
}
|
|
199
|
+
} else {
|
|
200
|
+
this.server = this.mainServer!
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
if (this.URLList.length) {
|
|
204
|
+
if (this.URL === this.URLList[0]) {
|
|
205
|
+
let filteredServers = this.URLList.filter(
|
|
206
|
+
url =>
|
|
207
|
+
url !== this.URLList[0] &&
|
|
208
|
+
this.alreadyAttemptedOtherServers.indexOf(url) + 1 === 0
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
if (!filteredServers.length) {
|
|
212
|
+
this.alreadyAttemptedOtherServers = []
|
|
213
|
+
|
|
214
|
+
filteredServers = this.URLList.filter(
|
|
215
|
+
url =>
|
|
216
|
+
url !== this.URLList[0] &&
|
|
217
|
+
this.alreadyAttemptedOtherServers.indexOf(url) + 1 === 0
|
|
218
|
+
)
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (filteredServers.length) {
|
|
222
|
+
this.URL = filteredServers[0]
|
|
223
|
+
|
|
224
|
+
this.alreadyAttemptedOtherServers.push(this.URL)
|
|
225
|
+
}
|
|
226
|
+
} else {
|
|
227
|
+
this.URL = this.URLList[0]
|
|
228
|
+
}
|
|
229
|
+
}
|
|
207
230
|
}
|
|
208
|
-
|
|
209
|
-
return this.server
|
|
210
231
|
}
|
|
211
232
|
|
|
212
233
|
public getServerWithHighestPriority (servers: Server[]): Server {
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import EventsSdkClass from '@/classes/events-sdk/events-sdk.class'
|
|
2
|
-
import {
|
|
2
|
+
import { LoginType } from '@/enum/auth.enum'
|
|
3
3
|
|
|
4
4
|
describe('EventsSdkClass', () => {
|
|
5
5
|
it('should be true', () => {
|
|
6
|
-
const eventsSdkClass = new EventsSdkClass(
|
|
6
|
+
const eventsSdkClass = new EventsSdkClass({
|
|
7
|
+
loginUrl: 'https://loginapidev.voicenter.co.il/Auth/Login/Voicenter/Monitor',
|
|
8
|
+
refreshTokenUrl: 'https://loginapidev.voicenter.co.il/Auth/RefreshToken',
|
|
9
|
+
loginType: LoginType.TOKEN, // <=== "User" or "Token"
|
|
10
|
+
token: 'QMSVU9dwNYC9Le9VCBqx24AB9TYyWj9Xn5aCPV0GFHIWoShQqfPtnAPmnw24xpJIUSsDDtlac2OPpjx0t3MSkxH3AhiQGHCeGZ8e',
|
|
11
|
+
getSettingsUrl: 'https://loginapidev.voicenter.co.il/Application/GetSettings',
|
|
12
|
+
isNewStack: true,
|
|
13
|
+
})
|
|
14
|
+
|
|
7
15
|
expect(eventsSdkClass.init()).toBe(true)
|
|
8
16
|
})
|
|
9
|
-
})
|
|
17
|
+
})
|
|
@@ -4,7 +4,7 @@ import { LoginType } from '@/enum/auth.enum'
|
|
|
4
4
|
|
|
5
5
|
export interface EventsSdkOptions {
|
|
6
6
|
isNewStack?: boolean,
|
|
7
|
-
servers
|
|
7
|
+
servers?: Server[],
|
|
8
8
|
loginUrl: string,
|
|
9
9
|
getSettingsUrl?: string
|
|
10
10
|
fallbackServer: Server,
|
|
@@ -70,7 +70,6 @@ export interface ReconnectOptions {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
export enum ServerParameter {
|
|
73
|
-
|
|
74
|
-
NEXT = 'next'
|
|
75
|
-
PREVIOUS = 'previous'
|
|
73
|
+
MAIN = 'main',
|
|
74
|
+
NEXT = 'next'
|
|
76
75
|
}
|
|
@@ -39,9 +39,9 @@ export class SocketIoClass{
|
|
|
39
39
|
const server = this.eventsSdkClass.server
|
|
40
40
|
|
|
41
41
|
try {
|
|
42
|
-
const domain = server.Domain
|
|
42
|
+
const domain = server ? server.Domain : this.eventsSdkClass.URL
|
|
43
43
|
|
|
44
|
-
const url = `${protocol}://${domain}`
|
|
44
|
+
const url = server ? `${protocol}://${domain}` : this.eventsSdkClass.URL
|
|
45
45
|
|
|
46
46
|
// this.eventsSdkClass.loggerClass.log(
|
|
47
47
|
// LoggerTypeEnum.INFO,
|
|
@@ -74,8 +74,10 @@ export class SocketIoClass{
|
|
|
74
74
|
}
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
if (this.ioFunction) {
|
|
77
|
+
if (this.ioFunction && url) {
|
|
78
78
|
this.io = this.ioFunction(url, options)
|
|
79
|
+
} else {
|
|
80
|
+
throw new Error('Socket server url no defined')
|
|
79
81
|
}
|
|
80
82
|
} catch (e) {
|
|
81
83
|
// this.log(ERROR, e);
|
|
@@ -97,7 +99,9 @@ export class SocketIoClass{
|
|
|
97
99
|
.on(EventsEnum.DISCONNECT, () => this.onDisconnect())
|
|
98
100
|
.on(EventsEnum.CONNECT_ERROR_EVENT, () => {
|
|
99
101
|
this.eventsSdkClass.emit(EventsEnum.ONLINE_STATUS_EVENT, { isSocketConnected: false })
|
|
100
|
-
|
|
102
|
+
setTimeout(() => {
|
|
103
|
+
this.eventsSdkClass.connect(ServerParameter.NEXT)
|
|
104
|
+
},this.eventsSdkClass.options.reconnectionDelay)
|
|
101
105
|
})
|
|
102
106
|
}
|
|
103
107
|
}
|
|
@@ -182,9 +186,7 @@ export class SocketIoClass{
|
|
|
182
186
|
return
|
|
183
187
|
}
|
|
184
188
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
this.eventsSdkClass.connect(ServerParameter.NEXT, true)
|
|
188
|
-
}, 15000)
|
|
189
|
+
console.log('attempt to connect...')
|
|
190
|
+
this.eventsSdkClass.connect(ServerParameter.NEXT)
|
|
189
191
|
}
|
|
190
192
|
}
|