Skip to content
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

Add a dart:io WebSocket implementation #1139

Merged
merged 12 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 61 additions & 67 deletions .github/workflows/dart.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 46 additions & 2 deletions pkgs/web_socket/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,46 @@
TODO: Put a short description of the package here that helps potential users
know whether this package might be useful for them.
[![pub package](https://img.shields.io/pub/v/web_socket.svg)](https://pub.dev/packages/web_socket)
[![package publisher](https://img.shields.io/pub/publisher/web_socket.svg)](https://pub.dev/packages/web_socket/publisher)

Any easy-to-use library for communicating with WebSockets that has multiple
implementations.

## Using

```dart
import 'package:web_socket/io_web_socket.dart';
import 'package:web_socket/web_socket.dart';

void main() async {
final socket =
await IOWebSocket.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]');
}
});

socket.sendText('Hello Dart WebSockets! 🎉');
}
```

## Status: experimental

**NOTE**: This package is currently experimental and published under the
[labs.dart.dev](https://dart.dev/dart-team-packages) pub publisher in order to
solicit feedback.

For packages in the labs.dart.dev publisher we generally plan to either graduate
the package into a supported publisher (dart.dev, tools.dart.dev) after a period
of feedback and iteration, or discontinue the package. These packages have a
much higher expected rate of API and breaking changes.

Your feedback is valuable and will help us evolve this package. For general
feedback, suggestions, and comments, please file an issue in the
[bug tracker](https://github.com/dart-lang/http/issues).
42 changes: 40 additions & 2 deletions pkgs/web_socket/example/web_socket_example.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,41 @@
void main() {
// TODO: add an example.
import 'dart:convert';
import 'dart:io';

import 'package:web_socket/io_web_socket.dart';
import 'package:web_socket/web_socket.dart';

const requestId = 305;

/// Prints the US dollar value of Bitcoins continuously.
void main() async {
// Whitebit public WebSocket API documentation:
// https://docs.whitebit.com/public/websocket/
final socket =
await IOWebSocket.connect(Uri.parse('wss://api.whitebit.com/ws'));

socket.events.listen((e) {
switch (e) {
case TextDataReceived(text: final text):
final json = jsonDecode(text) as Map;
if (json['id'] == requestId) {
if (json['error'] != null) {
stderr.writeln('Failure: ${json['error']}');
socket.close();
}
} else {
final params = (json['params'] as List).cast<List<dynamic>>();
print('₿1 = USD\$${params[0][2]}');
}
case BinaryDataReceived():
stderr.writeln('Unexpected binary response from server');
socket.close();
case CloseReceived():
stderr.writeln('Connection to server closed');
}
});
socket.sendText(jsonEncode({
'id': requestId,
'method': 'candles_subscribe',
'params': ['BTC_USD', 5]
}));
}
1 change: 1 addition & 0 deletions pkgs/web_socket/lib/io_web_socket.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'src/io_web_socket.dart' show IOWebSocket;
Loading