web_socket 0.1.4 copy "web_socket: ^0.1.4" to clipboard
web_socket: ^0.1.4 copied to clipboard

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

example/web_socket_example.dart

import 'dart:convert';
import 'dart:io';

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 WebSocket.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]
  }));
}
15
likes
0
pub points
95%
popularity

Publisher

verified publisherlabs.dart.dev

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

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

web

More

Packages that depend on web_socket