dart-lang / web_socket_channel

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

🐛 RangeError (code): Invalid value: Not in inclusive range 3000..4999: 1000 after updating to 3.0.0 from 2.4.0 #355

Closed tempo-riz closed 5 months ago

tempo-riz commented 6 months ago

Hello after upgrading the package from 2.4.0 to 3.0.0 calling close seems to throw (was tested and full working before upgrading) am I missing something ?

For a bit of context the code sends audio data to a server, which returns the transcription First I start the connection :

  Future<void> start() async {
    _wsChannel = WebSocketChannel.connect(
      buildUrl(_baseLiveUrl, null, queryParams),
      protocols: ['token', apiKey],
    );
    await _wsChannel.ready;

    _wsChannel.stream.listen((event) {
      if (_outputTranscriptStream.isClosed) {
        close();
      } else {
        _outputTranscriptStream.add(...)
      }
    }, onDone: () {
      close();
    }, onError: (error) {
      _outputTranscriptStream.addError(...)
    });

    inputAudioStream.listen((data) {
      if (_wsChannel.closeCode != null) {
        close();
      } else {
        _wsChannel.sink.add(data);
      }
    }, onDone: () {
      close();
    });
  }

The error occurs in the close method :

  Future<void> close() async {
    await _wsChannel.sink.close(status.normalClosure); <------ this throws the following error
    await _outputTranscriptStream.close();
  }

The error :

  RangeError (code): Invalid value: Not in inclusive range 3000..4999: 1000
  dart:core                                                          RangeError.checkValueInInterval
  package:web_socket/src/utils.dart 10:16                            checkCloseCode
  package:web_socket/src/io_web_socket.dart 108:5                    IOWebSocket.close
  package:web_socket_channel/adapter_web_socket_channel.dart 112:27  new AdapterWebSocketChannel.<fn>.<fn>
  package:stream_channel                                             _GuaranteeSink.close
  package:async/src/delegate/stream_sink.dart 47:27                  DelegatingStreamSink.close
  package:web_socket_channel/adapter_web_socket_channel.dart 147:18  _WebSocketSink.close
  package:deepgram_speech_to_text/src/deepgram.dart 68:27            DeepgramLiveTranscriber.close
  package:deepgram_speech_to_text/src/deepgram.dart 62:7             DeepgramLiveTranscriber.start.<fn>
ekasetiawans commented 6 months ago

+1

ekasetiawans commented 6 months ago

This issue makes our backend leaking connection 😩

polvallverdu commented 5 months ago

Same issue here when closing the socket. Any updates?

polvallverdu commented 5 months ago

I investigated a bit, and internally they use code 3001 when the client is told to close the socket, and 3005 when the client closes the connection at their end.

This is not the standard, and should be fixed. Also, the error comes from https://pub.dev/packages/web_socket, so I think this issue should be closed.

bhumkong commented 5 months ago

Yeah, the issue has been closed in web_socket 0.1.5 – 1000 close code is now allowed.

We should update the docs, because channel.sink.close(status.goingAway) will give this error.

catapop84 commented 5 months ago

I have the some problem. web_socket_channel needs to depend on web_socket 0.1.5 if you want to close with 1000.