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>
AConnectedChannel
which 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 interface
WebSocketChannel.PartialFrame
Interface that represents a frame channel that is in the process of being created
-
Field Summary
Fields Modifier and Type Field Description protected ExtensionFunction
extensionFunction
protected boolean
extensionsSupported
protected StreamSourceFrameChannel
fragmentedChannel
protected boolean
hasReservedOpCode
static java.lang.String
WEB_SOCKETS_READ_TIMEOUT
Configure a read timeout for a web socket, in milliseconds.static java.lang.String
WEB_SOCKETS_WRITE_TIMEOUT
Configure a write timeout for a web socket, in milliseconds.
-
Constructor Summary
Constructors Modifier Constructor Description 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 newWebSocketChannel
8
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description boolean
areExtensionsSupported()
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
createStreamSinkChannel(WebSocketFrameType type)
Create a new StreamSinkFrameChannel which can be used to send a WebSocket Frame of the typeWebSocketFrameType
.java.lang.Object
getAttribute(java.lang.String key)
int
getCloseCode()
java.lang.String
getCloseReason()
java.net.InetSocketAddress
getDestinationAddress()
Get the destination address of the WebSocket Channel.ExtensionFunction
getExtensionFunction()
protected WebSocketFramePriority
getFramePriority()
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.String
getRequestScheme()
Get the request URI scheme.java.net.InetSocketAddress
getSourceAddress()
Get the source address of the WebSocket Channel.java.lang.String
getSubProtocol()
java.util.Set<java.lang.String>
getSubProtocols()
Deprecated.java.lang.String
getUrl()
Return the URL of the WebSocket endpoint.WebSocketVersion
getVersion()
Return theWebSocketVersion
which is usedprotected void
handleBrokenSinkChannel(java.lang.Throwable e)
Method that is invoked when then write side of a channel is broken.protected void
handleBrokenSourceChannel(java.lang.Throwable e)
Method that is invoked when the read side of the channel is broken.boolean
isClient()
boolean
isCloseFrameReceived()
boolean
isCloseFrameSent()
boolean
isCloseInitiatedByRemotePeer()
If this is true the session is being closed because the remote peer sent a close frameprotected boolean
isLastFrameReceived()
Returns true if the protocol specific final frame has been received.protected boolean
isLastFrameSent()
protected boolean
isReadsBroken()
boolean
isSecure()
Returntrue
if this is handled via WebSocket Secure.protected void
lastDataRead()
Method than is invoked when read() returns -1.protected void
markReadsBroken(java.lang.Throwable cause)
Called when a source sub channel fails to fulfil its contract, and leaves the channel in an inconsistent state.protected FrameHeaderData
parseFrame(java.nio.ByteBuffer data)
Attempts to parse an incoming frame header from the data in the buffer.protected abstract WebSocketChannel.PartialFrame
receiveFrame()
Create a newStreamSourceFrameChannel
which can be used to read the data of the received FrameStreamSinkFrameChannel
send(WebSocketFrameType type)
Returns a newStreamSinkFrameChannel
for sending the givenWebSocketFrameType
with the given payload.void
sendClose()
Send a Close frame without a payloadboolean
setAttribute(java.lang.String key, java.lang.Object value)
void
setCloseCode(int closeCode)
void
setCloseReason(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 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 Detail
-
getReceivers
protected java.util.Collection<AbstractFramedStreamSourceChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>> getReceivers()
- Specified by:
getReceivers
in classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
createIdleTimeoutChannel
protected IdleTimeoutConduit createIdleTimeoutChannel(org.xnio.StreamConnection connectedStreamChannel)
- Overrides:
createIdleTimeoutChannel
in classAbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>
-
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
protected void markReadsBroken(java.lang.Throwable cause)
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
protected FrameHeaderData parseFrame(java.nio.ByteBuffer data) throws java.io.IOException
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:
java.io.IOException
- If the data could not be parsed.
-
receiveFrame
protected abstract WebSocketChannel.PartialFrame 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
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()
Returnstrue
if extensions are supported by this WebSocket Channel.
-
handleBrokenSourceChannel
protected void handleBrokenSourceChannel(java.lang.Throwable e)
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
protected void handleBrokenSinkChannel(java.lang.Throwable e)
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 public java.util.Set<java.lang.String> getSubProtocols()
Deprecated.Returns an unmodifiableSet
of 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 ofws
orwss
.- Returns:
- the request URI scheme
-
isSecure
public boolean isSecure()
Returntrue
if 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 theWebSocketVersion
which is used- Returns:
- version The
WebSocketVersion
which is in use
-
getSourceAddress
public java.net.InetSocketAddress 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
public java.net.InetSocketAddress 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
public final StreamSinkFrameChannel send(WebSocketFrameType type) throws java.io.IOException
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:
java.io.IOException
-
sendClose
public void sendClose() throws java.io.IOException
Send 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
- TheWebSocketFrameType
of the WebSocketFrame which will be send over thisStreamSinkFrameChannel
-
getFramePriority
protected WebSocketFramePriority getFramePriority()
- Overrides:
getFramePriority
in 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:
true
if 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()
-
-