close method
Tells the stream sink that no further streams will be added.
This allows the stream sink to complete any remaining work and release resources that are no longer needed
Returns a future which is completed when the stream sink has shut down.
If cleaning up can fail, the error may be reported in the returned future,
otherwise it completes with null.
Returns the same future as done.
The stream sink may close before the close method is called, either due
to an error or because it is itself providing events to someone who has
stopped listening. In that case, the done future is completed first,
and the close method will return the done future when called.
Unifies StreamConsumer.close and EventSink.close which both mark their object as not expecting any further events.
Implementation
@override
Future close([int? code, String? reason]) {
if (_isReservedStatusCode(code)) {
throw WebSocketChannelException('Reserved status code $code');
}
if (_outCloseCode == null) {
_outCloseCode = code;
_outCloseReason = reason;
}
if (!_controller.isClosed) {
// If a close has not yet been received from the other end then
// 1) make sure to listen on the stream so the close frame will be
// processed if received.
// 2) set a timer terminate the connection if a close frame is
// not received.
if (!_controller.hasListener && _subscription != null) {
_controller.stream.drain().catchError((_) => {});
}
// When closing the web-socket, we no longer accept data.
_closeTimer ??= Timer(const Duration(seconds: 5), () {
// Reuse code and reason from the local close.
_closeCode = _outCloseCode;
_closeReason = _outCloseReason;
if (_subscription != null) _subscription!.cancel();
_controller.close();
_webSockets.remove(_serviceId);
});
}
return _sink.close();
}