-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: websocket not receiving messages after some time (#791)
* fix: websocket not receiving messages after some time * chore: reduce ping interval to 20s Co-authored-by: Mohamad Jaara <mohamad.jaara@wire.com>
- Loading branch information
1 parent
83a9c42
commit dda279e
Showing
5 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
network/src/commonJvmAndroid/kotlin/com/wire/kalium/network/KaliumWebSocketFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.wire.kalium.network | ||
|
||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.Response | ||
import okhttp3.WebSocket | ||
import okhttp3.WebSocketListener | ||
import okio.ByteString | ||
|
||
/** | ||
* Upon request, creates normal [WebSocket], but wraps the listener | ||
* to add logging. | ||
* @see WrapperListener | ||
*/ | ||
class KaliumWebSocketFactory(private val okHttpClient: OkHttpClient) : WebSocket.Factory { | ||
|
||
override fun newWebSocket(request: Request, listener: WebSocketListener): WebSocket { | ||
val wrapperListener = WrapperListener(listener) | ||
return okHttpClient.newWebSocket(request, wrapperListener) | ||
} | ||
|
||
/** | ||
* Wraps the provided [wrappedListener], keeping the normal behaviour, | ||
* but using [kaliumLogger] to log all operations. | ||
*/ | ||
inner class WrapperListener(private val wrappedListener: WebSocketListener) : WebSocketListener() { | ||
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { | ||
super.onClosed(webSocket, code, reason) | ||
kaliumLogger.v("WEBSOCKET: onClosed($code, $reason)") | ||
wrappedListener.onClosed(webSocket, code, reason) | ||
} | ||
|
||
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { | ||
super.onClosing(webSocket, code, reason) | ||
kaliumLogger.v("WEBSOCKET: onClosing($code, $reason)") | ||
wrappedListener.onClosing(webSocket, code, reason) | ||
} | ||
|
||
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { | ||
super.onFailure(webSocket, t, response) | ||
kaliumLogger.v("WEBSOCKET: onFailure($t, $response)") | ||
wrappedListener.onFailure(webSocket, t, response) | ||
} | ||
|
||
override fun onMessage(webSocket: WebSocket, text: String) { | ||
super.onMessage(webSocket, text) | ||
kaliumLogger.v("WEBSOCKET: onMessage($text)") | ||
wrappedListener.onMessage(webSocket, text) | ||
} | ||
|
||
override fun onMessage(webSocket: WebSocket, bytes: ByteString) { | ||
super.onMessage(webSocket, bytes) | ||
kaliumLogger.v("WEBSOCKET: onMessage($bytes)") | ||
wrappedListener.onMessage(webSocket, bytes) | ||
} | ||
|
||
override fun onOpen(webSocket: WebSocket, response: Response) { | ||
super.onOpen(webSocket, response) | ||
kaliumLogger.v("WEBSOCKET: onOpen($response)") | ||
wrappedListener.onOpen(webSocket, response) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters