类 SubProtocolWebSocketHandler
- java.lang.Object
- org.springframework.web.socket.messaging.SubProtocolWebSocketHandler
public class SubProtocolWebSocketHandler extends Object implements WebSocketHandler, SubProtocolCapable, MessageHandler, SmartLifecycle
An implementation ofWebSocketHandlerthat delegates incoming WebSocket messages to aSubProtocolHandleralong with aMessageChannelto which the sub-protocol handler can send messages from WebSocket clients to the application.Also an implementation of
MessageHandlerthat finds the WebSocket session associated with theMessageand passes it, along with the message, to the sub-protocol handler to send messages from the application back to the client.- 从以下版本开始:
- 4.0
- 作者:
- Rossen Stoyanchev, Juergen Hoeller, Andy Wilkinson, Artem Bilan
构造器概要
构造器 构造器 说明 SubProtocolWebSocketHandler(MessageChannel clientInboundChannel, SubscribableChannel clientOutboundChannel)Create a newSubProtocolWebSocketHandlerfor the given inbound and outbound channels.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 voidaddProtocolHandler(SubProtocolHandler handler)Register a sub-protocol handler.voidafterConnectionClosed(WebSocketSession session, CloseStatus closeStatus)Invoked after the WebSocket connection has been closed by either side, or after a transport error has occurred.voidafterConnectionEstablished(WebSocketSession session)Invoked after WebSocket negotiation has succeeded and the WebSocket connection is opened and ready for use.protected WebSocketSessiondecorateSession(WebSocketSession session)Decorate the givenWebSocketSession, if desired.protected SubProtocolHandlerfindProtocolHandler(WebSocketSession session)Find aSubProtocolHandlerfor the given session.SubProtocolHandlergetDefaultProtocolHandler()Return the default sub-protocol handler to use.intgetPhase()Return the phase value of this object.Map<String,SubProtocolHandler>getProtocolHandlerMap()Return the sub-protocols keyed by protocol name.List<SubProtocolHandler>getProtocolHandlers()intgetSendBufferSizeLimit()Return the buffer-size limit (number of bytes).intgetSendTimeLimit()Return the send-time limit (milliseconds).StringgetStatsInfo()Return a String describing internal state and counters.List<String>getSubProtocols()Return all supported protocols.voidhandleMessage(Message<?> message)Handle an outbound Spring Message to a WebSocket client.voidhandleMessage(WebSocketSession session, WebSocketMessage<?> message)Handle an inbound message from a WebSocket client.voidhandleTransportError(WebSocketSession session, Throwable exception)Handle an error from the underlying WebSocket message transport.booleanisAutoStartup()Returnstrueif thisLifecyclecomponent should get started automatically by the container at the time that the containingApplicationContextgets refreshed.booleanisRunning()Check whether this component is currently running.voidsetDefaultProtocolHandler(SubProtocolHandler defaultProtocolHandler)Set theSubProtocolHandlerto use when the client did not request a sub-protocol.voidsetProtocolHandlers(List<SubProtocolHandler> protocolHandlers)Configure one or more handlers to use depending on the sub-protocol requested by the client in the WebSocket handshake request.voidsetSendBufferSizeLimit(int sendBufferSizeLimit)Specify the buffer-size limit (number of bytes).voidsetSendTimeLimit(int sendTimeLimit)Specify the send-time limit (milliseconds).voidstart()Start this component.voidstop()Stop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method.voidstop(Runnable callback)Indicates that a Lifecycle component must stop if it is currently running.booleansupportsPartialMessages()Whether the WebSocketHandler handles partial messages.StringtoString()
构造器详细资料
SubProtocolWebSocketHandler
public SubProtocolWebSocketHandler(MessageChannel clientInboundChannel, SubscribableChannel clientOutboundChannel)
Create a newSubProtocolWebSocketHandlerfor the given inbound and outbound channels.- 参数:
clientInboundChannel- the inboundMessageChannelclientOutboundChannel- the outboundMessageChannel
方法详细资料
setProtocolHandlers
public void setProtocolHandlers(List<SubProtocolHandler> protocolHandlers)
Configure one or more handlers to use depending on the sub-protocol requested by the client in the WebSocket handshake request.- 参数:
protocolHandlers- the sub-protocol handlers to use
getProtocolHandlers
public List<SubProtocolHandler> getProtocolHandlers()
addProtocolHandler
public void addProtocolHandler(SubProtocolHandler handler)
Register a sub-protocol handler.
getProtocolHandlerMap
public Map<String,SubProtocolHandler> getProtocolHandlerMap()
Return the sub-protocols keyed by protocol name.
setDefaultProtocolHandler
public void setDefaultProtocolHandler(SubProtocolHandler defaultProtocolHandler)
Set theSubProtocolHandlerto use when the client did not request a sub-protocol.- 参数:
defaultProtocolHandler- the default handler
getDefaultProtocolHandler
public SubProtocolHandler getDefaultProtocolHandler()
Return the default sub-protocol handler to use.
getSubProtocols
public List<String> getSubProtocols()
Return all supported protocols.- 指定者:
getSubProtocols在接口中SubProtocolCapable
setSendTimeLimit
public void setSendTimeLimit(int sendTimeLimit)
Specify the send-time limit (milliseconds).
getSendTimeLimit
public int getSendTimeLimit()
Return the send-time limit (milliseconds).
setSendBufferSizeLimit
public void setSendBufferSizeLimit(int sendBufferSizeLimit)
Specify the buffer-size limit (number of bytes).
getSendBufferSizeLimit
public int getSendBufferSizeLimit()
Return the buffer-size limit (number of bytes).
getStatsInfo
public String getStatsInfo()
Return a String describing internal state and counters.
isAutoStartup
public boolean isAutoStartup()
从接口复制的说明:SmartLifecycleReturnstrueif thisLifecyclecomponent should get started automatically by the container at the time that the containingApplicationContextgets refreshed.A value of
falseindicates that the component is intended to be started through an explicitLifecycle.start()call instead, analogous to a plainLifecycleimplementation.
start
public final void start()
从接口复制的说明:LifecycleStart this component.Should not throw an exception if the component is already running.
In the case of a container, this will propagate the start signal to all components that apply.
- 指定者:
start在接口中Lifecycle- 另请参阅:
SmartLifecycle.isAutoStartup()
stop
public final void stop()
从接口复制的说明:LifecycleStop this component, typically in a synchronous fashion, such that the component is fully stopped upon return of this method. Consider implementingSmartLifecycleand itsstop(Runnable)variant when asynchronous stop behavior is necessary.Note that this stop notification is not guaranteed to come before destruction: On regular shutdown,
Lifecyclebeans will first receive a stop notification before the general destruction callbacks are being propagated; however, on hot refresh during a context's lifetime or on aborted refresh attempts, a given bean's destroy method will be called without any consideration of stop signals upfront.Should not throw an exception if the component is not running (not started yet).
In the case of a container, this will propagate the stop signal to all components that apply.
- 指定者:
stop在接口中Lifecycle- 另请参阅:
SmartLifecycle.stop(Runnable),DisposableBean.destroy()
stop
public final void stop(Runnable callback)
从接口复制的说明:SmartLifecycleIndicates that a Lifecycle component must stop if it is currently running.The provided callback is used by the
LifecycleProcessorto support an ordered, and potentially concurrent, shutdown of all components having a common shutdown order value. The callback must be executed after theSmartLifecyclecomponent does indeed stop.The
LifecycleProcessorwill call only this variant of thestopmethod; i.e.Lifecycle.stop()will not be called forSmartLifecycleimplementations unless explicitly delegated to within the implementation of this method.- 指定者:
stop在接口中SmartLifecycle- 另请参阅:
Lifecycle.stop(),Phased.getPhase()
isRunning
public final boolean isRunning()
从接口复制的说明:LifecycleCheck whether this component is currently running.In the case of a container, this will return
trueonly if all components that apply are currently running.
afterConnectionEstablished
public void afterConnectionEstablished(WebSocketSession session) throws Exception
从接口复制的说明:WebSocketHandlerInvoked after WebSocket negotiation has succeeded and the WebSocket connection is opened and ready for use.- 指定者:
afterConnectionEstablished在接口中WebSocketHandler- 抛出:
Exception- this method can handle or propagate exceptions; see class-level Javadoc for details.
handleMessage
public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception
Handle an inbound message from a WebSocket client.- 指定者:
handleMessage在接口中WebSocketHandler- 抛出:
Exception- this method can handle or propagate exceptions; see class-level Javadoc for details.
handleMessage
public void handleMessage(Message<?> message) throws MessagingException
Handle an outbound Spring Message to a WebSocket client.- 指定者:
handleMessage在接口中MessageHandler- 参数:
message- the message to be handled- 抛出:
MessagingException- if the handler failed to process the message
handleTransportError
public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception
从接口复制的说明:WebSocketHandlerHandle an error from the underlying WebSocket message transport.- 指定者:
handleTransportError在接口中WebSocketHandler- 抛出:
Exception- this method can handle or propagate exceptions; see class-level Javadoc for details.
afterConnectionClosed
public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception
从接口复制的说明:WebSocketHandlerInvoked after the WebSocket connection has been closed by either side, or after a transport error has occurred. Although the session may technically still be open, depending on the underlying implementation, sending messages at this point is discouraged and most likely will not succeed.- 指定者:
afterConnectionClosed在接口中WebSocketHandler- 抛出:
Exception- this method can handle or propagate exceptions; see class-level Javadoc for details.
supportsPartialMessages
public boolean supportsPartialMessages()
从接口复制的说明:WebSocketHandlerWhether the WebSocketHandler handles partial messages. If this flag is set totrueand the underlying WebSocket server supports partial messages, then a large WebSocket message, or one of an unknown size may be split and maybe received over multiple calls toWebSocketHandler.handleMessage(WebSocketSession, WebSocketMessage). The flagWebSocketMessage.isLast()indicates if the message is partial and whether it is the last part.- 指定者:
supportsPartialMessages在接口中WebSocketHandler
decorateSession
protected WebSocketSession decorateSession(WebSocketSession session)
Decorate the givenWebSocketSession, if desired.The default implementation builds a
ConcurrentWebSocketSessionDecoratorwith the configuredsend-time limitandbuffer-size limit.- 参数:
session- the originalWebSocketSession- 返回:
- the decorated
WebSocketSession, or potentially the given session as-is - 从以下版本开始:
- 4.3.13
findProtocolHandler
protected final SubProtocolHandler findProtocolHandler(WebSocketSession session)
Find aSubProtocolHandlerfor the given session.- 参数:
session- theWebSocketSessionto find a handler for