Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

flutter websocket auto reconnect

  wserror(err) async {
    print(new DateTime.now().toString() + " Connection error: $err");
    await reconnect();
  }

 reconnect() async {
    if (_channel != null) {
      // add in a reconnect delay
      await Future.delayed(Duration(seconds: 4));
    }
    setState(() {
      print(new DateTime.now().toString() + " Starting connection attempt...");
      _channel = IOWebSocketChannel.connect(wsUrl);
      print(new DateTime.now().toString() + " Connection attempt completed.");
    });
    _channel.stream.listen((data) => processMessage(data), onDone: reconnect, onError: wserror, cancelOnError: true);
  }
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #flutter #websocket #auto #reconnect
ADD COMMENT
Topic
Name
6+9 =