Add non-reliable websockets support (#1497)

* Add non-reliable websockets support

* Remove unused properties

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Scott Bishel <scott.bishel@mattermost.com>
This commit is contained in:
Miguel de la Cruz 2021-10-21 15:44:27 +02:00 committed by GitHub
parent 17b2d07daf
commit afc37ca3af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -34,10 +34,10 @@ export const ACTION_UPDATE_CLIENT_CONFIG = 'UPDATE_CLIENT_CONFIG'
export interface MMWebSocketClient {
conn: WebSocket | null;
sendMessage(action: string, data: any, responseCallback?: () => void): void /* eslint-disable-line @typescript-eslint/no-explicit-any */
setFirstConnectCallback(callback: () => void): void
setReconnectCallback(callback: () => void): void
setErrorCallback(callback: (event: Event) => void): void
setCloseCallback(callback: (connectFailCount: number) => void): void
connectionId: string
}
type OnChangeHandler = (client: WSClient, blocks: Block[]) => void
@ -57,8 +57,6 @@ class WSClient {
onChange: OnChangeHandler[] = []
onError: OnErrorHandler[] = []
onConfigChange: OnConfigChangeHandler[] = []
private mmWSMaxRetries = 100
private mmWSRetryDelay = 300
private notificationDelay = 100
private reopenDelay = 3000
private updatedBlocks: Block[] = []
@ -163,10 +161,19 @@ class WSClient {
open(): void {
if (this.client !== null) {
// configure the Mattermost websocket client callbacks
const onConnect = () => {
Utils.log('WSClient in plugin mode, reusing Mattermost WS connection')
for (const handler of this.onStateChange) {
handler(this, 'open')
}
this.state = 'open'
}
const onReconnect = () => {
Utils.logWarn('WSClient reconnected')
this.open()
onConnect()
for (const handler of this.onReconnect) {
handler(this)
}
@ -204,34 +211,11 @@ class WSClient {
}
}
this.client.setFirstConnectCallback(onConnect)
this.client.setErrorCallback(onError)
this.client.setCloseCallback(onClose)
this.client.setReconnectCallback(onReconnect)
// WSClient needs to ensure that the Mattermost client has
// correctly stablished the connection before opening
let retries = 0
const setPluginOpen = () => {
if (this.client?.connectionId !== '') {
Utils.log('WSClient in plugin mode, reusing Mattermost WS connection')
for (const handler of this.onStateChange) {
handler(this, 'open')
}
this.state = 'open'
return
}
retries++
if (retries <= this.mmWSMaxRetries) {
Utils.log('WSClient Mattermost Websocket not ready, retrying')
setTimeout(setPluginOpen, this.mmWSRetryDelay)
} else {
Utils.logError('WSClient error on open: Mattermost Websocket client is not ready')
}
}
setPluginOpen()
return
}