@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.
@@ -64,12 +64,24 @@ async function login() {
64
64
  loading.value = true
65
65
 
66
66
  const sdk = new EventsSdkClass({
67
- loginUrl: "https://loginapidev.voicenter.co.il/Auth/Login/Voicenter/Monitor",
68
- refreshTokenUrl: "https://loginapidev.voicenter.co.il/Auth/RefreshToken",
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
- getSettingsUrl: "https://loginapidev.voicenter.co.il/Application/GetSettings",
72
- isNewStack: true,
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@voicenter-team/events-sdk",
3
- "version": "0.0.19",
3
+ "version": "0.0.20",
4
4
  "description": "",
5
5
  "main": "dist/voicenter-events-sdk.cjs.js",
6
6
  "jsdelivr": "dist/voicenter-events-sdk.umd.js",
@@ -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.DEFAULT, true)
135
+ this.eventsSdkClass.connect(ServerParameter.MAIN)
130
136
  }
131
137
  if (loginSessionData.Token) {
132
138
  this.token = loginSessionData.Token
133
- this.eventsSdkClass.connect(ServerParameter.DEFAULT, true)
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: false,
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 = debounce(this.connect.bind(this), this.reconnectOptions.reconnectionDelay, {
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 (server: ServerParameter = ServerParameter.DEFAULT) {
114
- let serverToConnect: Server | undefined
115
-
116
- if (server === ServerParameter.DEFAULT) {
117
- serverToConnect = this.findCurrentServer()
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 (server === ServerParameter.NEXT) {
121
- serverToConnect = this.findNextAvailableServer()
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 findCurrentServer (): Server {
159
+ private findMainServer () {
151
160
  if (this.servers.length) {
152
- this.server = this.servers[0]
153
- }
154
- if (!this.server) {
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
- const maxPriorityServer = this.getServerWithHighestPriority(this.servers)
180
-
181
- if (!this.server) {
182
- this.server = maxPriorityServer
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 findMinPriorityServer (): Server {
199
- const minPriority = Math.min(...this.servers.map(server => server.Priority))
200
-
201
- const server = this.servers.find(server => server.Priority === minPriority)
202
-
203
- if (server) {
204
- this.server = server
205
-
206
- return this.server
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 { eventsSdkDefaultOptions } from '@/classes/events-sdk/events-sdk-default-options'
2
+ import { LoginType } from '@/enum/auth.enum'
3
3
 
4
4
  describe('EventsSdkClass', () => {
5
5
  it('should be true', () => {
6
- const eventsSdkClass = new EventsSdkClass(eventsSdkDefaultOptions)
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: Server[],
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
- DEFAULT = 'default',
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
- this.eventsSdkClass.connect(ServerParameter.NEXT)
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
- this.keepReconnectInterval = setInterval(() => {
186
- console.log('attempt to connect...')
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
  }