Package io.micronaut.websocket
Interface WebSocketSession
-
- All Superinterfaces:
java.lang.AutoCloseable,io.micronaut.core.convert.value.ConvertibleValues<java.lang.Object>,java.lang.Iterable<java.util.Map.Entry<java.lang.String,java.lang.Object>>,io.micronaut.core.convert.value.MutableConvertibleValues<java.lang.Object>,io.micronaut.core.value.ValueResolver<java.lang.CharSequence>
public interface WebSocketSession extends io.micronaut.core.convert.value.MutableConvertibleValues<java.lang.Object>, java.lang.AutoCloseableRepresents an open WebSocket connection. Based largely onjavax.websocketand likely to be able to implement the spec in the future.- Since:
- 1.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description voidclose()voidclose(CloseReason closeReason)Close the session with the given event.io.micronaut.core.convert.value.MutableConvertibleValues<java.lang.Object>getAttributes()java.lang.StringgetId()The ID of the session.java.util.Set<? extends WebSocketSession>getOpenSessions()The current open sessions.java.lang.StringgetProtocolVersion()The protocol version of the WebSocket protocol currently being used.default io.micronaut.core.convert.value.ConvertibleMultiValues<java.lang.String>getRequestParameters()The request parameters used to create this session.java.net.URIgetRequestURI()The request URI this session was opened under.default java.util.Optional<java.lang.String>getSubprotocol()The subprotocol if one is used.default io.micronaut.core.convert.value.ConvertibleValues<java.lang.Object>getUriVariables()Any matching URI path variables.default java.util.Optional<java.security.Principal>getUserPrincipal()The userPrincipalused to create the session.booleanisOpen()Whether the session is open.booleanisSecure()Whether the connection is secure.booleanisWritable()Whether the session is writable.default <T> org.reactivestreams.Publisher<T>send(T message)Send the given message to the remote peer.<T> org.reactivestreams.Publisher<T>send(T message, io.micronaut.http.MediaType mediaType)Send the given message to the remote peer.default <T> java.util.concurrent.CompletableFuture<T>sendAsync(T message)Send the given message to the remote peer asynchronously.<T> java.util.concurrent.CompletableFuture<T>sendAsync(T message, io.micronaut.http.MediaType mediaType)Send the given message to the remote peer asynchronously.default java.util.concurrent.CompletableFuture<?>sendPingAsync(byte[] content)Send a ping through this WebSocket.default voidsendSync(java.lang.Object message)Send the given message to the remote peer synchronously.default voidsendSync(java.lang.Object message, io.micronaut.http.MediaType mediaType)Send the given message to the remote peer synchronously.-
Methods inherited from interface io.micronaut.core.convert.value.ConvertibleValues
asMap, asMap, asProperties, contains, forEach, getValue, getValueType, isEmpty, iterator, names, subMap, subMap, subMap, values
-
-
-
-
Method Detail
-
getId
java.lang.String getId()
The ID of the session.- Returns:
- The ID of the session
-
getAttributes
io.micronaut.core.convert.value.MutableConvertibleValues<java.lang.Object> getAttributes()
- Returns:
- Only the attributes of the session
-
isOpen
boolean isOpen()
Whether the session is open.- Returns:
- True if it is
-
isWritable
boolean isWritable()
Whether the session is writable. It may not be writable, if the buffer is currently full- Returns:
- True if it is
-
isSecure
boolean isSecure()
Whether the connection is secure.- Returns:
- True if it is secure
-
getOpenSessions
java.util.Set<? extends WebSocketSession> getOpenSessions()
The current open sessions.- Returns:
- The open sessions
-
getRequestURI
java.net.URI getRequestURI()
The request URI this session was opened under.- Returns:
- The request URI
-
getProtocolVersion
java.lang.String getProtocolVersion()
The protocol version of the WebSocket protocol currently being used.- Returns:
- The protocol version
-
send
<T> org.reactivestreams.Publisher<T> send(T message, io.micronaut.http.MediaType mediaType)Send the given message to the remote peer. The resultingPublisherdoes not start sending until subscribed to. If you return it from Micronaut annotated methods such asOnOpenandOnMessage, Micronaut will subscribe to it and send the message without blocking.- Type Parameters:
T- The message type- Parameters:
message- The messagemediaType- The media type of the message. Used to lookup an appropriate codec via theMediaTypeCodecRegistry.- Returns:
- A
Publisherthat either emits an error or emits the message once it has been published successfully.
-
sendAsync
<T> java.util.concurrent.CompletableFuture<T> sendAsync(T message, io.micronaut.http.MediaType mediaType)Send the given message to the remote peer asynchronously.- Type Parameters:
T- The message type- Parameters:
message- The messagemediaType- The media type of the message. Used to lookup an appropriate codec via theMediaTypeCodecRegistry.- Returns:
- A
CompletableFuturethat tracks the execution.CompletableFuture.get()and related methods will return the message on success, on error throw the underlying Exception.
-
sendSync
default void sendSync(java.lang.Object message, io.micronaut.http.MediaType mediaType)Send the given message to the remote peer synchronously.- Parameters:
message- The messagemediaType- The media type of the message. Used to lookup an appropriate codec via theMediaTypeCodecRegistry.
-
send
default <T> org.reactivestreams.Publisher<T> send(T message)
Send the given message to the remote peer. The resultingPublisherdoes not start sending until subscribed to. If you return it from Micronaut annotated methods such asOnOpenandOnMessage, Micronaut will subscribe to it and send the message without blocking.- Type Parameters:
T- The message type- Parameters:
message- The message- Returns:
- A
Publisherthat either emits an error or emits the message once it has been published successfully.
-
sendAsync
default <T> java.util.concurrent.CompletableFuture<T> sendAsync(T message)
Send the given message to the remote peer asynchronously.- Type Parameters:
T- The message type- Parameters:
message- The message- Returns:
- A
CompletableFuturethat tracks the execution.CompletableFuture.get()and related methods will return the message on success, on error throw the underlying Exception.
-
sendSync
default void sendSync(java.lang.Object message)
Send the given message to the remote peer synchronously.- Parameters:
message- The message
-
sendPingAsync
@NonNull default java.util.concurrent.CompletableFuture<?> sendPingAsync(@NonNull byte[] content)Send a ping through this WebSocket. The pong reply can be intercepted using a@OnMessagemethod that accepts aWebSocketPongMessage.- Parameters:
content- The content of the ping. The remote should return the same content in itsWebSocketPongMessage.- Returns:
- A future that completes when the ping has been sent. (Not when the pong has been received!)
-
getSubprotocol
default java.util.Optional<java.lang.String> getSubprotocol()
The subprotocol if one is used.- Returns:
- The subprotocol
-
getRequestParameters
default io.micronaut.core.convert.value.ConvertibleMultiValues<java.lang.String> getRequestParameters()
The request parameters used to create this session.- Returns:
- The request parameters
-
getUriVariables
default io.micronaut.core.convert.value.ConvertibleValues<java.lang.Object> getUriVariables()
Any matching URI path variables.- Returns:
- The path variables
-
getUserPrincipal
default java.util.Optional<java.security.Principal> getUserPrincipal()
The userPrincipalused to create the session.- Returns:
- The
Principal
-
close
void close()
- Specified by:
closein interfacejava.lang.AutoCloseable
-
close
void close(CloseReason closeReason)
Close the session with the given event.- Parameters:
closeReason- The close event
-
-