dart-lang / web_socket_channel

StreamChannel wrappers for WebSockets.
https://pub.dev/packages/web_socket_channel
BSD 3-Clause "New" or "Revised" License
423 stars 112 forks source link

WSL Laravel Sail: Websocket not upgraded #227

Closed TylerByte666 closed 1 year ago

TylerByte666 commented 2 years ago

Hi all.

I would really love to know how to get this working locally with laravel sail?

I have tried both laravel-websockets and soketi locally through sail but I keep getting this output:

WebSocketException: Connection to 'http://10.0.2.2:6001#' was not upgraded to websocket

I have this as my home widget, where the error originates:

class _MyHomePageState extends State<MyHomePage> {
  final _channel = IOWebSocketChannel.connect(
    Uri.parse('ws://10.0.2.2:6001'),
  );

  @override
  initState() {
    _channel.sink.add(json.encode({
      "event": "pusher:subscribe",
      "data": {"channel": "channel"}
    }));

    _channel.stream.listen((_data) {
      print(_data.toString());
    }, onError: (error) {
      print("Socket: error => " + error.toString());
    }, onDone: () {
      print("Socket: done");
    });

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: const <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _channel.sink.close(status.goingAway);
    super.dispose();
  }
}

I use WSL, Docker-Desktop and Windows 10. The channel I want to connect to is public and is called: channel, the event is called: App\Events\TestEvent.php

I have added http ability in the android/app/main/AndroidManifest.xml:

<application
       ...
        android:usesCleartextTraffic="true" >

The web sockets are working with Vue.js, Echo.js and Pusher.js so I really do not know what to do. Please help 🤕

novas1r1 commented 2 years ago

@TylerByte666 have you found any solution for your problem since I'm facing the exact same one.

snarcraft commented 1 year ago

@TylerByte666 @novas1r1 try to connect to ws://10.0.2.2:6001/app/${YOUR_PUSHER_KEY} instead :)

novas1r1 commented 1 year ago

Thanks @snarcraft . For me the issue was actually a wrong port I got from my backend dev :D... So the error message lead me here, but was unrelated to this issue.

TylerByte666 commented 1 year ago

@snarcraft Thanks matey, didn't work at all. @novas1r1 Glad you fixed your issue. I tried this plugin this morning and all my problems are fine :) laravel_flutter_pusher 0.0.4