-
- All Implemented Interfaces:
-
com.tds.common.websocket.conn.WebSocket
public class WebSocketImpl implements WebSocket
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
Fields Modifier and Type Field Description public final static int
DEFAULT_PORT
public final static int
DEFAULT_WSS_PORT
public final static int
RCVBUF
public final BlockingQueue<ByteBuffer>
outQueue
public final BlockingQueue<ByteBuffer>
inQueue
private ByteChannel
channel
private WebSocketServer.WebSocketWorker
workerThread
private volatile ReadyState
readyState
private Draft
draft
private String
resourceDescriptor
private Object
attachment
-
Constructor Summary
Constructors Constructor Description WebSocketImpl(WebSocketListener listener, List<Draft> drafts)
Creates a websocket with server role WebSocketImpl(WebSocketListener listener, Draft draft)
creates a websocket with client role
-
Method Summary
Modifier and Type Method Description ByteChannel
getChannel()
void
setChannel(ByteChannel channel)
WebSocketServer.WebSocketWorker
getWorkerThread()
void
setWorkerThread(WebSocketServer.WebSocketWorker workerThread)
ReadyState
getReadyState()
Retrieve the WebSocket 'ReadyState'. Draft
getDraft()
Getter for the draft String
getResourceDescriptor()
Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1. <T> void
setAttachment(T attachment)
Setter for an attachment on the socket connection. void
decode(ByteBuffer socketBuffer)
Method to decode the provided ByteBuffer synchronized void
close(int code, String message, boolean remote)
void
close(int code, String message)
sends the closing handshake. synchronized void
closeConnection(int code, String message, boolean remote)
This will close the connection immediately without a proper close handshake. void
closeConnection()
void
closeConnection(int code, String message)
This will close the connection immediately without a proper close handshake. synchronized void
flushAndClose(int code, String message, boolean remote)
void
eot()
void
close(int code)
sends the closing handshake. void
close(InvalidDataException e)
void
send(String text)
Send Text data to the other end. void
send(ByteBuffer bytes)
Send Binary data (plain bytes) to the other end. void
send(Array<byte> bytes)
Send Binary data (plain bytes) to the other end. void
sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)
Allows to send continuous/fragmented frames conveniently. void
sendFrame(Collection<Framedata> frames)
Send a collection of frames to the other end void
sendFrame(Framedata framedata)
Send a frame to the other end void
sendPing()
Send a ping to the other end boolean
hasBufferedData()
Checks if the websocket has buffered data void
startHandshake(ClientHandshakeBuilder handshakedata)
boolean
isOpen()
Is the websocket in the state OPEN boolean
isClosing()
Is the websocket in the state CLOSING boolean
isFlushAndClose()
Returns true when no further frames may be submitted This happens before the socketconnection is closed. boolean
isClosed()
Is the websocket in the state CLOSED void
setSelectionKey(SelectionKey key)
SelectionKey
getSelectionKey()
String
toString()
InetSocketAddress
getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or {@code null}
if it isunconnected.InetSocketAddress
getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to. void
close()
Convenience function which behaves like close(CloseFrame. void
updateLastPong()
Update the timestamp when the last pong was received WebSocketListener
getWebSocketListener()
Getter for the websocket listener boolean
hasSSLSupport()
Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection SSLSession
getSSLSession()
Returns the ssl session of websocket, if ssl/wss is used for this instance. IProtocol
getProtocol()
Returns the used Sec-WebSocket-Protocol for this websocket connection -
-
Constructor Detail
-
WebSocketImpl
WebSocketImpl(WebSocketListener listener, List<Draft> drafts)
Creates a websocket with server role- Parameters:
listener
- The listener for this instancedrafts
- The drafts which should be used
-
WebSocketImpl
WebSocketImpl(WebSocketListener listener, Draft draft)
creates a websocket with client role- Parameters:
listener
- The listener for this instancedraft
- The draft which should be used
-
-
Method Detail
-
getChannel
ByteChannel getChannel()
-
setChannel
void setChannel(ByteChannel channel)
-
getWorkerThread
WebSocketServer.WebSocketWorker getWorkerThread()
-
setWorkerThread
void setWorkerThread(WebSocketServer.WebSocketWorker workerThread)
-
getReadyState
ReadyState getReadyState()
Retrieve the WebSocket 'ReadyState'. This represents the state of the connection. It returns anumerical value, as per W3C WebSockets specs.
-
getResourceDescriptor
String getResourceDescriptor()
Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1.2If the opening handshake has not yet happened it will return null.
-
setAttachment
<T> void setAttachment(T attachment)
Setter for an attachment on the socket connection. The attachment may be of any type.
- Parameters:
attachment
- The object to be attached to the user
-
decode
void decode(ByteBuffer socketBuffer)
Method to decode the provided ByteBuffer
- Parameters:
socketBuffer
- the ByteBuffer to decode
-
close
void close(int code, String message)
sends the closing handshake. may be send in response to an other handshake.
- Parameters:
code
- the closing codemessage
- the closing message
-
closeConnection
synchronized void closeConnection(int code, String message, boolean remote)
This will close the connection immediately without a proper close handshake. The code and themessage therefore won't be transferred over the wire also they will be forwarded toonClose/onWebsocketClose.
- Parameters:
code
- the closing codemessage
- the closing messageremote
- Indicates who "generated"code
.
-
closeConnection
void closeConnection()
-
closeConnection
void closeConnection(int code, String message)
This will close the connection immediately without a proper close handshake. The code and themessage therefore won't be transferred over the wire also they will be forwarded toonClose/onWebsocketClose.
- Parameters:
code
- the closing codemessage
- the closing message
-
flushAndClose
synchronized void flushAndClose(int code, String message, boolean remote)
-
eot
void eot()
-
close
void close(int code)
sends the closing handshake. may be send in response to an other handshake.
- Parameters:
code
- the closing code
-
close
void close(InvalidDataException e)
-
send
void send(ByteBuffer bytes)
Send Binary data (plain bytes) to the other end.
-
send
void send(Array<byte> bytes)
Send Binary data (plain bytes) to the other end.
- Parameters:
bytes
- the byte array to send
-
sendFragmentedFrame
void sendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)
Allows to send continuous/fragmented frames conveniently. For more into on this frame typesee 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 willreceived via onMessage instead of onFragmented even though it was send by this method.
- Parameters:
op
- This is only important for the first frame in the sequence.buffer
- The buffer which contains the payload.fin
- true means the current frame is the last in the sequence.
-
sendFrame
void sendFrame(Collection<Framedata> frames)
Send a collection of frames to the other end
- Parameters:
frames
- the frames to send to the other end
-
sendFrame
void sendFrame(Framedata framedata)
Send a frame to the other end
- Parameters:
framedata
- the frame to send to the other end
-
sendPing
void sendPing()
Send a ping to the other end
-
hasBufferedData
boolean hasBufferedData()
Checks if the websocket has buffered data
-
startHandshake
void startHandshake(ClientHandshakeBuilder handshakedata)
-
isOpen
boolean isOpen()
Is the websocket in the state OPEN
-
isClosing
boolean isClosing()
Is the websocket in the state CLOSING
-
isFlushAndClose
boolean isFlushAndClose()
Returns true when no further frames may be submitted This happens before the socketconnection is closed.
-
isClosed
boolean isClosed()
Is the websocket in the state CLOSED
-
setSelectionKey
void setSelectionKey(SelectionKey key)
- Parameters:
key
- the selection key of this implementation
-
getSelectionKey
SelectionKey getSelectionKey()
-
getRemoteSocketAddress
InetSocketAddress getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, or
{@code null}
if it isunconnected.
-
getLocalSocketAddress
InetSocketAddress getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to.
-
close
void close()
Convenience function which behaves like close(CloseFrame.NORMAL)
-
updateLastPong
void updateLastPong()
Update the timestamp when the last pong was received
-
getWebSocketListener
WebSocketListener getWebSocketListener()
Getter for the websocket listener
-
hasSSLSupport
boolean hasSSLSupport()
Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection
-
getSSLSession
SSLSession getSSLSession()
Returns the ssl session of websocket, if ssl/wss is used for this instance.
-
getProtocol
IProtocol getProtocol()
Returns the used Sec-WebSocket-Protocol for this websocket connection
-
-
-
-