Package io.micronaut.websocket
Interface WebSocketSession
- All Superinterfaces:
AutoCloseable
,ConversionServiceProvider
,ConvertibleValues<Object>
,Iterable<Map.Entry<String,
,Object>> MutableConvertibleValues<Object>
,ValueResolver<CharSequence>
- All Known Implementing Classes:
NettyWebSocketSession
Represents an open WebSocket connection. Based largely on
javax.websocket
and likely to be able to implement the spec in the future.- Since:
- 1.0
- Author:
- graemerocher
-
Field Summary
Fields inherited from interface io.micronaut.core.convert.value.ConvertibleValues
EMPTY
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
close
(CloseReason closeReason) Close the session with the given event.getId()
The ID of the session.Set<? extends WebSocketSession>
The current open sessions.The protocol version of the WebSocket protocol currently being used.default ConvertibleMultiValues<String>
The request parameters used to create this session.The request URI this session was opened under.The subprotocol if one is used.default ConvertibleValues<Object>
Any matching URI path variables.The userPrincipal
used to create the session.boolean
isOpen()
Whether the session is open.boolean
isSecure()
Whether the connection is secure.boolean
Whether the session is writable.default <T> Publisher<T>
send
(T message) Send the given message to the remote peer.<T> Publisher<T>
Send the given message to the remote peer.default <T> CompletableFuture<T>
sendAsync
(T message) Send the given message to the remote peer asynchronously.<T> CompletableFuture<T>
Send the given message to the remote peer asynchronously.default @NonNull CompletableFuture<?>
sendPingAsync
(@io.micronaut.core.annotation.NonNull byte[] content) Send a ping through this WebSocket.default void
Send the given message to the remote peer synchronously.default void
Send the given message to the remote peer synchronously.Methods inherited from interface io.micronaut.core.convert.value.ConvertibleValues
asMap, asMap, asProperties, contains, forEach, getConversionService, getValue, getValueType, isEmpty, iterator, names, subMap, subMap, subMap, values
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface io.micronaut.core.convert.value.MutableConvertibleValues
clear, put, putAll, putAll, remove
Methods inherited from interface io.micronaut.core.value.ValueResolver
get, get, get, get
-
Method Details
-
getId
String getId()The ID of the session.- Returns:
- The ID of the session
-
getAttributes
MutableConvertibleValues<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
Set<? extends WebSocketSession> getOpenSessions()The current open sessions.- Returns:
- The open sessions
-
getRequestURI
URI getRequestURI()The request URI this session was opened under.- Returns:
- The request URI
-
getProtocolVersion
String getProtocolVersion()The protocol version of the WebSocket protocol currently being used.- Returns:
- The protocol version
-
send
Send the given message to the remote peer. The resultingPublisher
does not start sending until subscribed to. If you return it from Micronaut annotated methods such asOnOpen
andOnMessage
, 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
Publisher
that either emits an error or emits the message once it has been published successfully.
-
sendAsync
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
CompletableFuture
that tracks the execution.CompletableFuture.get()
and related methods will return the message on success, on error throw the underlying Exception.
-
sendSync
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
Send the given message to the remote peer. The resultingPublisher
does not start sending until subscribed to. If you return it from Micronaut annotated methods such asOnOpen
andOnMessage
, Micronaut will subscribe to it and send the message without blocking.- Type Parameters:
T
- The message type- Parameters:
message
- The message- Returns:
- A
Publisher
that either emits an error or emits the message once it has been published successfully.
-
sendAsync
Send the given message to the remote peer asynchronously.- Type Parameters:
T
- The message type- Parameters:
message
- The message- Returns:
- A
CompletableFuture
that tracks the execution.CompletableFuture.get()
and related methods will return the message on success, on error throw the underlying Exception.
-
sendSync
Send the given message to the remote peer synchronously.- Parameters:
message
- The message
-
sendPingAsync
@NonNull default @NonNull CompletableFuture<?> sendPingAsync(@NonNull @io.micronaut.core.annotation.NonNull byte[] content) Send a ping through this WebSocket. The pong reply can be intercepted using a@OnMessage
method 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
The subprotocol if one is used.- Returns:
- The subprotocol
-
getRequestParameters
The request parameters used to create this session.- Returns:
- The request parameters
-
getUriVariables
Any matching URI path variables.- Returns:
- The path variables
-
getUserPrincipal
The userPrincipal
used to create the session.- Returns:
- The
Principal
-
close
void close()- Specified by:
close
in interfaceAutoCloseable
-
close
Close the session with the given event.- Parameters:
closeReason
- The close event
-