SocketSession

class SocketSession(session: DefaultWebSocketServerSession)

Represents a WebSocket session and provides methods for handling events and messages.

This class allows WebSocket clients to interact with the server, send/receive messages, and join/leave rooms.

Constructors

Link copied to clipboard
constructor(session: DefaultWebSocketServerSession)

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val id: UUID
Link copied to clipboard
var player: Player?
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
suspend fun close(reason: String? = null): SocketSession

Closes the WebSocket session with an optional reason.

Link copied to clipboard
suspend fun emit(event: String, data: Any, acknowledgment: Boolean = false, timeout: Long = 5000): SocketSession

Emits an event with data to the WebSocket client, optionally requiring an acknowledgment.

Link copied to clipboard
fun header(key: String): String?

Retrieves a header value from the WebSocket connection.

Link copied to clipboard

Checks if the WebSocket session is still open.

Link copied to clipboard
fun join(newRoom: String): SocketSession

Joins a specific room for the session.

Link copied to clipboard

Leaves the current room and reverts to the default "global" room.

Link copied to clipboard
fun on(event: String, callback: suspend (JsonElement) -> Unit): SocketSession

Registers an event listener for a specific event.

Link copied to clipboard
fun onAny(callback: suspend (String, JsonElement) -> Unit): SocketSession

Registers a wildcard event listener for all events.

Link copied to clipboard
fun onClose(callback: suspend (String) -> Unit): SocketSession

Sets the callback function to be invoked when the WebSocket session is closed.

Link copied to clipboard
inline fun <T> onTyped(event: String, crossinline callback: suspend (T) -> Unit): SocketSession

Registers a typed event listener for a specific event, decoding the event data to the specified type.

Link copied to clipboard
suspend fun send(text: String): SocketSession

Sends a message to the WebSocket client.