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

Fix(websocket): Do not crash app when a reserved close code is passed (Android) #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

canmertc
Copy link

@canmertc canmertc commented Mar 19, 2025

PR Checklist

What is the current behavior?

Android

The following line throws an error when the code argument passed to the close method is a reserved number. The plugin doesn't handle this error currently. And, the app crashes with an error similar to Code 1005 is reserved and may not be used.

https://github.com/square/okhttp/blob/master/okhttp/src/commonJvmAndroid/kotlin/okhttp3/internal/ws/WebSocketProtocol.kt#L146

iOS

The app doesn't crash. onClose listener gets called with code 1001.

What is the new behavior?

Android

This update makes the plugin handle this error that otherwise crashes the app.

Added Try-Catch Block:
  • Wrapped the close method's logic in a try-catch block to handle any errors that may occur during the execution of the method.
Reserved Close Code Check
  • Implemented a check to identify if the provided code falls within reserved ranges (1004-1006 or 1015-2999).
  • Logs a warning if a reserved code is detected, ensuring developers are aware of its use.
Fallback to Standard Close Code:
  • If a reserved code is used, the method falls back to the standard close code 1000, indicating a normal closure.
  • This ensures the connection is closed gracefully, so the app can start retrying the connection.

iOS

Nothing changed.

the following line throws an error when the "code" argument passed to the close method is a reserved number. This update makes the plugin handle this error that otherwise crashes the app.
@canmertc canmertc changed the title Fix(websocket): Do not crash app when a reserved close code is passed Fix(websocket): Do not crash app when a reserved close code is passed (Android) Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant