websocket_reconnect 0.0.1 copy "websocket_reconnect: ^0.0.1" to clipboard
websocket_reconnect: ^0.0.1 copied to clipboard

The "websocket_reconnect" is a convenient package for Flutter developers to automatically reconnect to a WebSocket when connection is lost. It eliminates the need for manual reconnection logic. Simply [...]

example/lib/main.dart

import 'dart:async';

import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/status.dart' as status;
import 'package:web_socket_channel/web_socket_channel.dart';

import 'package:websocket_reconnect/websocket_reconnect.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter WebSocket Demo',
      home: CounterPage(),
    );
  }
}

class CounterPage extends StatefulWidget {
  @override
  _CounterPageState createState() => _CounterPageState();
}

class _CounterPageState extends State<CounterPage> {
  int _count = 0;
  WebSocketChannel? _channel;
  bool _isConnected = false;

  @override
  void initState() {
    super.initState();
    _connectToWebSocket();
  }

  @override
  void dispose() {
    _disconnectFromWebSocket();
    super.dispose();
  }

  void _connectToWebSocket() async {
    final url = 'ws://echo.websocket.events';
    final delay = Duration(seconds: 3);

    final channel = IOWebSocketChannel.connect(url);

    channel.stream.listen((message) {
      setState(() {
        _count = int.parse(message);
      });
    }, onError: (error) {
      print('WebSocket error: $error');
      _isConnected = false;
      _showConnectionLostDialog();
      WebsocketReconnect(url: url, delay: delay).scheduleReconnect();
    }, onDone: () {
      print('WebSocket done');
      _isConnected = false;
      _showConnectionLostDialog();
      WebsocketReconnect(url: url, delay: delay).scheduleReconnect();
    }, cancelOnError: true);

    _channel = channel;
    _isConnected = true;

    Connectivity().onConnectivityChanged.listen((result) {
      if ((result == ConnectivityResult.mobile ||
              result == ConnectivityResult.wifi) &&
          !_isConnected) {
        _connectToWebSocket();
      }
    });
  }

  void _disconnectFromWebSocket() {
    _channel?.sink.close(status.goingAway);
  }

  void _showConnectionLostDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Connection Lost'),
          content: Text('Attempting to reconnect...'),
        );
      },
    );
  }

  void _incrementCounter() {
    _channel?.sink.add((_count + 1).toString());
  }

  void _decrementCounter() {
    _channel?.sink.add((_count - 1).toString());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Flutter WebSocket Demo'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            SizedBox(height: 16),
            Text(
              '$_count',
              style: Theme.of(context).textTheme.headline1,
            ),
          ],
        ),
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        children: [
          FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
          SizedBox(height: 16),
          FloatingActionButton(
            onPressed: _decrementCounter,
            tooltip: 'Decrement',
            child: Icon(Icons.remove),
          ),
        ],
      ),
    );
  }
}
1
likes
130
points
1
downloads

Publisher

unverified uploader

Weekly Downloads

The "websocket_reconnect" is a convenient package for Flutter developers to automatically reconnect to a WebSocket when connection is lost. It eliminates the need for manual reconnection logic. Simply define your WebSocket connection and pass it to the "AutoReconnectWebSocket" constructor. The package monitors the connection and automatically reconnects it if lost. You can customize the retry interval and set a maximum number of attempts.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

connectivity_plus, flutter, plugin_platform_interface, web_socket_channel

More

Packages that depend on websocket_reconnect

Packages that implement websocket_reconnect