web_socket_client 0.1.2 copy "web_socket_client: ^0.1.2" to clipboard
web_socket_client: ^0.1.2 copied to clipboard

A simple WebSocket client for Dart which includes automatic reconnection logic.

example/main.dart

// ignore_for_file: avoid_print
import 'package:web_socket_client/web_socket_client.dart';

void main() async {
  // Create a WebSocket client.
  final uri = Uri.parse('ws://localhost:8080');
  const backoff = ConstantBackoff(Duration(seconds: 1));
  final socket = WebSocket(uri, backoff: backoff);

  // Listen for changes in the connection state.
  socket.connection.listen((state) => print('state: "$state"'));

  // Listen for incoming messages.
  socket.messages.listen((message) {
    print('message: "$message"');

    // Send a message to the server.
    socket.send('ping');
  });

  await Future<void>.delayed(const Duration(seconds: 3));

  // Close the connection.
  socket.close();
}
107
likes
140
pub points
95%
popularity

Publisher

verified publisherfelangel.dev

A simple WebSocket client for Dart which includes automatic reconnection logic.

Repository (GitHub)
View/report issues

Topics

#web-socket #web-socket-client

Documentation

API reference

License

MIT (LICENSE)

Dependencies

web, web_socket_channel

More

Packages that depend on web_socket_client