Skip to content

Commit

Permalink
More docs
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Feb 6, 2024
1 parent 824ffcf commit 8b7b932
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 31 deletions.
6 changes: 2 additions & 4 deletions pkgs/websocket/example/websocket_example.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'package:websocket/iowebsocket.dart';
import 'package:websocket/websocket.dart';

void main() {
var awesome = Awesome();
print('awesome: ${awesome.isAwesome}');
}
void main() {}
57 changes: 30 additions & 27 deletions pkgs/websocket/lib/websocket.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'dart:async';
import 'dart:typed_data';

/// An event received by the peer through the [WebSocket].
sealed class WebSocketEvent {}

/// Text data received by the peer.
/// Text data received by the peer through the [WebSocket].
///
/// See [XXXWebSocket.events].
final class TextDataReceived extends WebSocketEvent {
Expand All @@ -18,7 +19,7 @@ final class TextDataReceived extends WebSocketEvent {
int get hashCode => text.hashCode;
}

/// Binary data received by the peer.
/// Binary data received by the peer through the [WebSocket].
///
/// See [XXXWebSocket.events].
final class BinaryDataReceived extends WebSocketEvent {
Expand All @@ -43,7 +44,8 @@ final class BinaryDataReceived extends WebSocketEvent {
String toString() => 'BinaryDataReceived($data)';
}

/// A close notification sent from the peer or a failure indication.
/// A close notification (Close frame) sent from the peer through the
/// [WebSocket] or a failure indication.
///
/// See [XXXWebSocket.events].
final class CloseReceived extends WebSocketEvent {
Expand All @@ -69,50 +71,51 @@ class XXXWebSocketException implements Exception {
XXXWebSocketException([this.message = ""]);
}

/// Thrown if [XXXWebSocket.sendText] or [XXXWebSocket.sendBytes] is called
/// when the [XXXWebSocket] is closed.
/// Thrown if [XXXWebSocket.sendText], [XXXWebSocket.sendBytes], or
/// [XXXWebSocket.closed] is called when the [XXXWebSocket] is closed.
class XXXWebSocketConnectionClosed extends XXXWebSocketException {
XXXWebSocketConnectionClosed([super.message = 'Connection Closed']);
}

/// What's a good name for this? `SimpleWebSocket`? 'LCDWebSocket`?
/// The interface for WebSocket connections.
///
/// TODO: insert a usage example.
///
/// TODO: thank of a better name, ideally not "WebSocket". Maybe
/// "SimpleWebSocket"?
abstract interface class XXXWebSocket {
/// Say something about not guaranteeing delivery.
/// Sends text data to the connected peer.
///
/// Throws [XXXWebSocketConnectionClosed] if the [XXXWebSocket] is closed
/// (either through [close] or by the peer). Alternatively, we could just throw
/// the data away - that's what JavaScript does. Probably that is better
/// so every call to [sendText], [sendBytes] and [close] doesn't need to be
/// surrounded in a try block.
/// (either through [close] or by the peer).
void sendText(String s);

/// Say something about not guaranteeing delivery.
/// Sends binary data to the connected peer.
///
/// Throws [XXXWebSocketConnectionClosed] if the [XXXWebSocket] is closed
/// (either through [close] or by the peer). Alternatively, we could just throw
/// the data away - that's what JavaScript does.
/// (either through [close] or by the peer).
void sendBytes(Uint8List b);

/// Closes the WebSocket connection.
/// Closes the WebSocket connection and the [events] `Stream`.
///
/// Set the optional code and reason arguments to send close information
/// to the peer. If they are omitted, the peer will see a 1005 status code
/// with no reason.
/// 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.
///
/// If [code] is not in the range 3000-4999 then an [RangeError]
/// will be thrown.
/// Throws a [RangeError] if [code] is not in the range 3000-4999.
///
/// If [reason] is longer than 123 bytes when encoded as UTF-8 then
/// [ArgumentError] will be thrown.
///
/// [events] will be closed.
/// Throws an [ArgumentError] if [reason] is longer than 123 bytes when
/// encoded as UTF-8
///
/// Throws [XXXWebSocketConnectionClosed] if the connection is already closed
/// (including by the peer). Alternatively, we could just throw the close
/// away. Same as the reasoning for [sendText].
/// (including by the peer).
Future<void> close([int? code, String reason = '']);

/// Events received from the peer.
/// A [Stream] of [WebSocketEvent] received from the peer.
///
/// Data received by the peer will be delivered as a [TextDataReceived] or
/// [BinaryDataReceived].
///
/// If a [CloseReceived] event is received then the [Stream] will be closed. A
/// [CloseReceived] event indicates either that:
Expand Down

0 comments on commit 8b7b932

Please sign in to comment.