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 interface
Interface that represents a frame channel that is in the process of being created -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ExtensionFunction
protected final boolean
protected StreamSourceFrameChannel
protected final boolean
Fields inherited from interface org.xnio.channels.Configurable
EMPTY
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
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 newWebSocketChannel
8 -
Method Summary
Modifier and TypeMethodDescriptionboolean
Returnstrue
if extensions are supported by this WebSocket Channel.protected StreamSourceFrameChannel
createChannel
(FrameHeaderData frameHeaderData, PooledByteBuffer frameData) Method that creates the actual stream source channel implementation that is in use.protected IdleTimeoutConduit
createIdleTimeoutChannel
(org.xnio.StreamConnection connectedStreamChannel) protected abstract StreamSinkFrameChannel
Create a new StreamSinkFrameChannel which can be used to send a WebSocket Frame of the typeWebSocketFrameType
.final Object
getAttribute
(String key) int
Get the destination address of the WebSocket Channel.protected WebSocketFramePriority
Returns 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 theWebSocketVersion
which is usedprotected void
Method that is invoked when then write side of a channel is broken.protected void
Method that is invoked when the read side of the channel is broken.boolean
isClient()
boolean
boolean
boolean
If this is true the session is being closed because the remote peer sent a close frameprotected boolean
Returns true if the protocol specific final frame has been received.protected boolean
protected boolean
boolean
isSecure()
Returntrue
if this is handled via WebSocket Secure.protected void
Method than is invoked when read() returns -1.protected void
markReadsBroken
(Throwable cause) Called when a source sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.protected FrameHeaderData
parseFrame
(ByteBuffer data) Attempts to parse an incoming frame header from the data in the buffer.protected abstract WebSocketChannel.PartialFrame
Create a newStreamSourceFrameChannel
which can be used to read the data of the received Framefinal StreamSinkFrameChannel
send
(WebSocketFrameType type) Returns a newStreamSinkFrameChannel
for sending the givenWebSocketFrameType
with the given payload.void
Send a Close frame without a payloadfinal boolean
setAttribute
(String key, Object value) void
setCloseCode
(int closeCode) void
setCloseReason
(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 newWebSocketChannel
8- Parameters:
connectedStreamChannel
- TheConnectedStreamChannel
over which the WebSocket Frames should get send and received. Be aware that it already must be "upgraded".bufferPool
- ThePool
which will be used to acquireByteBuffer
's from.version
- TheWebSocketVersion
of theWebSocketChannel
wsUrl
- 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:
getReceivers
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel>
-
createIdleTimeoutChannel
protected IdleTimeoutConduit createIdleTimeoutChannel(org.xnio.StreamConnection connectedStreamChannel) -
isLastFrameSent
protected boolean isLastFrameSent()- Specified by:
isLastFrameSent
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel> - Returns:
true
If the last frame has been sent
-
isLastFrameReceived
protected boolean isLastFrameReceived()Description copied from class:AbstractFramedChannel
Returns true if the protocol specific final frame has been received.- Specified by:
isLastFrameReceived
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel> - Returns:
true
If the last frame has been received
-
markReadsBroken
Description copied from class:AbstractFramedChannel
Called 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:
markReadsBroken
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel> - Parameters:
cause
- The possibly null cause
-
lastDataRead
protected void lastDataRead()Description copied from class:AbstractFramedChannel
Method than is invoked when read() returns -1.- Overrides:
lastDataRead
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel>
-
isReadsBroken
protected boolean isReadsBroken()- Overrides:
isReadsBroken
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel>
-
parseFrame
Description copied from class:AbstractFramedChannel
Attempts to parse an incoming frame header from the data in the buffer.- Specified by:
parseFrame
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel> - Parameters:
data
- The data that has been read from the channel- Returns:
- The frame header data, or
null
if the data was incomplete - Throws:
IOException
- If the data could not be parsed.
-
receiveFrame
Create a newStreamSourceFrameChannel
which can be used to read the data of the received Frame- Returns:
- channel A
StreamSourceFrameChannel
will be used to read a Frame from. This will returnnull
if the rightStreamSourceFrameChannel
could 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:AbstractFramedChannel
Method that creates the actual stream source channel implementation that is in use.- Specified by:
createChannel
in 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()Returnstrue
if extensions are supported by this WebSocket Channel. -
handleBrokenSourceChannel
Description copied from class:AbstractFramedChannel
Method that is invoked when the read side of the channel is broken. This generally happens on a protocol error.- Specified by:
handleBrokenSourceChannel
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel>
-
handleBrokenSinkChannel
Description copied from class:AbstractFramedChannel
Method that is invoked when then write side of a channel is broken. This generally happens on a protocol error.- Specified by:
handleBrokenSinkChannel
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel>
-
getSubProtocols
Deprecated.Returns an unmodifiableSet
of the selected subprotocols if any. -
getSubProtocol
-
isCloseFrameReceived
public boolean isCloseFrameReceived() -
isCloseFrameSent
public boolean isCloseFrameSent() -
getRequestScheme
Get the request URI scheme. Normally this is one ofws
orwss
.- Returns:
- the request URI scheme
-
isSecure
public boolean isSecure()Returntrue
if this is handled via WebSocket Secure. -
getUrl
Return the URL of the WebSocket endpoint.- Returns:
- url The URL of the endpoint
-
getVersion
Return theWebSocketVersion
which is used- Returns:
- version The
WebSocketVersion
which is in use
-
getSourceAddress
Get the source address of the WebSocket Channel.- Overrides:
getSourceAddress
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel> - Returns:
- the source address of the WebSocket Channel
-
getDestinationAddress
Get the destination address of the WebSocket Channel.- Overrides:
getDestinationAddress
in classAbstractFramedChannel<WebSocketChannel,
StreamSourceFrameChannel, StreamSinkFrameChannel> - Returns:
- the destination address of the WebSocket Channel
-
isClient
public boolean isClient() -
send
Returns a newStreamSinkFrameChannel
for sending the givenWebSocketFrameType
with 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
- TheWebSocketFrameType
for which aStreamSinkChannel
should 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
- TheWebSocketFrameType
of the WebSocketFrame which will be send over thisStreamSinkFrameChannel
-
getFramePriority
- Overrides:
getFramePriority
in 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:
true
if 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
-