dart-lang / web_socket_channel

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

Auto invoke onDown method to close websocket. #331

Open HydroCarbon opened 4 months ago

HydroCarbon commented 4 months ago

When i send a stream to wss server, The onDown method be invoked auto Someone help me? There is my demo code:

record stream and send code:

Stream<Uint8List> recordStream = await _recorder.startStream(config);

    recordStream.listen(
      (data) {
        List<int> binaryData =
            _recorder.convertBytesToInt16(Uint8List.fromList(data));
        _channel.sink.addStream(Stream.fromIterable(binaryData));
      },
      onDone: () {
        print('Record done.');
      },
      onError: (error) {
        print('Record occur error: $error');
      },

wss channel connect code:

await _channel.ready;

    _channel.stream.listen(
      (message) {
        print(message);
      },
      onDone: () {
        print('Ws is done.');

        print(_channel.closeReason);
        if (_channel.closeCode != status.goingAway) {
          print('Ws is closed.');
        }
      },
      onError: (error) {
        print('Ws occur error: $error');
      },
    );

I had try to use add() method, issue is still there.

HydroCarbon commented 4 months ago

At first, I build a channel to wss: _channel = WebSocketChannel.connect(wsUrl); Then send a text frame to it , it work perfect and i got listen callback: _channel.sink.add(payload);

but i send a stream next, websocket closed by no reason. There is my logs:

flutter: {"header":{"appkey":"xxxxxxx","message_id":"7aefd166623b4b9f80d452a580a8f2f7","task_id":"b49441934424490795240605d7799505","namespace":"SpeechTranscriber","name":"StartTranscription"},"payload":{"format":"pcm","sample_rate":16000,"enable_punctuation_prediction":true}}

flutter: {"header":{"namespace":"SpeechTranscriber","name":"TranscriptionStarted","status":20000000,"message_id":"7d4ceda2dfbe4789b07f648637c803e8","task_id":"b49441934424490795240605d7799505","status_text":"Gateway:SUCCESS:Success."}}

flutter: Ws is done.

flutter: 

flutter: Ws is closed.