Skip to content

Commit edbb5a9

Browse files
authored
Add WebSocket usage examples to cupertino_http (#1266)
1 parent 760564f commit edbb5a9

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

pkgs/cupertino_http/README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ void main() async {
4646
}
4747
```
4848

49+
[CupertinoWebSocket][] provides a [package:web_socket][] [WebSocket][]
50+
implementation.
51+
52+
```dart
53+
import 'package:cupertino_http/cupertino_http.dart';
54+
import 'package:web_socket/web_socket.dart';
55+
56+
void main() async {
57+
final socket = await CupertinoWebSocket.connect(
58+
Uri.parse('wss://ws.postman-echo.com/raw'));
59+
60+
socket.events.listen((e) async {
61+
switch (e) {
62+
case TextDataReceived(text: final text):
63+
print('Received Text: $text');
64+
await socket.close();
65+
case BinaryDataReceived(data: final data):
66+
print('Received Binary: $data');
67+
case CloseReceived(code: final code, reason: final reason):
68+
print('Connection to server closed: $code [$reason]');
69+
}
70+
});
71+
}
72+
```
73+
4974
You can also use the [Foundation URL Loading System] API directly.
5075

5176
```dart
@@ -63,6 +88,9 @@ final task = session.dataTaskWithCompletionHandler(URLRequest.fromUrl(url),
6388
task.resume();
6489
```
6590

66-
[package:http Client]: https://pub.dev/documentation/http/latest/http/Client-class.html
67-
[Foundation URL Loading System]: https://developer.apple.com/documentation/foundation/url_loading_system
91+
[CupertinoWebSocket]: https://pub.dev/documentation/cupertino_http/latest/cupertino_http/CupertinoWebSocket-class.html
6892
[dart:io HttpClient]: https://api.dart.dev/stable/dart-io/HttpClient-class.html
93+
[Foundation URL Loading System]: https://developer.apple.com/documentation/foundation/url_loading_system
94+
[package:http Client]: https://pub.dev/documentation/http/latest/http/Client-class.html
95+
[package:web_socket]: https://pub.dev/packages/web_socket
96+
[WebSocket]: https://pub.dev/documentation/web_socket/latest/web_socket/WebSocket-class.html

pkgs/cupertino_http/lib/src/cupertino_web_socket.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ class ConnectionException extends WebSocketException {
2525
///
2626
/// NOTE: the [WebSocket] interface is currently experimental and may change in
2727
/// the future.
28+
///
29+
/// ```dart
30+
/// import 'package:cupertino_http/cupertino_http.dart';
31+
/// import 'package:web_socket/web_socket.dart';
32+
///
33+
/// void main() async {
34+
/// final socket = await CupertinoWebSocket.connect(
35+
/// Uri.parse('wss://ws.postman-echo.com/raw'));
36+
///
37+
/// socket.events.listen((e) async {
38+
/// switch (e) {
39+
/// case TextDataReceived(text: final text):
40+
/// print('Received Text: $text');
41+
/// await socket.close();
42+
/// case BinaryDataReceived(data: final data):
43+
/// print('Received Binary: $data');
44+
/// case CloseReceived(code: final code, reason: final reason):
45+
/// print('Connection to server closed: $code [$reason]');
46+
/// }
47+
/// });
48+
/// }
49+
/// ```
2850
class CupertinoWebSocket implements WebSocket {
2951
/// Create a new WebSocket connection using the
3052
/// [NSURLSessionWebSocketTask API](https://developer.apple.com/documentation/foundation/nsurlsessionwebsockettask).

0 commit comments

Comments
 (0)