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 Classes
`static interface` `WebSocketChannel.PartialFrame` Interface that represents a frame channel that is in the process of being created

### Field Summary

Fields
`protected ExtensionFunction` `extensionFunction`
`protected boolean` `extensionsSupported`
`protected StreamSourceFrameChannel` `fragmentedChannel`
`protected boolean` `hasReservedOpCode`

### Fields inherited from interface org.xnio.channels.Configurable

`EMPTY`

### Constructor Summary

Constructors
`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 new `WebSocketChannel` 8

### Method Summary

All Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods
`boolean` `areExtensionsSupported()` Returns `true` 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 type `WebSocketFrameType`.
`Object` `getAttribute(String key)`
`int` `getCloseCode()`
`String` `getCloseReason()`
`InetSocketAddress` `getDestinationAddress()` Get the destination address of the WebSocket Channel.
`ExtensionFunction` `getExtensionFunction()`
`protected WebSocketFramePriority` `getFramePriority()`
`Set<WebSocketChannel>` `getPeerConnections()` Returns all 'peer' web socket connections that were created from the same endpoint.
`protected Collection<AbstractFramedStreamSourceChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>>` `getReceivers()`
`String` `getRequestScheme()` Get the request URI scheme.
`InetSocketAddress` `getSourceAddress()` Get the source address of the WebSocket Channel.
`String` `getSubProtocol()`
`Set<String>` `getSubProtocols()` Deprecated.
`String` `getUrl()` Return the URL of the WebSocket endpoint.
`WebSocketVersion` `getVersion()` Return the `WebSocketVersion` which is used
`protected void` `handleBrokenSinkChannel(Throwable e)` Method that is invoked when then write side of a channel is broken.
`protected void` `handleBrokenSourceChannel(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 frame
`protected boolean` `isLastFrameReceived()` Returns true if the protocol specific final frame has been received.
`protected boolean` `isLastFrameSent()`
`protected boolean` `isReadsBroken()`
`boolean` `isSecure()` Return `true` if this is handled via WebSocket Secure.
`protected void` `lastDataRead()` 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` `receiveFrame()` Create a new `StreamSourceFrameChannel` which can be used to read the data of the received Frame
`StreamSinkFrameChannel` `send(WebSocketFrameType type)` Returns a new `StreamSinkFrameChannel` for sending the given `WebSocketFrameType` with the given payload.
`void` `sendClose()` Send a Close frame without a payload
`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`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

### Field Detail

#### 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,
                               String wsUrl,
                               String subProtocol,
                               boolean client,
                               boolean extensionsSupported,
                               ExtensionFunction extensionFunction,
                               Set<WebSocketChannel> peerConnections,
                               org.xnio.OptionMap options)

Create a new `WebSocketChannel` 8

Parameters:
  `connectedStreamChannel` - The `ConnectedStreamChannel` over which the WebSocket Frames should get send and received. Be aware that it already must be "upgraded".
  `bufferPool` - The `Pool` which will be used to acquire `ByteBuffer`'s from.
  `version` - The `WebSocketVersion` of the `WebSocketChannel`
  `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 Collection<AbstractFramedStreamSourceChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>> getReceivers()

Specified by:
  `getReceivers` in class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`

#### createIdleTimeoutChannel

    protected IdleTimeoutConduit createIdleTimeoutChannel(org.xnio.StreamConnection connectedStreamChannel)

Overrides:
  `createIdleTimeoutChannel` in class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`

#### isLastFrameSent

    protected boolean isLastFrameSent()

Specified by:
  `isLastFrameSent` in class `AbstractFramedChannel<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 class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`
Returns:
  `true` If the last frame has been received

#### markReadsBroken

    protected void markReadsBroken(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 class `AbstractFramedChannel<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 class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`

#### isReadsBroken

    protected boolean isReadsBroken()

Overrides:
  `isReadsBroken` in class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`

#### parseFrame

    protected FrameHeaderData parseFrame(ByteBuffer data)
                                  throws IOException

Description copied from class: `AbstractFramedChannel`
Attempts to parse an incoming frame header from the data in the buffer.

Specified by:
  `parseFrame` in class `AbstractFramedChannel<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

    protected abstract WebSocketChannel.PartialFrame receiveFrame()

Create a new `StreamSourceFrameChannel` 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 return `null` if the right `StreamSourceFrameChannel` 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 class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`
Parameters:
  `frameHeaderData` - The header data, as returned by `AbstractFramedChannel.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(String key,
                                      Object value)

#### getAttribute

    public final Object getAttribute(String key)

#### areExtensionsSupported

    public boolean areExtensionsSupported()

Returns `true` if extensions are supported by this WebSocket Channel.

#### handleBrokenSourceChannel

    protected void handleBrokenSourceChannel(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 class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`

#### handleBrokenSinkChannel

    protected void handleBrokenSinkChannel(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 class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`

#### getSubProtocols

    @Deprecated
    public Set<String> getSubProtocols()

Deprecated.
Returns an unmodifiable `Set` of the selected subprotocols if any.

#### getSubProtocol

    public String getSubProtocol()

#### isCloseFrameReceived

    public boolean isCloseFrameReceived()

#### isCloseFrameSent

    public boolean isCloseFrameSent()

#### getRequestScheme

    public String getRequestScheme()

Get the request URI scheme. Normally this is one of `ws` or `wss`.

Returns:
  the request URI scheme

#### isSecure

    public boolean isSecure()

Return `true` if this is handled via WebSocket Secure.

#### getUrl

    public String getUrl()

Return the URL of the WebSocket endpoint.

Returns:
  url The URL of the endpoint

#### getVersion

    public WebSocketVersion getVersion()

Return the `WebSocketVersion` which is used

Returns:
  version The `WebSocketVersion` which is in use

#### getSourceAddress

    public InetSocketAddress getSourceAddress()

Get the source address of the WebSocket Channel.

Overrides:
  `getSourceAddress` in class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`
Returns:
  the source address of the WebSocket Channel

#### getDestinationAddress

    public InetSocketAddress getDestinationAddress()

Get the destination address of the WebSocket Channel.

Overrides:
  `getDestinationAddress` in class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`
Returns:
  the destination address of the WebSocket Channel

#### isClient

    public boolean isClient()

#### send

    public final StreamSinkFrameChannel send(WebSocketFrameType type)
                                      throws IOException

Returns a new `StreamSinkFrameChannel` for sending the given `WebSocketFrameType` with the given payload. If this method is called multiple times, subsequent `StreamSinkFrameChannel`'s will not be writable until all previous frames were completely written.

Parameters:
  `type` - The `WebSocketFrameType` for which a `StreamSinkChannel` should be created
Throws:
  `IOException`

#### sendClose

    public void sendClose()
                   throws IOException

Send a Close frame without a payload

Throws:
  `IOException`

#### createStreamSinkChannel

    protected abstract StreamSinkFrameChannel createStreamSinkChannel(WebSocketFrameType type)

Create a new StreamSinkFrameChannel which can be used to send a WebSocket Frame of the type `WebSocketFrameType`.

Parameters:
  `type` - The `WebSocketFrameType` of the WebSocketFrame which will be send over this `StreamSinkFrameChannel`

#### getFramePriority

    protected WebSocketFramePriority getFramePriority()

Overrides:
  `getFramePriority` in class `AbstractFramedChannel<WebSocketChannel,StreamSourceFrameChannel,StreamSinkFrameChannel>`

#### getPeerConnections

    public 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 String getCloseReason()

Returns:
  The close reason

#### setCloseReason

    public void setCloseReason(String closeReason)

#### getCloseCode

    public int getCloseCode()

Returns:
  The close code

#### setCloseCode

    public void setCloseCode(int closeCode)

#### getExtensionFunction

    public ExtensionFunction getExtensionFunction()