import ceylon.http.server.websocket {
import ceylon.buffer {
import net.gyokuro.core.internal {

"A handler that can react to WebSocket events."
shared alias WSHandler => Anything(WebSocketChannel, String)|WebSocketHandler;

"A handler for WebSockets that reacts to advanced events."
shared abstract class WebSocketHandler() {
    shared default void onOpen(WebSocketChannel channel) {}

    shared default void onClose(WebSocketChannel channel, CloseReason closeReason) {}

    shared default void onError(WebSocketChannel channel, Throwable? throwable) {}

    shared default void onText(WebSocketChannel channel, String text) {}

    shared default void onBinary(WebSocketChannel channel, ByteBuffer binary) {}

"Registers a new web socket handler for the given [[path]]. The handler can be a simple
 'onText' function, or a more advanced [[WebSocketHandler]]."
shared void websocket(String path, WSHandler handler)
        => router.registerWebSocketHandler(path, handler);