Aether supports WebSocket connections for real-time bidirectional communication.
Use the ws method in the router DSL.
router {
ws("/chat") { session ->
// Handle new connection
println("New connection: ${session.id}")
// Receive messages
for (frame in session.incoming) {
when (frame) {
is Frame.Text -> {
val text = frame.readText()
// Echo back
session.send(Frame.Text("Echo: $text"))
}
is Frame.Binary -> {
// Handle binary
}
is Frame.Close -> {
// Handle close
}
}
}
}
}
The WebSocketSession represents the connection.
id: Unique identifier for the session.incoming: A ReceiveChannel<Frame> of incoming frames.outgoing: A SendChannel<Frame> for outgoing frames.send(frame: Frame): Sends a frame.close(reason: CloseReason): Closes the connection.Frame.Text: Contains text data.Frame.Binary: Contains byte data.Frame.Ping / Frame.Pong: Keep-alive frames.Frame.Close: Connection closure frame.