serve static method
Future<AgentWebSocketSurface>
serve({
- required AgentSurfaceLink link,
- InternetAddress? address,
- int port = 0,
- String? protocolScheme,
- String owner = 'server',
- AgentLinkDirectory? directory,
Implementation
static Future<AgentWebSocketSurface> serve({
required final AgentSurfaceLink link,
final InternetAddress? address,
final int port = 0,
final String? protocolScheme,
final String owner = 'server',
final AgentLinkDirectory? directory,
}) async {
final server = await HttpServer.bind(
address ?? InternetAddress.loopbackIPv4,
port,
);
final host = server.address.host;
final boundPort = server.port;
final scheme = protocolScheme?.trim();
final announcement = scheme == null || scheme.isEmpty
? null
: AgentLinkAnnouncement(
protocolScheme: scheme,
owner: owner,
websocket: 'ws://$host:$boundPort$path',
httpInvoke: 'http://$host:$boundPort${AgentHttpInvoke.defaultPath}',
discover: 'http://$host:$boundPort$discoverPath',
);
late final AgentWebSocketSurface surface;
// Cancelled in [close]. The server must outlive this listen call.
// ignore: cancel_subscriptions
final subscription = link.watch().listen((final observation) {
surface._broadcast(observation);
});
surface = AgentWebSocketSurface._(
server,
link,
subscription,
announcement == null ? null : (directory ?? AgentLinkDirectory()),
scheme,
announcement,
);
if (announcement != null) {
await surface._directory!.publish(announcement);
}
server.listen(surface._onRequest);
return surface;
}