-
- 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 intDEFAULT_PORTpublic final static intDEFAULT_WSS_PORTpublic final static intRCVBUFpublic final BlockingQueue<ByteBuffer>outQueuepublic final BlockingQueue<ByteBuffer>inQueueprivate ByteChannelchannelprivate WebSocketServer.WebSocketWorkerworkerThreadprivate volatile ReadyStatereadyStateprivate Draftdraftprivate StringresourceDescriptorprivate Objectattachment
-
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 ByteChannelgetChannel()voidsetChannel(ByteChannel channel)WebSocketServer.WebSocketWorkergetWorkerThread()voidsetWorkerThread(WebSocketServer.WebSocketWorker workerThread)ReadyStategetReadyState()Retrieve the WebSocket 'ReadyState'. DraftgetDraft()Getter for the draft StringgetResourceDescriptor()Returns the HTTP Request-URI as defined by http://tools.ietf.org/html/rfc2616#section-5.1. <T> voidsetAttachment(T attachment)Setter for an attachment on the socket connection. voiddecode(ByteBuffer socketBuffer)Method to decode the provided ByteBuffer synchronized voidclose(int code, String message, boolean remote)voidclose(int code, String message)sends the closing handshake. synchronized voidcloseConnection(int code, String message, boolean remote)This will close the connection immediately without a proper close handshake. voidcloseConnection()voidcloseConnection(int code, String message)This will close the connection immediately without a proper close handshake. synchronized voidflushAndClose(int code, String message, boolean remote)voideot()voidclose(int code)sends the closing handshake. voidclose(InvalidDataException e)voidsend(String text)Send Text data to the other end. voidsend(ByteBuffer bytes)Send Binary data (plain bytes) to the other end. voidsend(Array<byte> bytes)Send Binary data (plain bytes) to the other end. voidsendFragmentedFrame(Opcode op, ByteBuffer buffer, boolean fin)Allows to send continuous/fragmented frames conveniently. voidsendFrame(Collection<Framedata> frames)Send a collection of frames to the other end voidsendFrame(Framedata framedata)Send a frame to the other end voidsendPing()Send a ping to the other end booleanhasBufferedData()Checks if the websocket has buffered data voidstartHandshake(ClientHandshakeBuilder handshakedata)booleanisOpen()Is the websocket in the state OPEN booleanisClosing()Is the websocket in the state CLOSING booleanisFlushAndClose()Returns true when no further frames may be submitted This happens before the socketconnection is closed. booleanisClosed()Is the websocket in the state CLOSED voidsetSelectionKey(SelectionKey key)SelectionKeygetSelectionKey()StringtoString()InetSocketAddressgetRemoteSocketAddress()Returns the address of the endpoint this socket is connected to, or {@code null}if it isunconnected.InetSocketAddressgetLocalSocketAddress()Returns the address of the endpoint this socket is bound to. voidclose()Convenience function which behaves like close(CloseFrame. voidupdateLastPong()Update the timestamp when the last pong was received WebSocketListenergetWebSocketListener()Getter for the websocket listener booleanhasSSLSupport()Does this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection SSLSessiongetSSLSession()Returns the ssl session of websocket, if ssl/wss is used for this instance. IProtocolgetProtocol()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
-
-
-
-