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:
Closeable,AutoCloseable,Channel,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>
A
ConnectedChannel which can be used to send and receive WebSocket Frames.- Author:
- Norman Maurer, Stuart Douglas
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface that represents a frame channel that is in the process of being created -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ExtensionFunctionprotected final booleanprotected StreamSourceFrameChannelprotected final booleanFields inherited from interface org.xnio.channels.Configurable
EMPTY -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedWebSocketChannel(org.xnio.StreamConnection connectedStreamChannel, ByteBufferPool bufferPool, WebSocketVersion version, String wsUrl, String subProtocol, boolean client, boolean extensionsSupported, ExtensionFunction extensionFunction, Set<WebSocketChannel> peerConnections, org.xnio.OptionMap options) Create a newWebSocketChannel8 -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturnstrueif 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 StreamSinkFrameChannelCreate a new StreamSinkFrameChannel which can be used to send a WebSocket Frame of the typeWebSocketFrameType.final ObjectgetAttribute(String key) intGet the destination address of the WebSocket Channel.protected WebSocketFramePriorityReturns all 'peer' web socket connections that were created from the same endpoint.protected Collection<AbstractFramedStreamSourceChannel<WebSocketChannel,StreamSourceFrameChannel, StreamSinkFrameChannel>> Get the request URI scheme.Get the source address of the WebSocket Channel.Deprecated.getUrl()Return the URL of the WebSocket endpoint.Return theWebSocketVersionwhich is usedprotected voidMethod that is invoked when then write side of a channel is broken.protected voidMethod that is invoked when the read side of the channel is broken.booleanisClient()booleanbooleanbooleanIf this is true the session is being closed because the remote peer sent a close frameprotected booleanReturns true if the protocol specific final frame has been received.protected booleanprotected booleanbooleanisSecure()Returntrueif this is handled via WebSocket Secure.protected voidMethod than is invoked when read() returns -1.protected voidmarkReadsBroken(Throwable cause) Called when a source sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.protected FrameHeaderDataparseFrame(ByteBuffer data) Attempts to parse an incoming frame header from the data in the buffer.protected abstract WebSocketChannel.PartialFrameCreate a newStreamSourceFrameChannelwhich can be used to read the data of the received Framefinal StreamSinkFrameChannelsend(WebSocketFrameType type) Returns a newStreamSinkFrameChannelfor sending the givenWebSocketFrameTypewith the given payload.voidSend a Close frame without a payloadfinal booleansetAttribute(String key, Object value) voidsetCloseCode(int closeCode) voidsetCloseReason(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 Details
-
extensionsSupported
protected final boolean extensionsSupported -
extensionFunction
-
hasReservedOpCode
protected final boolean hasReservedOpCode -
fragmentedChannel
-
-
Constructor Details
-
WebSocketChannel
protected WebSocketChannel(org.xnio.StreamConnection connectedStreamChannel, ByteBufferPool bufferPool, WebSocketVersion version, String wsUrl, String subProtocol, boolean client, boolean extensionsSupported, ExtensionFunction extensionFunction, 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 Details
-
getReceivers
protected Collection<AbstractFramedStreamSourceChannel<WebSocketChannel,StreamSourceFrameChannel, getReceivers()StreamSinkFrameChannel>> - Specified by:
getReceiversin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel, StreamSinkFrameChannel>
-
createIdleTimeoutChannel
protected IdleTimeoutConduit createIdleTimeoutChannel(org.xnio.StreamConnection connectedStreamChannel) -
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
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
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:
IOException- If the data could not be parsed.
-
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
-
getAttribute
-
areExtensionsSupported
public boolean areExtensionsSupported()Returnstrueif extensions are supported by this WebSocket Channel. -
handleBrokenSourceChannel
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
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.Returns an unmodifiableSetof the selected subprotocols if any. -
getSubProtocol
-
isCloseFrameReceived
public boolean isCloseFrameReceived() -
isCloseFrameSent
public boolean isCloseFrameSent() -
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
Return the URL of the WebSocket endpoint.- Returns:
- url The URL of the endpoint
-
getVersion
Return theWebSocketVersionwhich is used- Returns:
- version The
WebSocketVersionwhich is in use
-
getSourceAddress
Get the source address of the WebSocket Channel.- Overrides:
getSourceAddressin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel, StreamSinkFrameChannel> - Returns:
- the source address of the WebSocket Channel
-
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
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:
IOException
-
sendClose
Send a Close frame without a payload- Throws:
IOException
-
createStreamSinkChannel
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
- Overrides:
getFramePriorityin classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel, StreamSinkFrameChannel>
-
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
- Returns:
- The close reason
-
setCloseReason
-
getCloseCode
public int getCloseCode()- Returns:
- The close code
-
setCloseCode
public void setCloseCode(int closeCode) -
getExtensionFunction
-