Skip to content

JSON RPC2: How to handle automatic reconnection on the client side #1973

Open
@wanclem

Description

@wanclem

I have this:

import 'dart:async';
import 'package:json_rpc_2/json_rpc_2.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class RPClient {
  static Client? _client;
  static WebSocketChannel? _socket;
  static const String _url = "ws://localhost:8080";

  static Future<void> _initialize() async {
    _socket = WebSocketChannel.connect(Uri.parse(_url));
    _client = Client(_socket!.cast<String>());
    unawaited(_client!.listen());
  }

  static Future<dynamic> call(
    String functionName,
    Map<String, dynamic> params,
  ) async {
    if (_client == null) {
      await _initialize();
    }
    try {
      return await _client!.sendRequest(functionName, params);
    } on RpcException {
      rethrow;
    } catch (e) {
      rethrow;
    }
  }
}

If I deliberately restart my server I get back Client closed on the client side.

Are there any techniques to enable automatic reconnection?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions