Package io.undertow.websockets.core
Class WebSocketChannel
- java.lang.Object
-
- io.undertow.server.protocol.framed.AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
- io.undertow.websockets.core.WebSocketChannel
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,java.nio.channels.Channel,java.nio.channels.InterruptibleChannel,org.xnio.channels.BoundChannel,org.xnio.channels.CloseableChannel,org.xnio.channels.Configurable,org.xnio.channels.ConnectedChannel
- Direct Known Subclasses:
WebSocket07Channel
public abstract class WebSocketChannel extends AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
AConnectedChannelwhich can be used to send and receive WebSocket Frames.- Author:
- Norman Maurer, Stuart Douglas
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceWebSocketChannel.PartialFrameInterface that represents a frame channel that is in the process of being created
-
Field Summary
Fields Modifier and Type Field Description protected ExtensionFunctionextensionFunctionprotected booleanextensionsSupportedprotected StreamSourceFrameChannelfragmentedChannelprotected booleanhasReservedOpCodestatic java.lang.StringWEB_SOCKETS_READ_TIMEOUTConfigure a read timeout for a web socket, in milliseconds.static java.lang.StringWEB_SOCKETS_WRITE_TIMEOUTConfigure a write timeout for a web socket, in milliseconds.
-
Constructor Summary
Constructors Modifier Constructor Description protectedWebSocketChannel(org.xnio.StreamConnection connectedStreamChannel, ByteBufferPool bufferPool, WebSocketVersion version, java.lang.String wsUrl, java.lang.String subProtocol, boolean client, boolean extensionsSupported, ExtensionFunction extensionFunction, java.util.Set<WebSocketChannel> peerConnections, org.xnio.OptionMap options)Create a newWebSocketChannel8
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description booleanareExtensionsSupported()Returnstrueif extensions are supported by this WebSocket Channel.protected StreamSourceFrameChannelcreateChannel(FrameHeaderData frameHeaderData, PooledByteBuffer frameData)Method that creates the actual stream source channel implementation that is in use.protected IdleTimeoutConduitcreateIdleTimeoutChannel(org.xnio.StreamConnection connectedStreamChannel)protected abstract StreamSinkFrameChannelcreateStreamSinkChannel(WebSocketFrameType type)Create a new StreamSinkFrameChannel which can be used to send a WebSocket Frame of the typeWebSocketFrameType.java.lang.ObjectgetAttribute(java.lang.String key)intgetCloseCode()java.lang.StringgetCloseReason()java.net.InetSocketAddressgetDestinationAddress()Get the destination address of the WebSocket Channel.ExtensionFunctiongetExtensionFunction()protected WebSocketFramePrioritygetFramePriority()java.util.Set<WebSocketChannel>getPeerConnections()Returns all 'peer' web socket connections that were created from the same endpoint.protected java.util.Collection<AbstractFramedStreamSourceChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>>getReceivers()java.lang.StringgetRequestScheme()Get the request URI scheme.java.net.InetSocketAddressgetSourceAddress()Get the source address of the WebSocket Channel.java.lang.StringgetSubProtocol()java.util.Set<java.lang.String>getSubProtocols()Deprecated.java.lang.StringgetUrl()Return the URL of the WebSocket endpoint.WebSocketVersiongetVersion()Return theWebSocketVersionwhich is usedprotected voidhandleBrokenSinkChannel(java.lang.Throwable e)Method that is invoked when then write side of a channel is broken.protected voidhandleBrokenSourceChannel(java.lang.Throwable e)Method that is invoked when the read side of the channel is broken.booleanisClient()booleanisCloseFrameReceived()booleanisCloseFrameSent()booleanisCloseInitiatedByRemotePeer()If this is true the session is being closed because the remote peer sent a close frameprotected booleanisLastFrameReceived()Returns true if the protocol specific final frame has been received.protected booleanisLastFrameSent()protected booleanisReadsBroken()booleanisSecure()Returntrueif this is handled via WebSocket Secure.protected voidlastDataRead()Method than is invoked when read() returns -1.protected voidmarkReadsBroken(java.lang.Throwable cause)Called when a source sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.protected FrameHeaderDataparseFrame(java.nio.ByteBuffer data)Attempts to parse an incoming frame header from the data in the buffer.protected abstract WebSocketChannel.PartialFramereceiveFrame()Create a newStreamSourceFrameChannelwhich can be used to read the data of the received FrameStreamSinkFrameChannelsend(WebSocketFrameType type)Returns a newStreamSinkFrameChannelfor sending the givenWebSocketFrameTypewith the given payload.voidsendClose()Send a Close frame without a payloadbooleansetAttribute(java.lang.String key, java.lang.Object value)voidsetCloseCode(int closeCode)voidsetCloseReason(java.lang.String closeReason)-
Methods inherited from class io.undertow.server.protocol.framed.AbstractFramedChannel
addCloseTask, close, closeSubChannels, flush, flushSenders, getBufferPool, getCloseSetter, getIdleTimeout, getIoThread, getLocalAddress, getLocalAddress, getOption, getPeerAddress, getPeerAddress, getReceiveSetter, getSettings, getUnderlyingConnection, getWorker, isOpen, isReceivesResumed, isRequireExplicitFlush, isWritesBroken, markWritesBroken, queueFrame, recalculateHeldFrames, receive, resumeReceives, setIdleTimeout, setOption, setRequireExplicitFlush, supportsOption, suspendReceives, toString, writeExceptionHandler
-
-
-
-
Field Detail
-
WEB_SOCKETS_READ_TIMEOUT
public static final java.lang.String WEB_SOCKETS_READ_TIMEOUT
Configure a read timeout for a web socket, in milliseconds. If its present it will overrideorg.xnio.Options.READ_TIMEOUT. If the given amount of time elapses without a successful read taking place, the socket's next read will throw aReadTimeoutException.- See Also:
- Constant Field Values
-
WEB_SOCKETS_WRITE_TIMEOUT
public static final java.lang.String WEB_SOCKETS_WRITE_TIMEOUT
Configure a write timeout for a web socket, in milliseconds. If its present it will overrideorg.xnio.Options.WRITE_TIMEOUT. If the given amount of time elapses without a successful write taking place, the socket's next write will throw aWriteTimeoutException.- See Also:
- Constant Field Values
-
extensionsSupported
protected final boolean extensionsSupported
-
extensionFunction
protected final ExtensionFunction extensionFunction
-
hasReservedOpCode
protected final boolean hasReservedOpCode
-
fragmentedChannel
protected StreamSourceFrameChannel fragmentedChannel
-
-
Constructor Detail
-
WebSocketChannel
protected WebSocketChannel(org.xnio.StreamConnection connectedStreamChannel, ByteBufferPool bufferPool, WebSocketVersion version, java.lang.String wsUrl, java.lang.String subProtocol, boolean client, boolean extensionsSupported, ExtensionFunction extensionFunction, java.util.Set<WebSocketChannel> peerConnections, org.xnio.OptionMap options)Create a newWebSocketChannel8- Parameters:
connectedStreamChannel- TheConnectedStreamChannelover which the WebSocket Frames should get send and received. Be aware that it already must be "upgraded".bufferPool- ThePoolwhich will be used to acquireByteBuffer's from.version- TheWebSocketVersionof theWebSocketChannelwsUrl- The url for which the channel was created.client-peerConnections- The concurrent set that is used to track open connections associtated with an endpoint
-
-
Method Detail
-
getReceivers
protected java.util.Collection<AbstractFramedStreamSourceChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>> getReceivers()
- Specified by:
getReceiversin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
createIdleTimeoutChannel
protected IdleTimeoutConduit createIdleTimeoutChannel(org.xnio.StreamConnection connectedStreamChannel)
- Overrides:
createIdleTimeoutChannelin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
isLastFrameSent
protected boolean isLastFrameSent()
- Specified by:
isLastFrameSentin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>- Returns:
trueIf the last frame has been sent
-
isLastFrameReceived
protected boolean isLastFrameReceived()
Description copied from class:AbstractFramedChannelReturns true if the protocol specific final frame has been received.- Specified by:
isLastFrameReceivedin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>- Returns:
trueIf the last frame has been received
-
markReadsBroken
protected void markReadsBroken(java.lang.Throwable cause)
Description copied from class:AbstractFramedChannelCalled when a source sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.The underlying read side will be forcibly closed.
- Overrides:
markReadsBrokenin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>- Parameters:
cause- The possibly null cause
-
lastDataRead
protected void lastDataRead()
Description copied from class:AbstractFramedChannelMethod than is invoked when read() returns -1.- Overrides:
lastDataReadin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
isReadsBroken
protected boolean isReadsBroken()
- Overrides:
isReadsBrokenin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
parseFrame
protected FrameHeaderData parseFrame(java.nio.ByteBuffer data) throws java.io.IOException
Description copied from class:AbstractFramedChannelAttempts to parse an incoming frame header from the data in the buffer.- Specified by:
parseFramein classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>- Parameters:
data- The data that has been read from the channel- Returns:
- The frame header data, or
nullif the data was incomplete - Throws:
java.io.IOException- If the data could not be parsed.
-
receiveFrame
protected abstract WebSocketChannel.PartialFrame receiveFrame()
Create a newStreamSourceFrameChannelwhich can be used to read the data of the received Frame- Returns:
- channel A
StreamSourceFrameChannelwill be used to read a Frame from. This will returnnullif the rightStreamSourceFrameChannelcould not be detected with the given buffer and so more data is needed.
-
createChannel
protected StreamSourceFrameChannel createChannel(FrameHeaderData frameHeaderData, PooledByteBuffer frameData)
Description copied from class:AbstractFramedChannelMethod that creates the actual stream source channel implementation that is in use.- Specified by:
createChannelin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>- Parameters:
frameHeaderData- The header data, as returned byAbstractFramedChannel.parseFrame(java.nio.ByteBuffer)frameData- Any additional data for the frame that has already been read. This may not be the complete frame contents- Returns:
- A new stream source channel
-
setAttribute
public final boolean setAttribute(java.lang.String key, java.lang.Object value)
-
getAttribute
public final java.lang.Object getAttribute(java.lang.String key)
-
areExtensionsSupported
public boolean areExtensionsSupported()
Returnstrueif extensions are supported by this WebSocket Channel.
-
handleBrokenSourceChannel
protected void handleBrokenSourceChannel(java.lang.Throwable e)
Description copied from class:AbstractFramedChannelMethod that is invoked when the read side of the channel is broken. This generally happens on a protocol error.- Specified by:
handleBrokenSourceChannelin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
handleBrokenSinkChannel
protected void handleBrokenSinkChannel(java.lang.Throwable e)
Description copied from class:AbstractFramedChannelMethod that is invoked when then write side of a channel is broken. This generally happens on a protocol error.- Specified by:
handleBrokenSinkChannelin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
getSubProtocols
@Deprecated public java.util.Set<java.lang.String> getSubProtocols()
Deprecated.Returns an unmodifiableSetof the selected subprotocols if any.
-
getSubProtocol
public java.lang.String getSubProtocol()
-
isCloseFrameReceived
public boolean isCloseFrameReceived()
-
isCloseFrameSent
public boolean isCloseFrameSent()
-
getRequestScheme
public java.lang.String getRequestScheme()
Get the request URI scheme. Normally this is one ofwsorwss.- Returns:
- the request URI scheme
-
isSecure
public boolean isSecure()
Returntrueif this is handled via WebSocket Secure.
-
getUrl
public java.lang.String getUrl()
Return the URL of the WebSocket endpoint.- Returns:
- url The URL of the endpoint
-
getVersion
public WebSocketVersion getVersion()
Return theWebSocketVersionwhich is used- Returns:
- version The
WebSocketVersionwhich is in use
-
getSourceAddress
public java.net.InetSocketAddress getSourceAddress()
Get the source address of the WebSocket Channel.- Overrides:
getSourceAddressin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>- Returns:
- the source address of the WebSocket Channel
-
getDestinationAddress
public java.net.InetSocketAddress getDestinationAddress()
Get the destination address of the WebSocket Channel.- Overrides:
getDestinationAddressin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>- Returns:
- the destination address of the WebSocket Channel
-
isClient
public boolean isClient()
-
send
public final StreamSinkFrameChannel send(WebSocketFrameType type) throws java.io.IOException
Returns a newStreamSinkFrameChannelfor sending the givenWebSocketFrameTypewith the given payload. If this method is called multiple times, subsequentStreamSinkFrameChannel's will not be writable until all previous frames were completely written.- Parameters:
type- TheWebSocketFrameTypefor which aStreamSinkChannelshould be created- Throws:
java.io.IOException
-
sendClose
public void sendClose() throws java.io.IOExceptionSend a Close frame without a payload- Throws:
java.io.IOException
-
createStreamSinkChannel
protected abstract StreamSinkFrameChannel createStreamSinkChannel(WebSocketFrameType type)
Create a new StreamSinkFrameChannel which can be used to send a WebSocket Frame of the typeWebSocketFrameType.- Parameters:
type- TheWebSocketFrameTypeof the WebSocketFrame which will be send over thisStreamSinkFrameChannel
-
getFramePriority
protected WebSocketFramePriority getFramePriority()
- Overrides:
getFramePriorityin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
getPeerConnections
public java.util.Set<WebSocketChannel> getPeerConnections()
Returns all 'peer' web socket connections that were created from the same endpoint.- Returns:
- all 'peer' web socket connections
-
isCloseInitiatedByRemotePeer
public boolean isCloseInitiatedByRemotePeer()
If this is true the session is being closed because the remote peer sent a close frame- Returns:
trueif the remote peer closed the connection
-
getCloseReason
public java.lang.String getCloseReason()
- Returns:
- The close reason
-
setCloseReason
public void setCloseReason(java.lang.String closeReason)
-
getCloseCode
public int getCloseCode()
- Returns:
- The close code
-
setCloseCode
public void setCloseCode(int closeCode)
-
getExtensionFunction
public ExtensionFunction getExtensionFunction()
-
-