Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Feb 6, 2024
1 parent 9de26bb commit 7b30f61
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
14 changes: 13 additions & 1 deletion pkgs/cupertino_http/lib/src/websocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class CupertinoWebSocket implements XXXWebSocket {
void _closed(int? closeCode, Data? reason) {
print('closing with $closeCode');
if (!_events.isClosed) {
final closeReason = reason == null ? null : utf8.decode(reason.bytes);
final closeReason = reason == null ? '' : utf8.decode(reason.bytes);

_events
..add(CloseReceived(closeCode, closeReason))
Expand Down Expand Up @@ -126,6 +126,18 @@ class CupertinoWebSocket implements XXXWebSocket {

@override
Future<void> close([int? code, String? reason]) async {
if (_events.isClosed) {
throw XXXWebSocketConnectionClosed();
}

if (code != null) {
RangeError.checkValueInInterval(code, 3000, 4999, 'code');
}
if (reason != null && utf8.encode(reason).length > 123) {
throw ArgumentError.value(reason, 'reason',
'reason must be <= 123 bytes long when encoded as UTF-8');
}

if (!_events.isClosed) {
unawaited(_events.close());

Expand Down
8 changes: 4 additions & 4 deletions pkgs/websocket/lib/iowebsocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ class IOWebSocket implements XXXWebSocket {
onDone: () {
print('onDone');
if (!_events.isClosed) {
_events
.add(CloseReceived(_webSocket.closeCode, _webSocket.closeReason));
_events.add(CloseReceived(
_webSocket.closeCode, _webSocket.closeReason ?? ""));
_events.close();
}
},
Expand Down Expand Up @@ -80,15 +80,15 @@ class IOWebSocket implements XXXWebSocket {
// endpoint that has already sent a Close frame will continue to process
// data.
@override
Future<void> close([int? code, String reason = '']) async {
Future<void> close([int? code, String? reason]) async {
if (_events.isClosed) {
throw XXXWebSocketConnectionClosed();
}

if (code != null) {
RangeError.checkValueInInterval(code, 3000, 4999, 'code');
}
if (utf8.encode(reason).length > 123) {
if (reason != null && utf8.encode(reason).length > 123) {
throw ArgumentError.value(reason, "reason",
"reason must be <= 123 bytes long when encoded as UTF-8");
}
Expand Down
4 changes: 2 additions & 2 deletions pkgs/websocket/lib/websocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ abstract interface class XXXWebSocket {
/// Sends a Close frame to the peer. 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 receive an empty reason string.
/// is set then the peer will not receive a reason string.
///
/// Throws a [RangeError] if [code] is not in the range 3000-4999.
///
Expand All @@ -117,7 +117,7 @@ abstract interface class XXXWebSocket {
///
/// Throws [XXXWebSocketConnectionClosed] if the connection is already closed
/// (including by the peer).
Future<void> close([int? code, String reason = '']);
Future<void> close([int? code, String? reason]);

/// A [Stream] of [WebSocketEvent] received from the peer.
///
Expand Down

0 comments on commit 7b30f61

Please sign in to comment.