Package org.java_websocket
Class WebSocketImpl
- java.lang.Object
- 
- org.java_websocket.WebSocketImpl
 
- 
- All Implemented Interfaces:
- WebSocket
 
 public class WebSocketImpl extends Object 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.
- 
- 
Nested Class Summary- 
Nested classes/interfaces inherited from interface org.java_websocket.WebSocketWebSocket.READYSTATE, WebSocket.Role
 
- 
 - 
Field SummaryFields Modifier and Type Field Description ByteChannelchannelthe possibly wrapped channel object whose selection is controlled bykeystatic booleanDEBUGActivate debug mode for additional infosBlockingQueue<ByteBuffer>inQueueQueue of buffers that need to be processedSelectionKeykeyBlockingQueue<ByteBuffer>outQueueQueue of buffers that need to be sent to the client.static intRCVBUFWebSocketServer.WebSocketWorkerworkerThreadHelper variable meant to store the thread which ( exclusively ) triggers this objects decode method.- 
Fields inherited from interface org.java_websocket.WebSocketDEFAULT_PORT, DEFAULT_WSS_PORT
 
- 
 - 
Constructor SummaryConstructors Constructor Description WebSocketImpl(WebSocketListener listener, List<Draft> drafts)Creates a websocket with server roleWebSocketImpl(WebSocketListener listener, List<Draft> drafts, Socket socket)Deprecated.WebSocketImpl(WebSocketListener listener, Draft draft)creates a websocket with client roleWebSocketImpl(WebSocketListener listener, Draft draft, Socket socket)Deprecated.
 - 
Method SummaryAll Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidclose()Convenience function which behaves like close(CloseFrame.NORMAL)voidclose(int code)sends the closing handshake.voidclose(int code, String message)sends the closing handshake.voidclose(int code, String message, boolean remote)voidclose(InvalidDataException e)voidcloseConnection()protected voidcloseConnection(int code, boolean remote)voidcloseConnection(int code, String message)This will close the connection immediately without a proper close handshake.voidcloseConnection(int code, String message, boolean remote)This will close the connection immediately without a proper close handshake.voiddecode(ByteBuffer socketBuffer)Method to decode the provided ByteBuffervoideot()voidflushAndClose(int code, String message, boolean remote)<T> TgetAttachment()Getter for the connection attachment.DraftgetDraft()Getter for the draftInetSocketAddressgetLocalSocketAddress()Returns the address of the endpoint this socket is bound to.WebSocket.READYSTATEgetReadyState()Retrieve the WebSocket 'readyState'.InetSocketAddressgetRemoteSocketAddress()Returns the address of the endpoint this socket is connected to, ornullif it is unconnected.StringgetResourceDescriptor()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.WebSocketListenergetWebSocketListener()Getter for the websocket listenerbooleanhasBufferedData()Checks if the websocket has buffered datainthashCode()booleanisClosed()Is the websocket in the state CLOSEDbooleanisClosing()Is the websocket in the state CLOSINGbooleanisConnecting()Deprecated.booleanisFlushAndClose()Returns true when no further frames may be submitted
 This happens before the socket connection is closed.booleanisOpen()Is the websocket in the state OPENvoidsend(byte[] bytes)Send Binary data (plain bytes) to the other end.voidsend(String text)Send Text data to the other end.voidsend(ByteBuffer bytes)Send Binary data (plain bytes) to the other end.voidsendFragmentedFrame(Framedata.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 endvoidsendFrame(Framedata framedata)Send a frame to the other endvoidsendPing()Send a ping to the other end<T> voidsetAttachment(T attachment)Setter for an attachment on the socket connection.voidstartHandshake(ClientHandshakeBuilder handshakedata)StringtoString()voidupdateLastPong()Update the timestamp when the last pong was received
 
- 
- 
- 
Field Detail- 
RCVBUFpublic static int RCVBUF 
 - 
DEBUGpublic static boolean DEBUG Activate debug mode for additional infos
 - 
outQueuepublic final BlockingQueue<ByteBuffer> outQueue Queue of buffers that need to be sent to the client.
 - 
inQueuepublic final BlockingQueue<ByteBuffer> inQueue Queue of buffers that need to be processed
 - 
keypublic SelectionKey key 
 - 
channelpublic ByteChannel channel the possibly wrapped channel object whose selection is controlled bykey
 - 
workerThreadpublic volatile WebSocketServer.WebSocketWorker workerThread Helper variable meant to store the thread which ( exclusively ) triggers this objects decode method.
 
- 
 - 
Constructor Detail- 
WebSocketImplpublic WebSocketImpl(WebSocketListener listener, List<Draft> drafts) Creates a websocket with server role- Parameters:
- listener- The listener for this instance
- drafts- The drafts which should be used
 
 - 
WebSocketImplpublic WebSocketImpl(WebSocketListener listener, Draft draft) creates a websocket with client role- Parameters:
- listener- The listener for this instance
- draft- The draft which should be used
 
 - 
WebSocketImpl@Deprecated public WebSocketImpl(WebSocketListener listener, Draft draft, Socket socket) Deprecated.
 - 
WebSocketImpl@Deprecated public WebSocketImpl(WebSocketListener listener, List<Draft> drafts, Socket socket) Deprecated.
 
- 
 - 
Method Detail- 
decodepublic void decode(ByteBuffer socketBuffer) Method to decode the provided ByteBuffer- Parameters:
- socketBuffer- the ByteBuffer to decode
 
 - 
closepublic void close(int code, String message, boolean remote)
 - 
closepublic void close(int code, String message)Description copied from interface:WebSocketsends the closing handshake. may be send in response to an other handshake.
 - 
closeConnectionpublic 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 transfered over the wire also they will be forwarded to onClose/onWebsocketClose.- Parameters:
- code- the closing code
- message- the closing message
- remote- Indicates who "generated"- code.
 - truemeans that this endpoint received the- codefrom the other endpoint.
 false means this endpoint decided to send the given code,
 - remotemay also be true if this endpoint started the closing handshake since the other endpoint may not simply echo the- codebut close the connection the same time this endpoint does do but with an other- code.
 
 - 
closeConnectionprotected void closeConnection(int code, boolean remote)
 - 
closeConnectionpublic void closeConnection() 
 - 
closeConnectionpublic void closeConnection(int code, String message)Description copied from interface:WebSocketThis will close the connection immediately without a proper close handshake. The code and the message therefore won't be transfered over the wire also they will be forwarded to onClose/onWebsocketClose.- Specified by:
- closeConnectionin interface- WebSocket
- Parameters:
- code- the closing code
- message- the closing message
 
 - 
flushAndClosepublic void flushAndClose(int code, String message, boolean remote)
 - 
eotpublic void eot() 
 - 
closepublic void close(int code) Description copied from interface:WebSocketsends the closing handshake. may be send in response to an other handshake.
 - 
closepublic void close(InvalidDataException e) 
 - 
sendpublic void send(String text) throws WebsocketNotConnectedException Send Text data to the other end.- Specified by:
- sendin interface- WebSocket
- Parameters:
- text- the text data to send
- Throws:
- NotYetConnectedException- websocket is not yet connected
- WebsocketNotConnectedException
 
 - 
sendpublic void send(ByteBuffer bytes) throws IllegalArgumentException, WebsocketNotConnectedException Send Binary data (plain bytes) to the other end.- Specified by:
- sendin interface- WebSocket
- Parameters:
- bytes- the binary data to send
- Throws:
- IllegalArgumentException- the data is null
- NotYetConnectedException- websocket is not yet connected
- WebsocketNotConnectedException
 
 - 
sendpublic void send(byte[] bytes) throws IllegalArgumentException, WebsocketNotConnectedExceptionDescription copied from interface:WebSocketSend Binary data (plain bytes) to the other end.- Specified by:
- sendin interface- WebSocket
- Parameters:
- bytes- the byte array to send
- Throws:
- IllegalArgumentException- the data is null
- WebsocketNotConnectedException
 
 - 
sendFragmentedFramepublic void sendFragmentedFrame(Framedata.Opcode op, ByteBuffer buffer, boolean fin) Description copied from interface:WebSocketAllows 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.- Specified by:
- sendFragmentedFramein interface- WebSocket
- Parameters:
- op- This is only important for the first frame in the sequence. Opcode.TEXT, Opcode.BINARY are allowed.
- buffer- The buffer which contains the payload. It may have no bytes remaining.
- fin- true means the current frame is the last in the sequence.
 
 - 
sendFramepublic void sendFrame(Collection<Framedata> frames) Description copied from interface:WebSocketSend a collection of frames to the other end
 - 
sendFramepublic void sendFrame(Framedata framedata) Description copied from interface:WebSocketSend a frame to the other end
 - 
sendPingpublic void sendPing() throws NotYetConnectedExceptionDescription copied from interface:WebSocketSend a ping to the other end- Specified by:
- sendPingin interface- WebSocket
- Throws:
- NotYetConnectedException- websocket is not yet connected
 
 - 
hasBufferedDatapublic boolean hasBufferedData() Description copied from interface:WebSocketChecks if the websocket has buffered data- Specified by:
- hasBufferedDatain interface- WebSocket
- Returns:
- has the websocket buffered data
 
 - 
startHandshakepublic void startHandshake(ClientHandshakeBuilder handshakedata) throws InvalidHandshakeException - Throws:
- InvalidHandshakeException
 
 - 
isConnecting@Deprecated public boolean isConnecting() Deprecated.Description copied from interface:WebSocketIs the websocket in the state CONNECTING- Specified by:
- isConnectingin interface- WebSocket
- Returns:
- state equals READYSTATE.CONNECTING
 
 - 
isOpenpublic boolean isOpen() Description copied from interface:WebSocketIs the websocket in the state OPEN
 - 
isClosingpublic boolean isClosing() Description copied from interface:WebSocketIs the websocket in the state CLOSING
 - 
isFlushAndClosepublic boolean isFlushAndClose() Description copied from interface:WebSocketReturns true when no further frames may be submitted
 This happens before the socket connection is closed.- Specified by:
- isFlushAndClosein interface- WebSocket
- Returns:
- true when no further frames may be submitted
 
 - 
isClosedpublic boolean isClosed() Description copied from interface:WebSocketIs the websocket in the state CLOSED
 - 
getReadyStatepublic WebSocket.READYSTATE getReadyState() Description copied from interface:WebSocketRetrieve the WebSocket 'readyState'. This represents the state of the connection. It returns a numerical value, as per W3C WebSockets specs.- Specified by:
- getReadyStatein interface- WebSocket
- Returns:
- Returns '0 = CONNECTING', '1 = OPEN', '2 = CLOSING' or '3 = CLOSED'
 
 - 
getRemoteSocketAddresspublic InetSocketAddress getRemoteSocketAddress() Description copied from interface:WebSocketReturns the address of the endpoint this socket is connected to, ornullif it is unconnected.- Specified by:
- getRemoteSocketAddressin interface- WebSocket
- Returns:
- never returns null
 
 - 
getLocalSocketAddresspublic InetSocketAddress getLocalSocketAddress() Description copied from interface:WebSocketReturns the address of the endpoint this socket is bound to.- Specified by:
- getLocalSocketAddressin interface- WebSocket
- Returns:
- never returns null
 
 - 
closepublic void close() Description copied from interface:WebSocketConvenience function which behaves like close(CloseFrame.NORMAL)
 - 
getResourceDescriptorpublic String getResourceDescriptor() Description copied from interface:WebSocketReturns 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.- Specified by:
- getResourceDescriptorin interface- WebSocket
- Returns:
- Returns the decoded path component of this URI.
 
 - 
updateLastPongpublic void updateLastPong() Update the timestamp when the last pong was received
 - 
getWebSocketListenerpublic WebSocketListener getWebSocketListener() Getter for the websocket listener- Returns:
- the websocket listener associated with this instance
 
 - 
getAttachmentpublic <T> T getAttachment() Description copied from interface:WebSocketGetter for the connection attachment.- Specified by:
- getAttachmentin interface- WebSocket
- Type Parameters:
- T- The type of the attachment
- Returns:
- Returns the user attachment
 
 - 
setAttachmentpublic <T> void setAttachment(T attachment) Description copied from interface:WebSocketSetter for an attachment on the socket connection. The attachment may be of any type.- Specified by:
- setAttachmentin interface- WebSocket
- Type Parameters:
- T- The type of the attachment
- Parameters:
- attachment- The object to be attached to the user
 
 
- 
 
-