bind static method
Future<WsServerEndpoint>
bind({
- required Object host,
- required int port,
- required SecurityContext securityContext,
- required OnConnection onConnection,
- FrameCodec codecFactory()?,
Binds and starts a TLS WebSocket endpoint on host:port.
securityContext must provide the server certificate chain and private
key. onConnection receives every accepted connection. Pass port: 0 to
bind an ephemeral port (useful in tests); read port afterwards.
With shared true, multiple servers may bind the same address/port and
incoming connections are distributed among them. The Hub uses this when
the listener certificate is hot-reloadable: on renewal it binds a fresh
listener on the same port before draining the old one, so the swap happens
without a gap or a port-in-use race.
Implementation
static Future<WsServerEndpoint> bind({
required Object host,
required int port,
required SecurityContext securityContext,
required OnConnection onConnection,
FrameCodec Function()? codecFactory,
bool shared = false,
}) async {
final handler = webSocketHandler((channel, _) {
onConnection(
WebSocketConnection.fromChannel(
channel,
codec: codecFactory?.call() ?? FrameCodec.standard(),
),
);
});
final server = await shelf_io.serve(
const Pipeline().addHandler(handler),
host,
port,
securityContext: securityContext,
shared: shared,
);
return WsServerEndpoint._(server);
}