Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-reliable websockets support #1497

Merged
merged 5 commits into from
Oct 21, 2021
Merged
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
40 changes: 12 additions & 28 deletions webapp/src/wsclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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[] = []
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

Expand Down