-
Notifications
You must be signed in to change notification settings - Fork 358
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkgs/ok_http: JNIgen fixes and added WebSocket support (#1257)
* ok_http: upgrade jni and regenerate bindings * ok_http: jnigen fixes * generate bindings for okhttp websocket classes * add websocket dependency * create proxy and interceptor; generate bindings * first websocket impl * create an example section for websocket demo * add integration tests * add helper comments and docs to kotlin files * remove websocket example * rename 'WSInterceptor' to 'WebSocketInterceptor' * refer to deadlock issue in package: jni * bump up version from `0.1.0-wip` to `0.1.0` * increase step `test` timeout to 1200s to prevent workflow failures
- Loading branch information
1 parent
757438e
commit a0781c5
Showing
14 changed files
with
4,702 additions
and
1,083 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
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 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 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 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 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 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
Oops, something went wrong.