0

Simple example on WebSocket connection and data rountrip

Free lines Dart 1 revisions 57 3 months ago 3 months ago
import 'dart:io';
import 'dart:async';

void main() async {
  // Using constructor is deprecated. (and perhaps removed by now)
  WebSocket ws = await WebSocket.connect('wss://echo.websocket.org');
  
  // Listen to websocket events
  ws.listen((e) {
    print('Received: \'$e\'');
  });

  // ws.add sends bytes to the other end.
  ws.add('hi world');
}

This is a very minimalistic example of using WebSockets in Dart. Note that this uses dart:io and not dart:html, since this code isn't intended to run in a browser or anything.