Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions keeperapi/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion keeperapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@keeper-security/keeperapi",
"description": "Keeper API Javascript SDK",
"version": "17.0.1",
"version": "17.0.2",
"browser": "dist/index.es.js",
"main": "dist/index.cjs.js",
"types": "dist/node/index.d.ts",
Expand Down
18 changes: 12 additions & 6 deletions keeperapi/src/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class SocketListener {

private isConnected: boolean
private reconnectTimeout?: ReturnType<typeof setTimeout>
private currentBackoffSeconds: number
private currentBackoffSeconds: number = 0
private isClosedByClient: boolean

constructor(url: string, messageSessionUid?: Uint8Array, getConnectionRequest?: (messageSessionUid:Uint8Array) => Promise<string>) {
Expand All @@ -70,7 +70,7 @@ export class SocketListener {
this.messageListeners = []
this.singleMessageListeners = []
this.onOpenListeners = []
this.currentBackoffSeconds = SocketListener.getBaseReconnectionInterval()
this.currentBackoffSeconds = this.getBaseReconnectionInterval()
this.isClosedByClient = false
this.isConnected = false
if (getConnectionRequest) this.getConnectionRequest = getConnectionRequest
Expand All @@ -93,7 +93,7 @@ export class SocketListener {
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout)
}
this.currentBackoffSeconds = SocketListener.getBaseReconnectionInterval()
this.currentBackoffSeconds = this.getBaseReconnectionInterval()
this.handleOnOpen()
})

Expand All @@ -109,6 +109,7 @@ export class SocketListener {
reason = JSON.parse(event['reason'])
} catch {
console.log('Connection closed - no close reason.')
this.handleClose({code: 0, reason: {close_reason: 'No close reason provided'}})
this.reconnect()
return
}
Expand Down Expand Up @@ -170,7 +171,7 @@ export class SocketListener {

onClose(callback: () => void): void {
if (!this.socket) throw new Error('Socket not available')
this.socket.onClose(callback)
this.closeListeners.push(callback)
}

onError(callback: () => void): void {
Expand Down Expand Up @@ -203,6 +204,7 @@ export class SocketListener {
for (let { resolve } of this.singleCloseListeners) {
resolve(messageData)
}
this.closeListeners.length = 0
this.singleCloseListeners.length = 0
}

Expand All @@ -221,8 +223,12 @@ export class SocketListener {
})
}

private static getBaseReconnectionInterval(): number {
return Math.random() * 5
private getBaseReconnectionInterval(): number {
if (this.currentBackoffSeconds === 0) {
return Math.random() * 5
} else {
return this.currentBackoffSeconds
}
}

private reconnect() {
Expand Down