tryClose method
Try to close the WebSocket connection and the events Stream.
Returns false if already closed (including by peer), otherwise sends
a Close frame to the peer and returns true. If the optional code and
reason arguments are given, they will be included in the Close frame. If no
code is set then the peer will see a 1005 status code. If no reason
is set then the peer will not receive a reason string.
Throws an ArgumentError if code is not 1000, 1001 or in the range
3000-4999. A server sends 1001 when it is going away.
Throws an ArgumentError if reason is longer than 123 bytes when
encoded as UTF-8
Implementation
@override
Future<bool> tryClose([int? code, String? reason]) async {
if (isClosed) return false;
checkCloseCode(code);
checkCloseReason(reason);
closeEvents();
sendClose(code, reason ?? '');
return true;
}