public class PerConnectionWebSocketHandler extends java.lang.Object implements WebSocketHandler, BeanFactoryAware
WebSocketHandler
that initializes and destroys a WebSocketHandler
instance for each WebSocket connection and delegates all other methods to it.
Essentially create an instance of this class once, providing the type of
WebSocketHandler
class to create for each connection, and then pass it to any
API method that expects a WebSocketHandler
.
If initializing the target WebSocketHandler
type requires a Spring BeanFctory,
then the setBeanFactory(BeanFactory)
property accordingly. Simply declaring
this class as a Spring bean will do that. Otherwise, WebSocketHandler
instances
of the target type will be created using the default constructor.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<WebSocketSession,WebSocketHandler> |
handlers |
private static Log |
logger |
private BeanCreatingHandlerProvider<WebSocketHandler> |
provider |
private boolean |
supportsPartialMessages |
Constructor and Description |
---|
PerConnectionWebSocketHandler(java.lang.Class<? extends WebSocketHandler> handlerType) |
PerConnectionWebSocketHandler(java.lang.Class<? extends WebSocketHandler> handlerType,
boolean supportsPartialMessages) |
Modifier and Type | Method and Description |
---|---|
void |
afterConnectionClosed(WebSocketSession session,
CloseStatus closeStatus)
Invoked after the WebSocket connection has been closed by either side, or after a
transport error has occurred.
|
void |
afterConnectionEstablished(WebSocketSession session)
Invoked after WebSocket negotiation has succeeded and the WebSocket connection is
opened and ready for use.
|
private void |
destroy(WebSocketSession session) |
private WebSocketHandler |
getHandler(WebSocketSession session) |
void |
handleMessage(WebSocketSession session,
WebSocketMessage<?> message)
Invoked when a new WebSocket message arrives.
|
void |
handleTransportError(WebSocketSession session,
java.lang.Throwable exception)
Handle an error from the underlying WebSocket message transport.
|
void |
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.
|
boolean |
supportsPartialMessages()
Whether the WebSocketHandler handles messages in parts.
|
java.lang.String |
toString() |
private static final Log logger
private final BeanCreatingHandlerProvider<WebSocketHandler> provider
private final java.util.Map<WebSocketSession,WebSocketHandler> handlers
private final boolean supportsPartialMessages
public PerConnectionWebSocketHandler(java.lang.Class<? extends WebSocketHandler> handlerType)
public PerConnectionWebSocketHandler(java.lang.Class<? extends WebSocketHandler> handlerType, boolean supportsPartialMessages)
public void setBeanFactory(BeanFactory beanFactory) throws BeansException
BeanFactoryAware
Invoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeansException
- in case of initialization errorsBeanInitializationException
public void afterConnectionEstablished(WebSocketSession session) throws java.lang.Exception
WebSocketHandler
afterConnectionEstablished
in interface WebSocketHandler
java.lang.Exception
- this method can handle or propagate exceptions; see class-level
Javadoc for details.public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws java.lang.Exception
WebSocketHandler
handleMessage
in interface WebSocketHandler
java.lang.Exception
- this method can handle or propagate exceptions; see class-level
Javadoc for details.private WebSocketHandler getHandler(WebSocketSession session)
public void handleTransportError(WebSocketSession session, java.lang.Throwable exception) throws java.lang.Exception
WebSocketHandler
handleTransportError
in interface WebSocketHandler
java.lang.Exception
- this method can handle or propagate exceptions; see class-level
Javadoc for details.public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws java.lang.Exception
WebSocketHandler
afterConnectionClosed
in interface WebSocketHandler
java.lang.Exception
- this method can handle or propagate exceptions; see class-level
Javadoc for details.private void destroy(WebSocketSession session)
public boolean supportsPartialMessages()
WebSocketHandler
supportsPartialMessages
in interface WebSocketHandler
public java.lang.String toString()
toString
in class java.lang.Object