-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a dart:io WebSocket implementation (#1139)
- Loading branch information
1 parent
37fceb8
commit 199f9fa
Showing
41 changed files
with
1,180 additions
and
77 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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). |
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,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] | ||
})); | ||
} |
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 @@ | ||
export 'src/io_web_socket.dart' show IOWebSocket; |
Oops, something went wrong.