web_socket 1.0.0
web_socket: ^1.0.0 copied to clipboard
Any easy-to-use library for communicating with WebSockets that has multiple implementations.
An easy-to-use library for communicating with WebSockets that has multiple implementations.
Why another WebSocket package? #
The goal of package:web_socket
is to provide a simple, well-defined
WebSockets interface that has
consistent behavior across implementations.
package:web_socket_channel
is the most popular WebSocket package but it is complex and does not have
consistent behavior across implementations.
WebSocket
currently has four implementations that all pass the same set of
conformance tests:
BrowserWebSocket
CupertinoWebSocket
IOWebSocket
OkHttpWebSocket
(currently experimental)
Using #
import 'package:web_socket/web_socket.dart';
void main() async {
final socket =
await WebSocket.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! 🎉');
}