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 TypeMethodDescriptionCloses the WebSocket session with the specified close reason and delivery guarantee.booleanisOpen()Determines whether the WebSocket session is currently open.booleanonClose()sendMessage(Object value, 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.tryCompleteRequest(DeserializingMessage message) tryHandleRequest(DeserializingMessage message, Handler<DeserializingMessage> handler) 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
-
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<HandlerInvoker> tryHandleRequest(DeserializingMessage message, Handler<DeserializingMessage> handler) -
tryCompleteRequest
-
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 ensuring the closure message is sent, which can be one of theGuaranteevalues: NONE, SENT, or STORED.- Returns:
- a
CompletableFuture<Void>that completes when the close operation is performed successfully.
-
onClose
public boolean onClose() -
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.
-