From 2aed8bc9dacff68da293b124615391c01f2c272a Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Fri, 12 Jul 2024 17:18:25 -0700 Subject: [PATCH 1/3] Add WebSocket usage to cupertino_http README --- pkgs/cupertino_http/README.md | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/pkgs/cupertino_http/README.md b/pkgs/cupertino_http/README.md index ef5c9fff8c..dcf7cda91c 100644 --- a/pkgs/cupertino_http/README.md +++ b/pkgs/cupertino_http/README.md @@ -46,6 +46,32 @@ void main() async { } ``` +[CupertinoWebSocket][] provides a [package:web_socket][] [WebSocket][] +implementation. + + +```dart +import 'package:cupertino_http/cupertino_http.dart'; +import 'package:web_socket/web_socket.dart'; + +void main() async { + final socket = await CupertinoWebSocket.connect( + Uri.parse('wss://ws.postman-echo.com/raw')); + + socket.events.listen((e) async { + switch (e) { + case TextDataReceived(text: final text): + print('Received Text: $text'); + await socket.close(); + case BinaryDataReceived(data: final data): + print('Received Binary: $data'); + case CloseReceived(code: final code, reason: final reason): + print('Connection to server closed: $code [$reason]'); + } + }); +} +``` + You can also use the [Foundation URL Loading System] API directly. ```dart @@ -63,6 +89,9 @@ final task = session.dataTaskWithCompletionHandler(URLRequest.fromUrl(url), task.resume(); ``` -[package:http Client]: https://pub.dev/documentation/http/latest/http/Client-class.html -[Foundation URL Loading System]: https://developer.apple.com/documentation/foundation/url_loading_system +[CupertinoWebSocket]: https://pub.dev/documentation/cupertino_http/latest/cupertino_http/CupertinoWebSocket-class.html [dart:io HttpClient]: https://api.dart.dev/stable/dart-io/HttpClient-class.html +[Foundation URL Loading System]: https://developer.apple.com/documentation/foundation/url_loading_system +[package:http Client]: https://pub.dev/documentation/http/latest/http/Client-class.html +[package:web_socket]: https://pub.dev/packages/web_socket +[WebSocket]: https://pub.dev/documentation/web_socket/latest/web_socket/WebSocket-class.html From 6f1b4ed28320929afd6b40c0890ac27ba2a0de34 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Fri, 12 Jul 2024 17:22:57 -0700 Subject: [PATCH 2/3] Add example --- .../lib/src/cupertino_web_socket.dart | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkgs/cupertino_http/lib/src/cupertino_web_socket.dart b/pkgs/cupertino_http/lib/src/cupertino_web_socket.dart index 666c018835..b374dd9907 100644 --- a/pkgs/cupertino_http/lib/src/cupertino_web_socket.dart +++ b/pkgs/cupertino_http/lib/src/cupertino_web_socket.dart @@ -25,6 +25,28 @@ class ConnectionException extends WebSocketException { /// /// NOTE: the [WebSocket] interface is currently experimental and may change in /// the future. +/// +/// ```dart +/// import 'package:cupertino_http/cupertino_http.dart'; +/// import 'package:web_socket/web_socket.dart'; +/// +/// void main() async { +/// final socket = await CupertinoWebSocket.connect( +/// Uri.parse('wss://ws.postman-echo.com/raw')); +/// +/// socket.events.listen((e) async { +/// switch (e) { +/// case TextDataReceived(text: final text): +/// print('Received Text: $text'); +/// await socket.close(); +/// case BinaryDataReceived(data: final data): +/// print('Received Binary: $data'); +/// case CloseReceived(code: final code, reason: final reason): +/// print('Connection to server closed: $code [$reason]'); +/// } +/// }); +/// } +/// ``` class CupertinoWebSocket implements WebSocket { /// Create a new WebSocket connection using the /// [NSURLSessionWebSocketTask API](https://developer.apple.com/documentation/foundation/nsurlsessionwebsockettask). From b7bf9fe49f1a1713b46d33d1a3413aae2bdcecf2 Mon Sep 17 00:00:00 2001 From: Brian Quinlan Date: Fri, 12 Jul 2024 17:24:56 -0700 Subject: [PATCH 3/3] Remove blank line --- pkgs/cupertino_http/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/pkgs/cupertino_http/README.md b/pkgs/cupertino_http/README.md index dcf7cda91c..0e12a6ef08 100644 --- a/pkgs/cupertino_http/README.md +++ b/pkgs/cupertino_http/README.md @@ -49,7 +49,6 @@ void main() async { [CupertinoWebSocket][] provides a [package:web_socket][] [WebSocket][] implementation. - ```dart import 'package:cupertino_http/cupertino_http.dart'; import 'package:web_socket/web_socket.dart';