ws 1.0.0-pre.1 copy "ws: ^1.0.0-pre.1" to clipboard
ws: ^1.0.0-pre.1 copied to clipboard

WS: A compact, highly efficient WebSocket library. Easily establish, manage, and reconnect WebSocket connections in real-time apps.

example/ws_example.dart

// ignore_for_file: avoid_print
import 'dart:async';
import 'dart:io' as io show exit;

import 'package:ws/ws.dart';

void main([List<String>? args]) {
  // The server URL (pass it as --define=URL=...)
  const url = String.fromEnvironment('URL',
      defaultValue: 'wss://echo.plugfox.dev:443/connect');

  // Setup a WebSocket client with auto reconnect
  final client = WebSocketClient()
    // Observing the incoming messages from the server
    ..stream.listen((message) => print('< $message'))
    // Observing the state changes (connecting, open, disconnecting, closed)
    ..stateChanges.listen((state) => print('* $state'))
    // Connect to the server url
    ..connect(url)
    // Send a message
    ..add('Hello, ') // > Hello,
    // One more message after first is sent
    ..add('world!'); // > world!

  Timer(const Duration(seconds: 1), () async {
    await client.close(); // Close the connection
    print('Metrics:\n${client.metrics}'); // Print the metrics
    io.exit(0); // Exit the process
  });
}
83
likes
0
points
1.73k
downloads

Publisher

verified publisherplugfox.dev

Weekly Downloads

WS: A compact, highly efficient WebSocket library. Easily establish, manage, and reconnect WebSocket connections in real-time apps.

Repository (GitHub)
View/report issues

Topics

#ws #websocket #cross-platform #reconnect #socket

Funding

Consider supporting this project:

www.buymeacoffee.com
www.patreon.com
boosty.to

License

unknown (license)

Dependencies

meta

More

Packages that depend on ws