[doc/io] Add a RawSocketEvent example.

Bug: https://github.com/dart-lang/sdk/issues/33721
Change-Id: I4e8f35cec8d3e689469a63826d118cfa2fe42218
CoreLibraryReviewExempt: Documentation-only change
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277701
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Brian Quinlan <bquinlan@google.com>
This commit is contained in:
Brian Quinlan 2023-10-30 16:53:17 +00:00 committed by Commit Queue
parent 3cab7a2ce5
commit 942e897bf5

View file

@ -464,11 +464,42 @@ final class RawSocketOption {
external static int _getOptionValue(int key);
}
/// Events for the [RawSocket].
/// Events for the [RawDatagramSocket], [RawSecureSocket], and [RawSocket].
///
/// These event objects are used by the [Stream] behavior of [RawSocket]
/// These event objects are used by the [Stream] behavior of the sockets
/// (for example [RawSocket.listen], [RawSocket.forEach])
/// when the socket's state change.
///
/// ```dart
/// import 'dart:convert';
/// import 'dart:io';
///
/// void main() async {
/// final socket = await RawSocket.connect("example.com", 80);
///
/// socket.listen((event) {
/// switch (event) {
/// case RawSocketEvent.read:
/// final data = socket.read();
/// if (data != null) {
/// print(ascii.decode(data));
/// }
/// break;
/// case RawSocketEvent.write:
/// socket.write(ascii.encode('GET /\r\nHost: example.com\r\n\r\n'));
/// socket.writeEventsEnabled = false;
/// break;
/// case RawSocketEvent.readClosed:
/// socket.close();
/// break;
/// case RawSocketEvent.closed:
/// break;
/// default:
/// throw "Unexpected event $event";
/// }
/// });
/// }
/// ```
class RawSocketEvent {
/// An event indicates the socket is ready to be read.
static const RawSocketEvent read = const RawSocketEvent._(0);