Class DefaultSocketSession
java.lang.Object
io.fluxzero.sdk.web.DefaultSocketSession
- All Implemented Interfaces:
SocketSession
Default implementation of a
SocketSession for managing Fluxzero WebSocket sessions.
This class handles bidirectional communication over a WebSocket session, including:
- Sending regular messages, pings, and close instructions using various
Guaranteelevels - Sending and awaiting responses to
Requestobjects viaSocketRequest/SocketResponse - Tracking and completing pending requests with timeout handling
- Dispatching and serializing socket messages using the configured
ResultGatewayandTaskScheduler - Supporting graceful closure of sessions and safe completion of outstanding requests
ConcurrentHashMap to track open request-response pairs and leverages
HandlerInvoker delegation to wrap handler execution and return results as SocketResponses.
On receiving an incoming SocketRequest, it attempts to resolve and invoke the appropriate handler.
On receiving a SocketResponse, it attempts to match and complete the original request.
Sessions are identified by a unique session ID and can include custom headers. Delivery guarantees are honored
according to the underlying ResultGateway implementation.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionclose(int code, io.fluxzero.common.Guarantee guarantee) Closes the WebSocket session with the specified close reason and delivery guarantee.getUser()Returns the user currently associated with this socket session.booleanisOpen()Determines whether the WebSocket session is currently open.booleanonClose()Marks the session closed and fails pending requests.sendMessage(Object value, io.fluxzero.common.Guarantee guarantee) Sends a message over the WebSocket session with the specified delivery guarantee.Sends a ping message with the specified value to the WebSocket session.<R> CompletionStage<R> sendRequest(Request<R> request, Duration timeout) Sends a request over the WebSocket session with a specified timeout duration.Optional<io.fluxzero.common.handling.HandlerInvoker> tryCompleteRequest(DeserializingMessage message) Prepares an invoker that completes the pending request correlated with a valid socket response.Optional<io.fluxzero.common.handling.HandlerInvoker> tryHandleRequest(DeserializingMessage message, io.fluxzero.common.handling.Handler<DeserializingMessage> handler) Prepares an invoker for a valid socket request, wrapping synchronous or asynchronous results and failures in correlated socket responses.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface SocketSession
close, close, close, sendMessage, sendPing, sendRequest, sessionId
-
Constructor Details
-
DefaultSocketSession
public DefaultSocketSession()
-
-
Method Details
-
getUser
Returns the user currently associated with this socket session.- Returns:
- the current session user, or
nullif the session has no authenticated user
-
sendMessage
Description copied from interface:SocketSessionSends a message over the WebSocket session with the specified delivery guarantee.- Specified by:
sendMessagein interfaceSocketSession- Parameters:
value- the value of the message to be sent. The value will be serialized and transmitted as bytes.guarantee- the delivery guarantee for the message.- Returns:
- a
CompletableFuturethat completes when the message has been sent or stored, or fails with an exception if that operation fails.
-
sendRequest
Description copied from interface:SocketSessionSends a request over the WebSocket session with a specified timeout duration. This method asynchronously sends the given request and waits for the corresponding response. The request object will be wrapped bySocketRequestand then sent over the WebSocket session. Once the session receives aSocketResponsethe request will be completed.- Specified by:
sendRequestin interfaceSocketSession- Type Parameters:
R- the type of the response expected from the request- Parameters:
request- the request object to be senttimeout- the timeout duration for the request, after which it will fail if no response is received- Returns:
- a
CompletionStage<R>that completes with the response of the specified typeRor completes exceptionally if an error occurs or the timeout is exceeded - See Also:
-
tryHandleRequest
public Optional<io.fluxzero.common.handling.HandlerInvoker> tryHandleRequest(DeserializingMessage message, io.fluxzero.common.handling.Handler<DeserializingMessage> handler) Prepares an invoker for a valid socket request, wrapping synchronous or asynchronous results and failures in correlated socket responses. Returns empty for invalid requests or when no handler matches. -
tryCompleteRequest
public Optional<io.fluxzero.common.handling.HandlerInvoker> tryCompleteRequest(DeserializingMessage message) Prepares an invoker that completes the pending request correlated with a valid socket response. Returns empty for invalid responses; response errors and deserialization failures complete the pending future exceptionally. -
sendPing
Description copied from interface:SocketSessionSends a ping message with the specified value to the WebSocket session.- Specified by:
sendPingin interfaceSocketSession- Parameters:
value- the value to be included in the ping message. This can be any object that is serializable or manageable according to the WebSocket session's implementation.guarantee- the delivery guarantee for the ping message. It specifies how assurances (e.g., sending, storing) are handled for the message delivery.- Returns:
- a CompletableFuture that completes once the ping message is processed or the delivery guarantee has been fulfilled.
-
close
Description copied from interface:SocketSessionCloses the WebSocket session with the specified close reason and delivery guarantee.- Specified by:
closein interfaceSocketSession- Parameters:
code- an integer indicating the reason for closing the WebSocket session, typically based on WebSocket close codes (e.g., 1000 for normal closure).guarantee- the delivery guarantee for the close message, includingGuarantee.DEFAULT.- Returns:
- a future that completes according to the close message's selected delivery guarantee.
-
onClose
public boolean onClose()Marks the session closed and fails pending requests. Returns true only for the first close transition. -
isOpen
public boolean isOpen()Description copied from interface:SocketSessionDetermines whether the WebSocket session is currently open.- Specified by:
isOpenin interfaceSocketSession- Returns:
- true if the session is open; false otherwise.
-