-
Notifications
You must be signed in to change notification settings - Fork 4
feat: implement HybridWebSocket on Android #6
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -126,6 +126,9 @@ dependencies { | |
|
||
// Add a dependency on NitroModules | ||
implementation project(":react-native-nitro-modules") | ||
|
||
// Let's use whatever React Native uses right now to avoid duplicates | ||
implementation "com.squareup.okhttp3:okhttp:+" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this is correct, have to check 🤣 |
||
} | ||
|
||
if (isNewArchitectureEnabled()) { | ||
|
80 changes: 65 additions & 15 deletions
80
...react-native-fast-ws/android/src/main/java/com/margelo/nitro/websocket/HybridWebSocket.kt
This file contains hidden or 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 |
---|---|---|
@@ -1,49 +1,99 @@ | ||
package com.margelo.nitro.websocket | ||
|
||
import com.margelo.nitro.core.ArrayBuffer | ||
import okhttp3.* | ||
import okio.ByteString | ||
import okio.ByteString.Companion.toByteString | ||
import java.util.concurrent.TimeUnit | ||
|
||
class HybridWebSocket(url: String, protocols: Array<String>) : HybridWebSocketSpec() { | ||
override fun send(message: String) { | ||
TODO("Not implemented") | ||
} | ||
private var onOpenCallback: ((String) -> Unit)? = null | ||
private var onCloseCallback: ((Double, String) -> Unit)? = null | ||
private var onErrorCallback: ((String) -> Unit)? = null | ||
private var onMessageCallback: ((String) -> Unit)? = null | ||
private var onArrayBufferCallback: ((ArrayBuffer) -> Unit)? = null | ||
|
||
override fun sendArrayBuffer(buffer: ArrayBuffer) { | ||
TODO("Not implemented") | ||
private val client = OkHttpClient.Builder() | ||
.readTimeout(0, TimeUnit.MILLISECONDS) // Disable timeouts | ||
.build() | ||
|
||
private var webSocket: WebSocket? = null | ||
|
||
private val listener = object : WebSocketListener() { | ||
override fun onOpen(webSocket: WebSocket, response: Response) { | ||
val protocol = response.header("Sec-WebSocket-Protocol") ?: "" | ||
onOpenCallback?.invoke(protocol) | ||
} | ||
|
||
override fun onMessage(webSocket: WebSocket, text: String) { | ||
onMessageCallback?.invoke(text) | ||
} | ||
|
||
override fun onMessage(webSocket: WebSocket, bytes: ByteString) { | ||
val buffer = ArrayBuffer.allocate(bytes.size) | ||
buffer.getBuffer(false).put(bytes.toByteArray()) | ||
onArrayBufferCallback?.invoke(buffer) | ||
} | ||
|
||
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { | ||
onCloseCallback?.invoke(code.toDouble(), reason) | ||
webSocket.close(code, reason) | ||
grabbou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { | ||
onErrorCallback?.invoke(t.message ?: "WebSocket error") | ||
} | ||
} | ||
|
||
private val request = Request.Builder() | ||
.url(url) | ||
.apply { | ||
if (protocols.isNotEmpty()) { | ||
header("Sec-WebSocket-Protocol", protocols.joinToString(", ")) | ||
} | ||
} | ||
.build() | ||
|
||
override fun connect() { | ||
TODO("Not implemented") | ||
webSocket = client.newWebSocket(request, listener) | ||
} | ||
|
||
override fun close() { | ||
TODO("Not implemented") | ||
webSocket?.close(1000, null) | ||
} | ||
|
||
override fun send(message: String) { | ||
webSocket?.send(message) | ||
} | ||
|
||
override fun sendArrayBuffer(buffer: ArrayBuffer) { | ||
webSocket?.send(buffer.getBuffer(false).toByteString()) | ||
} | ||
|
||
override fun ping() { | ||
TODO("Not implemented") | ||
webSocket?.send(ByteString.EMPTY) | ||
} | ||
|
||
override fun onOpen(callback: (selectedProtocol: String) -> Unit) { | ||
TODO("Not implemented") | ||
onOpenCallback = callback | ||
} | ||
|
||
override fun onClose(callback: (code: Double, reason: String) -> Unit) { | ||
TODO("Not implemented") | ||
onCloseCallback = callback | ||
} | ||
|
||
override fun onError(callback: (error: String) -> Unit) { | ||
TODO("Not implemented") | ||
onErrorCallback = callback | ||
} | ||
|
||
override fun onMessage(callback: (message: String) -> Unit) { | ||
TODO("Not implemented") | ||
onMessageCallback = callback | ||
} | ||
|
||
override fun onArrayBuffer(callback: (buffer: ArrayBuffer) -> Unit) { | ||
TODO("Not implemented") | ||
onArrayBufferCallback = callback | ||
} | ||
|
||
override val memorySize: Long | ||
get() = 0L | ||
|
||
get() = 0L // Implement proper memory calculation if needed | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably move it to some config file