websocket_universal 0.0.2-dev copy "websocket_universal: ^0.0.2-dev" to clipboard
websocket_universal: ^0.0.2-dev copied to clipboard

outdated

Convenient websocket handler for all platforms (both IO and web/HTML). Websocket messages routing, statuses and other features have easy-to-use interface.

example/main.dart

import 'package:websocket_universal/websocket_universal.dart';

void main() async {
  // ignore: unused_local_variable
  const websocketLocalExample = 'ws://127.0.0.1:42627/websocket';
  const websocketConnectionUri = 'wss://ws.postman-echo.com/raw';

  /// Example using ISocketMessage and IMessageToServer:
  final IMessageProcessor<ISocketMessage<dynamic>, IMessageToServer>
      messageProcessor = SocketMessageProcessor();
  late final socketHandler = IWebSocketHandler.createClient(
    websocketConnectionUri, // Postman echo ws server
    messageProcessor,
    pingIntervalMs: 3000, // send Ping message every 3000 ms
    timeoutConnectionMs: 4000, // connection fail timeout after 4000 ms
  );

  socketHandler.logEventStream.listen((debugEvent) {
    // ignore: avoid_print
    print('debugEvent: ${debugEvent.socketLogEventType}'
        ' ping=${debugEvent.pingMs}. Debug message=${debugEvent.message}');
  });
  socketHandler.socketStateStream.listen((stateEvent) {
    // ignore: avoid_print
    print('webSocket status changed to ${stateEvent.status}');
  });
  socketHandler.incomingMessagesStream.listen((inMsg) {
    // ignore: avoid_print
    print('webSocket got message: $inMsg');
  });

  // Connecting to server:
  final isConnected = await socketHandler.connect();

  if (!isConnected) {
    // ignore: avoid_print
    print('Connection to [$websocketConnectionUri] failed for some reason!');
    return;
  }

  final outMsg = MessageToServerImpl.onlyHost(host: 'test', data: 'mydata');
  socketHandler.sendMessage(outMsg);

  await Future<void>.delayed(const Duration(seconds: 8));
  // Disconnecting from server:
  await socketHandler.disconnect('manual disconnect');
  // Disposing webSocket:
  socketHandler.close();
}
34
likes
0
pub points
86%
popularity

Publisher

verified publisherdvmatyun.ru

Convenient websocket handler for all platforms (both IO and web/HTML). Websocket messages routing, statuses and other features have easy-to-use interface.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

json_annotation, meta

More

Packages that depend on websocket_universal