Skip to content

Commit 199f9fa

Browse files
authored
Add a dart:io WebSocket implementation (#1139)
1 parent 37fceb8 commit 199f9fa

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1180
-77
lines changed

.github/workflows/dart.yml

+61-67
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkgs/web_socket/README.md

+46-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
1-
TODO: Put a short description of the package here that helps potential users
2-
know whether this package might be useful for them.
1+
[![pub package](https://img.shields.io/pub/v/web_socket.svg)](https://pub.dev/packages/web_socket)
2+
[![package publisher](https://img.shields.io/pub/publisher/web_socket.svg)](https://pub.dev/packages/web_socket/publisher)
3+
4+
Any easy-to-use library for communicating with WebSockets that has multiple
5+
implementations.
6+
7+
## Using
8+
9+
```dart
10+
import 'package:web_socket/io_web_socket.dart';
11+
import 'package:web_socket/web_socket.dart';
12+
13+
void main() async {
14+
final socket =
15+
await IOWebSocket.connect(Uri.parse('wss://ws.postman-echo.com/raw'));
16+
17+
socket.events.listen((e) async {
18+
switch (e) {
19+
case TextDataReceived(text: final text):
20+
print('Received Text: $text');
21+
await socket.close();
22+
case BinaryDataReceived(data: final data):
23+
print('Received Binary: $data');
24+
case CloseReceived(code: final code, reason: final reason):
25+
print('Connection to server closed: $code [$reason]');
26+
}
27+
});
28+
29+
socket.sendText('Hello Dart WebSockets! 🎉');
30+
}
31+
```
32+
33+
## Status: experimental
34+
35+
**NOTE**: This package is currently experimental and published under the
36+
[labs.dart.dev](https://dart.dev/dart-team-packages) pub publisher in order to
37+
solicit feedback.
38+
39+
For packages in the labs.dart.dev publisher we generally plan to either graduate
40+
the package into a supported publisher (dart.dev, tools.dart.dev) after a period
41+
of feedback and iteration, or discontinue the package. These packages have a
42+
much higher expected rate of API and breaking changes.
43+
44+
Your feedback is valuable and will help us evolve this package. For general
45+
feedback, suggestions, and comments, please file an issue in the
46+
[bug tracker](https://github.com/dart-lang/http/issues).
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1-
void main() {
2-
// TODO: add an example.
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:web_socket/io_web_socket.dart';
5+
import 'package:web_socket/web_socket.dart';
6+
7+
const requestId = 305;
8+
9+
/// Prints the US dollar value of Bitcoins continuously.
10+
void main() async {
11+
// Whitebit public WebSocket API documentation:
12+
// https://docs.whitebit.com/public/websocket/
13+
final socket =
14+
await IOWebSocket.connect(Uri.parse('wss://api.whitebit.com/ws'));
15+
16+
socket.events.listen((e) {
17+
switch (e) {
18+
case TextDataReceived(text: final text):
19+
final json = jsonDecode(text) as Map;
20+
if (json['id'] == requestId) {
21+
if (json['error'] != null) {
22+
stderr.writeln('Failure: ${json['error']}');
23+
socket.close();
24+
}
25+
} else {
26+
final params = (json['params'] as List).cast<List<dynamic>>();
27+
print('₿1 = USD\$${params[0][2]}');
28+
}
29+
case BinaryDataReceived():
30+
stderr.writeln('Unexpected binary response from server');
31+
socket.close();
32+
case CloseReceived():
33+
stderr.writeln('Connection to server closed');
34+
}
35+
});
36+
socket.sendText(jsonEncode({
37+
'id': requestId,
38+
'method': 'candles_subscribe',
39+
'params': ['BTC_USD', 5]
40+
}));
341
}
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export 'src/io_web_socket.dart' show IOWebSocket;

0 commit comments

Comments
 (0)