Class AbstractWebSocketSession<T>
java.lang.Object
org.springframework.web.socket.adapter.AbstractWebSocketSession<T>
- Type Parameters:
T
- the native session type
- All Implemented Interfaces:
Closeable
,AutoCloseable
,NativeWebSocketSession
,WebSocketSession
- Direct Known Subclasses:
JettyWebSocketSession
,StandardWebSocketSession
An abstract base class for implementations of
WebSocketSession
.- Since:
- 4.0
- Author:
- Rossen Stoyanchev
-
Field Summary
-
Constructor Summary
ConstructorDescriptionAbstractWebSocketSession
(Map<String, Object> attributes) Create a new instance and associate the given attributes with it. -
Method Summary
Modifier and TypeMethodDescriptionprotected final void
final void
close()
Close the WebSocket connection with status 1000, i.e.final void
close
(CloseStatus status) Close the WebSocket connection with the given close status.protected abstract void
closeInternal
(CloseStatus status) Return the map with attributes associated with the WebSocket session.Return the underlying native WebSocketSession.<R> R
getNativeSession
(Class<R> requiredType) Return the underlying native WebSocketSession, if available.void
initializeNativeSession
(T session) protected abstract void
sendBinaryMessage
(BinaryMessage message) final void
sendMessage
(WebSocketMessage<?> message) Send a WebSocket message: eitherTextMessage
orBinaryMessage
.protected abstract void
sendPingMessage
(PingMessage message) protected abstract void
sendPongMessage
(PongMessage message) protected abstract void
sendTextMessage
(TextMessage message) toString()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface org.springframework.web.socket.WebSocketSession
getAcceptedProtocol, getBinaryMessageSizeLimit, getExtensions, getHandshakeHeaders, getId, getLocalAddress, getPrincipal, getRemoteAddress, getTextMessageSizeLimit, getUri, isOpen, setBinaryMessageSizeLimit, setTextMessageSizeLimit
-
Field Details
-
idGenerator
-
logger
-
-
Constructor Details
-
AbstractWebSocketSession
Create a new instance and associate the given attributes with it.- Parameters:
attributes
- the attributes from the HTTP handshake to associate with the WebSocket session; the provided attributes are copied, the original map is not used.
-
-
Method Details
-
getAttributes
Description copied from interface:WebSocketSession
Return the map with attributes associated with the WebSocket session.On the server side the map can be populated initially through a
HandshakeInterceptor
. On the client side the map can be populated viaWebSocketClient
handshake methods.- Specified by:
getAttributes
in interfaceWebSocketSession
- Returns:
- a Map with the session attributes (never
null
)
-
getNativeSession
Description copied from interface:NativeWebSocketSession
Return the underlying native WebSocketSession.- Specified by:
getNativeSession
in interfaceNativeWebSocketSession
-
getNativeSession
Description copied from interface:NativeWebSocketSession
Return the underlying native WebSocketSession, if available.- Specified by:
getNativeSession
in interfaceNativeWebSocketSession
- Parameters:
requiredType
- the required type of the session- Returns:
- the native session of the required type,
or
null
if not available
-
initializeNativeSession
-
checkNativeSessionInitialized
protected final void checkNativeSessionInitialized() -
sendMessage
Description copied from interface:WebSocketSession
Send a WebSocket message: eitherTextMessage
orBinaryMessage
.Note: The underlying standard WebSocket session (JSR-356) does not allow concurrent sending. Therefore, sending must be synchronized. To ensure that, one option is to wrap the
WebSocketSession
with theConcurrentWebSocketSessionDecorator
.- Specified by:
sendMessage
in interfaceWebSocketSession
- Throws:
IOException
- See Also:
-
sendTextMessage
- Throws:
IOException
-
sendBinaryMessage
- Throws:
IOException
-
sendPingMessage
- Throws:
IOException
-
sendPongMessage
- Throws:
IOException
-
close
Description copied from interface:WebSocketSession
Close the WebSocket connection with status 1000, i.e. equivalent to:session.close(CloseStatus.NORMAL);
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceWebSocketSession
- Throws:
IOException
-
close
Description copied from interface:WebSocketSession
Close the WebSocket connection with the given close status.- Specified by:
close
in interfaceWebSocketSession
- Throws:
IOException
-
closeInternal
- Throws:
IOException
-
toString
-