Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cyfamobs opened this issue Jan 21, 2025 · 0 comments
Open

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

cyfamobs opened this issue Jan 21, 2025 · 0 comments

Comments

@cyfamobs
Copy link

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?

@cyfamobs cyfamobs changed the title How to handle reconnection on the client side JSON RPC2: How to handle reconnection on the client side Jan 21, 2025
@cyfamobs cyfamobs changed the title JSON RPC2: How to handle reconnection on the client side JSON RPC2: How to handle automatic reconnection on the client side Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant