-
Notifications
You must be signed in to change notification settings - Fork 388
pkgs/ok_http: JNIgen fixes and added WebSocket support #1257
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 all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
dbf7d1d
ok_http: upgrade jni and regenerate bindings
Anikate-De bd97d77
ok_http: jnigen fixes
Anikate-De 4cb89e8
generate bindings for okhttp websocket classes
Anikate-De daf5d3a
add websocket dependency
Anikate-De 87f7595
create proxy and interceptor; generate bindings
Anikate-De dc6984b
first websocket impl
Anikate-De 78b6637
create an example section for websocket demo
Anikate-De 7b93fb0
add integration tests
Anikate-De bb68b2b
add helper comments and docs to kotlin files
Anikate-De 5cea12b
add more docs
Anikate-De 3c14e5d
Squashed commit of the following:
Anikate-De 91c9378
add more and more docs
Anikate-De 9968a76
update changelog
Anikate-De 84cd97b
Merge branch 'master' into okhttp_websockets
Anikate-De add63c5
Revert "ok_http: jnigen fixes"
Anikate-De 8a22001
Merge branch 'okhttp_websockets' of https://github.com/Anikate-De/htt…
Anikate-De 280b727
jnigen fixes
Anikate-De 1f8de0c
fix duplicate sections in the readme
Anikate-De 6e64458
minor text fixes
Anikate-De 0bd1a73
fix JArray extensions
Anikate-De 4efbfad
remove websocket example
Anikate-De b2711bd
fix ok_http_web_socket.dart nits
Anikate-De d0d1961
rename 'WSInterceptor' to 'WebSocketInterceptor'
Anikate-De 7bc6d26
refer to deadlock issue in package: jni
Anikate-De 8871562
added usage notes to readme and docs
Anikate-De 57fe25f
documentation improvements
Anikate-De 05400b5
bump up version from `0.1.0-wip` to `0.1.0`
Anikate-De 1d0ef4c
increase step `test` timeout to 1200s to prevent workflow failures
Anikate-De 260c2a2
upgrade jni versions and regenerate bindings
Anikate-De 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| ## 0.1.0-wip | ||
| ## 0.1.0 | ||
|
|
||
| - Implementation of [`BaseClient`](https://pub.dev/documentation/http/latest/http/BaseClient-class.html) and `send()` method using [`enqueue()` API](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-call/enqueue.html) | ||
| - `ok_http` can now send asynchronous requests and stream response bodies. | ||
| - Add [DevTools Network View](https://docs.flutter.dev/tools/devtools/network) support. | ||
| - WebSockets support is now available in the `ok_http` package. Wraps around the OkHttp [WebSocket API](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-web-socket/index.html). |
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 was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
pkgs/ok_http/android/src/main/kotlin/com/example/ok_http/WebSocketInterceptor.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 |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| package com.example.ok_http | ||
|
|
||
| import okhttp3.Interceptor | ||
| import okhttp3.OkHttpClient | ||
|
|
||
| /** | ||
| * Usage of `chain.proceed(...)` via JNI Bindings leads to threading issues. This is a workaround | ||
| * to intercept the response before it is parsed by the WebSocketReader, to prevent response parsing errors. | ||
| * | ||
| * https://github.com/dart-lang/native/issues/1337 | ||
| */ | ||
| class WebSocketInterceptor { | ||
| companion object { | ||
| fun addWSInterceptor( | ||
| clientBuilder: OkHttpClient.Builder | ||
| ): OkHttpClient.Builder { | ||
| return clientBuilder.addInterceptor(Interceptor { chain -> | ||
| val request = chain.request() | ||
| val response = chain.proceed(request) | ||
|
|
||
| response.newBuilder() | ||
| // Removing this header to ensure that OkHttp does not fail due to unexpected values. | ||
| .removeHeader("sec-websocket-extensions") | ||
| // Adding the header to ensure successful parsing of the response. | ||
| .addHeader("sec-websocket-extensions", "permessage-deflate").build() | ||
| }) | ||
| } | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
pkgs/ok_http/android/src/main/kotlin/com/example/ok_http/WebSocketListenerProxy.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 |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| package com.example.ok_http | ||
|
|
||
| import okhttp3.Response | ||
| import okhttp3.WebSocket | ||
| import okhttp3.WebSocketListener | ||
| import okio.ByteString | ||
|
|
||
| /** | ||
| * `OkHttp` expects a subclass of the abstract class [`WebSocketListener`](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-web-socket-listener/index.html) | ||
| * to be passed to the `newWebSocket` method. | ||
| * | ||
| * `package:jnigen` does not support the ability to subclass abstract Java classes in Dart | ||
| * (see https://github.com/dart-lang/jnigen/issues/348). | ||
| * | ||
| * This file provides an interface `WebSocketListener`, which can | ||
| * be implemented in Dart and a wrapper class `WebSocketListenerProxy`, which | ||
| * can be passed to the OkHttp API. | ||
| */ | ||
| class WebSocketListenerProxy(private val listener: WebSocketListener) : WebSocketListener() { | ||
| interface WebSocketListener { | ||
| fun onOpen(webSocket: WebSocket, response: Response) | ||
| fun onMessage(webSocket: WebSocket, text: String) | ||
| fun onMessage(webSocket: WebSocket, bytes: ByteString) | ||
| fun onClosing(webSocket: WebSocket, code: Int, reason: String) | ||
| fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) | ||
| } | ||
|
|
||
| override fun onOpen(webSocket: WebSocket, response: Response) { | ||
| listener.onOpen(webSocket, response) | ||
| } | ||
|
|
||
| override fun onMessage(webSocket: WebSocket, text: String) { | ||
| listener.onMessage(webSocket, text) | ||
| } | ||
|
|
||
| override fun onMessage(webSocket: WebSocket, bytes: ByteString) { | ||
| listener.onMessage(webSocket, bytes) | ||
| } | ||
|
|
||
| override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { | ||
| listener.onClosing(webSocket, code, reason) | ||
| } | ||
|
|
||
| override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { | ||
| listener.onFailure(webSocket, t, response) | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
pkgs/ok_http/example/integration_test/web_socket_test.dart
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 |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| import 'package:ok_http/ok_http.dart'; | ||
| import 'package:web_socket_conformance_tests/web_socket_conformance_tests.dart'; | ||
|
|
||
| void main() { | ||
| testAll(OkHttpWebSocket.connect); | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.