pauldemarco / socketcluster_client

SocketCluster.io Client for Dart
BSD 3-Clause "New" or "Revised" License
12 stars 10 forks source link

[flutter web] violently crashes app on when the server isn't available #15

Open lukepighetti opened 3 years ago

lukepighetti commented 3 years ago

Description

If you try to connect to a websocket server that isn't available on flutter web the app violently crashes even if you try to catch the error.

flutter --version

Flutter 2.1.0-12.1.pre • channel dev • https://github.com/flutter/flutter.git
Framework • revision 8264cb3e8a (12 days ago) • 2021-03-10 12:37:57 -0800
Engine • revision 711ab3fda0
Tools • Dart 2.13.0 (build 2.13.0-116.0.dev)

Reproduction

Expected Results

Actual Results

Error State

Screen Shot 2021-03-22 at 11 40 10 AM

MCVE

import 'package:flutter/material.dart';
import 'package:socketcluster_client/socketcluster_client.dart';

void main() async {
  try {
    await Socket.connect('ws://localhost:8000/foo', listener: MyListener());
  } catch (e) {
    print('caught $e');
  } finally {
    runApp(MyApp());
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Websocket Issue',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Center(
          child: Text('Websocket Issue'),
        ),
      ),
    );
  }
}

class MyListener extends BasicListener {
  @override
  void onAuthentication(Socket socket, bool status) {
    print('onAuthentication');
  }

  @override
  void onConnectError(Socket socket, e) {
    print('onConnectError');
  }

  @override
  void onConnected(Socket socket) {
    print('onConnected');
  }

  @override
  void onDisconnected(Socket socket) {
    print('onDisconnected');
  }

  @override
  void onSetAuthToken(String token, Socket socket) {
    print('onSetAuthToken');
  }
}
name: flutter_websocket_issue
description: A new Flutter project.
publish_to: "none"
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  socketcluster_client: ^0.3.0

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter:
  uses-material-design: true
lukepighetti commented 3 years ago

This was cross posted from https://github.com/MichaelMarner/dart-redux-remote-devtools/issues/35, which has been closed because the issue is upstream in socketcluster_client

lukepighetti commented 3 years ago

I tried wrapping the web WebSocket creation with this, making sure to await the builder method, but I still can't get it to error cleanly. If I put this same exact code in my Flutter main() method, it will error cleanly. No idea what's going on inside this package to cause this difference.

final completer = Completer();
final ws = WebSocket(url);

ws.onError.listen((e) {
  print('onError $e');
  completer.complete();
});

ws.onOpen.listen((e) {
  print('onOpen $e');
  completer.complete();
});

await completer.future;

return ws;
MichaelMarner commented 3 years ago

I don't use Flutter on web so don't have much experience here. But do you get a stacktrace where the error is occuring (ie what's in the exception in the devtools if you expand it)?

lukepighetti commented 3 years ago

I get nothing useful out of any of the normal flutter tooling, unfortunately.

This package is upstream of redux_remote_dev_tools and the result is that on web if we try to connect to dev tools and the server is not up, the app hard crashes and won't launch. This problem doesn't exist on any other platform, where the failure to connect is easily caught by a try/catch block.