Top Description Fields Constructors Methods
org.java_websocket

public Class WebSocketImpl

extends Object
implements WebSocket
Class Inheritance
All Implemented Interfaces
org.java_websocket.WebSocket
Imports
java.io.IOException, java.net.InetSocketAddress, java.nio.ByteBuffer, java.nio.channels.ByteChannel, .SelectionKey, java.util.ArrayList, .Collection, .Collections, .List, java.util.concurrent.BlockingQueue, .LinkedBlockingQueue, javax.net.ssl.SSLSession, org.java_websocket.drafts.Draft, .Draft_6455, org.java_websocket.enums.CloseHandshakeType, .HandshakeState, .Opcode, .ReadyState, .Role, org.java_websocket.exceptions.IncompleteHandshakeException, .InvalidDataException, .InvalidHandshakeException, .LimitExceededException, .WebsocketNotConnectedException, org.java_websocket.framing.CloseFrame, .Framedata, .PingFrame, org.java_websocket.handshake.ClientHandshake, .ClientHandshakeBuilder, .Handshakedata, .ServerHandshake, .ServerHandshakeBuilder, org.java_websocket.interfaces.ISSLChannel, org.java_websocket.protocols.IProtocol, org.java_websocket.server.WebSocketServer.WebSocketWorker, org.java_websocket.util.Charsetfunctions, org.slf4j.Logger, .LoggerFactory

Represents one end (client or server) of a single WebSocketImpl connection. Takes care of the "handshake" phase, then allows for easy sending of text frames, and receiving frames through an event-based model.

Field Summary

Modifier and TypeField and Description
private Object
attachment

Attribute to store connection attachment

private ByteChannel
channel

the possibly wrapped channel object whose selection is controlled by key

private Integer
private Boolean
private String
public static final int
DEFAULT_PORT

The default port of WebSockets, as defined in the spec.

public static final int
DEFAULT_WSS_PORT

The default wss port of WebSockets, as defined in the spec.

private Draft
draft

The draft which is used by this websocket

private boolean
flushandclosestate

When true no further frames may be submitted to be sent

private ClientHandshake
handshakerequest

stores the handshake sent by this websocket ( Role.CLIENT only )

public final BlockingQueue<ByteBuffer>
inQueue

Queue of buffers that need to be processed

private SelectionKey
private List<Draft>
knownDrafts

A list of drafts available for this websocket

private long
lastPong

Attribute, when the last pong was received

private final Logger
log

Logger instance

public final BlockingQueue<ByteBuffer>
outQueue

Queue of buffers that need to be sent to the client.

private volatile ReadyState
readyState

The current state of the connection

private String
private Role
role

The role which this websocket takes in the connection

private final Object
synchronizeWriteObject

Attribut to synchronize the write

private ByteBuffer
tmpHandshakeBytes

the bytes of an incomplete received handshake

private WebSocketServer.WebSocketWorker
workerThread

Helper variable meant to store the thread which ( exclusively ) triggers this objects decode method.

private final WebSocketListener
wsl

The listener to notify of WebSocket events.

Constructor Summary

AccessConstructor and Description
public
WebSocketImpl(WebSocketListener
The listener for this instance
listener
,
List<Draft>
The drafts which should be used
drafts
)

Creates a websocket with server role

public
WebSocketImpl(WebSocketListener
The listener for this instance
listener
,
Draft
The draft which should be used
draft
)

creates a websocket with client role

Method Summary

Modifier and TypeMethod and Description
public synchronized void
close(int code, String message, boolean remote)

public void
close(int code, String message)

Implements org.java_websocket.WebSocket.close.

sends the closing handshake.

public void
close(int code)

Implements org.java_websocket.WebSocket.close.

sends the closing handshake.

public void
public void
close()

Implements org.java_websocket.WebSocket.close.

Convenience function which behaves like close(CloseFrame.NORMAL)

public synchronized void
closeConnection(int
the closing code
code
,
String
the closing message
message
,
boolean
Indicates who "generated" code.
true means that this endpoint received the code from the other endpoint.
false means this endpoint decided to send the given code,
remote may also be true if this endpoint started the closing handshake since the other endpoint may not simply echo the code but close the connection the same time this endpoint does do but with an other code.
remote
)

This will close the connection immediately without a proper close handshake.

protected void
closeConnection(int code, boolean remote)

public void
public void
closeConnection(int code, String message)

Implements org.java_websocket.WebSocket.closeConnection.

This will close the connection immediately without a proper close handshake.

private void
closeConnectionDueToInternalServerError(RuntimeException
the RuntimeException causing this problem
exception
)

Close the connection if there was a server error by a RuntimeException

private void
closeConnectionDueToWrongHandshake(InvalidDataException
the InvalidDataException causing this problem
exception
)

Close the connection if the received handshake was not correct

public void
decode(ByteBuffer
the ByteBuffer to decode
socketBuffer
)

Method to decode the provided ByteBuffer

private void
decodeFrames(ByteBuffer socketBuffer)

private boolean
decodeHandshake(ByteBuffer socketBufferNew)

Returns whether the handshake phase has is completed.

public void
eot()

public synchronized void
flushAndClose(int code, String message, boolean remote)

private ByteBuffer

Returns:

the complete response as ByteBuffer
generateHttpResponseDueToError
(int
the http error code
errorCode
)

Generate a simple response for the corresponding endpoint to indicate some error

public <T> T
getAttachment()

Implements org.java_websocket.WebSocket.getAttachment.

Getter for the connection attachment.

public ByteChannel
public Draft
getDraft()

Implements org.java_websocket.WebSocket.getDraft.

Getter for the draft

pack-priv long

Returns:

the timestamp for the last received pong
getLastPong
()

Getter for the last pong received

public InetSocketAddress
getLocalSocketAddress()

Implements org.java_websocket.WebSocket.getLocalSocketAddress.

Returns the address of the endpoint this socket is bound to, or null if it is not bound.

public IProtocol
getProtocol()

Implements org.java_websocket.WebSocket.getProtocol.

Returns the used Sec-WebSocket-Protocol for this websocket connection

public ReadyState
getReadyState()

Implements org.java_websocket.WebSocket.getReadyState.

Retrieve the WebSocket 'ReadyState'.

public InetSocketAddress
getRemoteSocketAddress()

Implements org.java_websocket.WebSocket.getRemoteSocketAddress.

Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

public String
getResourceDescriptor()

Implements org.java_websocket.WebSocket.getResourceDescriptor.

Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1.2 If the opening handshake has not yet happened it will return null.

public SelectionKey

Returns:

the selection key of this implementation
getSelectionKey
()

public SSLSession
getSSLSession()

Implements org.java_websocket.WebSocket.getSSLSession.

Returns the ssl session of websocket, if ssl/wss is used for this instance.

public WebSocketListener

Returns:

the websocket listener associated with this instance
getWebSocketListener
()

Getter for the websocket listener

public WebSocketServer.WebSocketWorker
public boolean
hasBufferedData()

Implements org.java_websocket.WebSocket.hasBufferedData.

Checks if the websocket has buffered data

public boolean
hasSSLSupport()

Implements org.java_websocket.WebSocket.hasSSLSupport.

Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection

public boolean
isClosed()

Implements org.java_websocket.WebSocket.isClosed.

Is the websocket in the state CLOSED

public boolean
isClosing()

Implements org.java_websocket.WebSocket.isClosing.

Is the websocket in the state CLOSING

public boolean
isFlushAndClose()

Implements org.java_websocket.WebSocket.isFlushAndClose.

Returns true when no further frames may be submitted This happens before the socket connection is closed.

public boolean
isOpen()

Implements org.java_websocket.WebSocket.isOpen.

Is the websocket in the state OPEN

private void
public void
send(String text)

Implements org.java_websocket.WebSocket.send.

Send Text data to the other end.

public void
send(ByteBuffer bytes)

Implements org.java_websocket.WebSocket.send.

Send Binary data (plain bytes) to the other end.

public void
send(byte[] bytes)

Implements org.java_websocket.WebSocket.send.

Send Binary data (plain bytes) to the other end.

private void
public void
sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)

Implements org.java_websocket.WebSocket.sendFragmentedFrame.

Allows to send continuous/fragmented frames conveniently.

public void
sendFrame(Collection<Framedata> frames)

Implements org.java_websocket.WebSocket.sendFrame.

Send a collection of frames to the other end

public void
sendFrame(Framedata framedata)

Implements org.java_websocket.WebSocket.sendFrame.

Send a frame to the other end

public void
sendPing()

Implements org.java_websocket.WebSocket.sendPing.

Send a ping to the other end

public <T> void
setAttachment(T attachment)

Implements org.java_websocket.WebSocket.setAttachment.

Setter for an attachment on the socket connection.

public void
public void
setSelectionKey(SelectionKey
the selection key of this implementation
key
)

public void
public void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public void
updateLastPong()

Update the timestamp when the last pong was received

private void
private void
write(List<ByteBuffer>
the list of bytebuffer
bufs
)

Write a list of bytebuffer (frames in binary form) into the outgoing queue

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

attachmentback to summary
private Object attachment

Attribute to store connection attachment

Since
1.3.7
channelback to summary
private ByteChannel channel

the possibly wrapped channel object whose selection is controlled by key

closecodeback to summary
private Integer closecode
closedremotelyback to summary
private Boolean closedremotely
closemessageback to summary
private String closemessage
DEFAULT_PORTback to summary
public static final int DEFAULT_PORT

The default port of WebSockets, as defined in the spec. If the nullary constructor is used, DEFAULT_PORT will be the port the WebSocketServer is binded to. Note that ports under 1024 usually require root permissions.

DEFAULT_WSS_PORTback to summary
public static final int DEFAULT_WSS_PORT

The default wss port of WebSockets, as defined in the spec. If the nullary constructor is used, DEFAULT_WSS_PORT will be the port the WebSocketServer is binded to. Note that ports under 1024 usually require root permissions.

draftback to summary
private Draft draft

The draft which is used by this websocket

flushandclosestateback to summary
private boolean flushandclosestate

When true no further frames may be submitted to be sent

handshakerequestback to summary
private ClientHandshake handshakerequest

stores the handshake sent by this websocket ( Role.CLIENT only )

inQueueback to summary
public final BlockingQueue<ByteBuffer> inQueue

Queue of buffers that need to be processed

keyback to summary
private SelectionKey key
knownDraftsback to summary
private List<Draft> knownDrafts

A list of drafts available for this websocket

lastPongback to summary
private long lastPong

Attribute, when the last pong was received

logback to summary
private final Logger log

Logger instance

Since
1.4.0
outQueueback to summary
public final BlockingQueue<ByteBuffer> outQueue

Queue of buffers that need to be sent to the client.

readyStateback to summary
private volatile ReadyState readyState

The current state of the connection

resourceDescriptorback to summary
private String resourceDescriptor
roleback to summary
private Role role

The role which this websocket takes in the connection

synchronizeWriteObjectback to summary
private final Object synchronizeWriteObject

Attribut to synchronize the write

tmpHandshakeBytesback to summary
private ByteBuffer tmpHandshakeBytes

the bytes of an incomplete received handshake

workerThreadback to summary
private WebSocketServer.WebSocketWorker workerThread

Helper variable meant to store the thread which ( exclusively ) triggers this objects decode method.

wslback to summary
private final WebSocketListener wsl

The listener to notify of WebSocket events.

Constructor Detail

WebSocketImplback to summary
public WebSocketImpl(WebSocketListener listener, List<Draft> drafts)

Creates a websocket with server role

Parameters
listener:WebSocketListener

The listener for this instance

drafts:List<Draft>

The drafts which should be used

WebSocketImplback to summary
public WebSocketImpl(WebSocketListener listener, Draft draft)

creates a websocket with client role

Parameters
listener:WebSocketListener

The listener for this instance

draft:Draft

The draft which should be used

Method Detail

closeback to summary
public synchronized void close(int code, String message, boolean remote)
closeback to summary
public void close(int code, String message)

Implements org.java_websocket.WebSocket.close.

Doc from org.java_websocket.WebSocket.close.

sends the closing handshake. may be send in response to an other handshake.

Annotations
@Override
closeback to summary
public void close(int code)

Implements org.java_websocket.WebSocket.close.

Doc from org.java_websocket.WebSocket.close.

sends the closing handshake. may be send in response to an other handshake.

Annotations
@Override
closeback to summary
public void close(InvalidDataException e)
closeback to summary
public void close()

Implements org.java_websocket.WebSocket.close.

Doc from org.java_websocket.WebSocket.close.

Convenience function which behaves like close(CloseFrame.NORMAL)

Annotations
@Override
closeConnectionback to summary
public synchronized void closeConnection(int code, String message, boolean remote)

This will close the connection immediately without a proper close handshake. The code and the message therefore won't be transferred over the wire also they will be forwarded to onClose/onWebsocketClose.

Parameters
code:int

the closing code

message:String

the closing message

remote:boolean

Indicates who "generated" code.
true means that this endpoint received the code from the other endpoint.
false means this endpoint decided to send the given code,
remote may also be true if this endpoint started the closing handshake since the other endpoint may not simply echo the code but close the connection the same time this endpoint does do but with an other code.

closeConnectionback to summary
protected void closeConnection(int code, boolean remote)
closeConnectionback to summary
public void closeConnection()
closeConnectionback to summary
public void closeConnection(int code, String message)

Implements org.java_websocket.WebSocket.closeConnection.

Doc from org.java_websocket.WebSocket.closeConnection.

This will close the connection immediately without a proper close handshake. The code and the message therefore won't be transferred over the wire also they will be forwarded to onClose/onWebsocketClose.

closeConnectionDueToInternalServerErrorback to summary
private void closeConnectionDueToInternalServerError(RuntimeException exception)

Close the connection if there was a server error by a RuntimeException

Parameters
exception:RuntimeException

the RuntimeException causing this problem

closeConnectionDueToWrongHandshakeback to summary
private void closeConnectionDueToWrongHandshake(InvalidDataException exception)

Close the connection if the received handshake was not correct

Parameters
exception:InvalidDataException

the InvalidDataException causing this problem

decodeback to summary
public void decode(ByteBuffer socketBuffer)

Method to decode the provided ByteBuffer

Parameters
socketBuffer:ByteBuffer

the ByteBuffer to decode

decodeFramesback to summary
private void decodeFrames(ByteBuffer socketBuffer)
decodeHandshakeback to summary
private boolean decodeHandshake(ByteBuffer socketBufferNew)

Returns whether the handshake phase has is completed. In case of a broken handshake this will be never the case.

eotback to summary
public void eot()
flushAndCloseback to summary
public synchronized void flushAndClose(int code, String message, boolean remote)
generateHttpResponseDueToErrorback to summary
private ByteBuffer generateHttpResponseDueToError(int errorCode)

Generate a simple response for the corresponding endpoint to indicate some error

Parameters
errorCode:int

the http error code

Returns:ByteBuffer

the complete response as ByteBuffer

getAttachmentback to summary
public <T> T getAttachment()

Implements org.java_websocket.WebSocket.getAttachment.

Doc from org.java_websocket.WebSocket.getAttachment.

Getter for the connection attachment.

Annotations
@Override
@SuppressWarnings:unchecked
getChannelback to summary
public ByteChannel getChannel()
getDraftback to summary
public Draft getDraft()

Implements org.java_websocket.WebSocket.getDraft.

Doc from org.java_websocket.WebSocket.getDraft.

Getter for the draft

Annotations
@Override
getLastPongback to summary
pack-priv long getLastPong()

Getter for the last pong received

Returns:long

the timestamp for the last received pong

getLocalSocketAddressback to summary
public InetSocketAddress getLocalSocketAddress()

Implements org.java_websocket.WebSocket.getLocalSocketAddress.

Doc from org.java_websocket.WebSocket.getLocalSocketAddress.

Returns the address of the endpoint this socket is bound to, or null if it is not bound.

Annotations
@Override
getProtocolback to summary
public IProtocol getProtocol()

Implements org.java_websocket.WebSocket.getProtocol.

Doc from org.java_websocket.WebSocket.getProtocol.

Returns the used Sec-WebSocket-Protocol for this websocket connection

Annotations
@Override
getReadyStateback to summary
public ReadyState getReadyState()

Implements org.java_websocket.WebSocket.getReadyState.

Doc from org.java_websocket.WebSocket.getReadyState.

Retrieve the WebSocket 'ReadyState'. This represents the state of the connection. It returns a numerical value, as per W3C WebSockets specs.

Annotations
@Override
getRemoteSocketAddressback to summary
public InetSocketAddress getRemoteSocketAddress()

Implements org.java_websocket.WebSocket.getRemoteSocketAddress.

Doc from org.java_websocket.WebSocket.getRemoteSocketAddress.

Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

Annotations
@Override
getResourceDescriptorback to summary
public String getResourceDescriptor()

Implements org.java_websocket.WebSocket.getResourceDescriptor.

Doc from org.java_websocket.WebSocket.getResourceDescriptor.

Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1.2
If the opening handshake has not yet happened it will return null.

Annotations
@Override
getSelectionKeyback to summary
public SelectionKey getSelectionKey()
Returns:SelectionKey

the selection key of this implementation

getSSLSessionback to summary
public SSLSession getSSLSession()

Implements org.java_websocket.WebSocket.getSSLSession.

Doc from org.java_websocket.WebSocket.getSSLSession.

Returns the ssl session of websocket, if ssl/wss is used for this instance.

Annotations
@Override
getWebSocketListenerback to summary
public WebSocketListener getWebSocketListener()

Getter for the websocket listener

Returns:WebSocketListener

the websocket listener associated with this instance

getWorkerThreadback to summary
public WebSocketServer.WebSocketWorker getWorkerThread()
hasBufferedDataback to summary
public boolean hasBufferedData()

Implements org.java_websocket.WebSocket.hasBufferedData.

Doc from org.java_websocket.WebSocket.hasBufferedData.

Checks if the websocket has buffered data

Annotations
@Override
hasSSLSupportback to summary
public boolean hasSSLSupport()

Implements org.java_websocket.WebSocket.hasSSLSupport.

Doc from org.java_websocket.WebSocket.hasSSLSupport.

Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection

Annotations
@Override
isClosedback to summary
public boolean isClosed()

Implements org.java_websocket.WebSocket.isClosed.

Doc from org.java_websocket.WebSocket.isClosed.

Is the websocket in the state CLOSED

Annotations
@Override
isClosingback to summary
public boolean isClosing()

Implements org.java_websocket.WebSocket.isClosing.

Doc from org.java_websocket.WebSocket.isClosing.

Is the websocket in the state CLOSING

Annotations
@Override
isFlushAndCloseback to summary
public boolean isFlushAndClose()

Implements org.java_websocket.WebSocket.isFlushAndClose.

Doc from org.java_websocket.WebSocket.isFlushAndClose.

Returns true when no further frames may be submitted
This happens before the socket connection is closed.

Annotations
@Override
isOpenback to summary
public boolean isOpen()

Implements org.java_websocket.WebSocket.isOpen.

Doc from org.java_websocket.WebSocket.isOpen.

Is the websocket in the state OPEN

Annotations
@Override
openback to summary
private void open(Handshakedata d)
sendback to summary
public void send(String text)

Implements org.java_websocket.WebSocket.send.

Send Text data to the other end.

Annotations
@Override
Exceptions
WebsocketNotConnectedException:
websocket is not yet connected
sendback to summary
public void send(ByteBuffer bytes)

Implements org.java_websocket.WebSocket.send.

Send Binary data (plain bytes) to the other end.

Annotations
@Override
Exceptions
IllegalArgumentException:
the data is null
WebsocketNotConnectedException:
websocket is not yet connected
sendback to summary
public void send(byte[] bytes)

Implements org.java_websocket.WebSocket.send.

Doc from org.java_websocket.WebSocket.send.

Send Binary data (plain bytes) to the other end.

Annotations
@Override
sendback to summary
private void send(Collection<Framedata> frames)
sendFragmentedFrameback to summary
public void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)

Implements org.java_websocket.WebSocket.sendFragmentedFrame.

Doc from org.java_websocket.WebSocket.sendFragmentedFrame.

Allows to send continuous/fragmented frames conveniently.
For more into on this frame type see http://tools.ietf.org/html/rfc6455#section-5.4

If the first frame you send is also the last then it is not a fragmented frame and will received via onMessage instead of onFragmented even though it was send by this method.

Annotations
@Override
sendFrameback to summary
public void sendFrame(Collection<Framedata> frames)

Implements org.java_websocket.WebSocket.sendFrame.

Doc from org.java_websocket.WebSocket.sendFrame.

Send a collection of frames to the other end

Annotations
@Override
sendFrameback to summary
public void sendFrame(Framedata framedata)

Implements org.java_websocket.WebSocket.sendFrame.

Doc from org.java_websocket.WebSocket.sendFrame.

Send a frame to the other end

Annotations
@Override
sendPingback to summary
public void sendPing() throws NullPointerException

Implements org.java_websocket.WebSocket.sendPing.

Doc from org.java_websocket.WebSocket.sendPing.

Send a ping to the other end

setAttachmentback to summary
public <T> void setAttachment(T attachment)

Implements org.java_websocket.WebSocket.setAttachment.

Doc from org.java_websocket.WebSocket.setAttachment.

Setter for an attachment on the socket connection. The attachment may be of any type.

Annotations
@Override
setChannelback to summary
public void setChannel(ByteChannel channel)
setSelectionKeyback to summary
public void setSelectionKey(SelectionKey key)
Parameters
key:SelectionKey

the selection key of this implementation

setWorkerThreadback to summary
public void setWorkerThread(WebSocketServer.WebSocketWorker workerThread)
startHandshakeback to summary
public void startHandshake(ClientHandshakeBuilder handshakedata) throws InvalidHandshakeException
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
updateLastPongback to summary
public void updateLastPong()

Update the timestamp when the last pong was received

writeback to summary
private void write(ByteBuffer buf)
writeback to summary
private void write(List<ByteBuffer> bufs)

Write a list of bytebuffer (frames in binary form) into the outgoing queue

Parameters
bufs:List<ByteBuffer>

the list of bytebuffer