WebsocketController

WebsocketController owns the lifecycle of a WebSocket connection, handles incoming messages, and dispatches messages to WebsocketAction classes.

Overview

A WebSocket connection differs from a normal HTTP request: it stays open and can carry multiple messages. Lifecycle callbacks such as onOpen, onClose, and onError manage the channel.

This preview illustrates connection establishment and the lifecycle callbacks that may follow.

CODE
public void onOpen(WebsocketContext context){
    // Connection established.
}

public void onClose(WebsocketContext context, CloseReason reason){
    // Connection closed.
}
RESULT
WebSocket session
HTTP session
Handshake parameters

Dispatch example

This preview shows how an incoming message can be intercepted, dispatched to an action, and answered with a PushAction.

CODE
public WebsocketAction interceptRequestedAction(
        WebsocketContext context,WebsocketAction action){
    return action;
}
RESULT
Incoming JSON message
WebsocketAction dispatch
PushAction response

How it works

CODE
WebsocketContext context = ...;
Session session = context.getWebsocketSession();
JSONMessage message = context.getWebsocketMessage();

Sending updates

PushAction can replace a component on the connected client without a new HTTP navigation.

CODE
context.sendClientAction(
    new PushAction.UpdateElement("result",new Text("Message received"))
);

Design guidance

An actual live demo requires your configured endpoint URL, session setup, and action mapping. These examples deliberately stop short of inventing a server connection.

Full API reference

These examples introduce the most important behavior without attempting to document every method. For the complete reference, see https://doc.jdance.org.