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.WebSocket
WebSocket.READYSTATE, WebSocket.Role
-
-
Field Summary
Fields 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.WebSocket
DEFAULT_PORT, DEFAULT_WSS_PORT
-
-
Constructor Summary
Constructors 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 Summary
All 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
-
RCVBUF
public static int RCVBUF
-
DEBUG
public static boolean DEBUG
Activate debug mode for additional infos
-
outQueue
public final BlockingQueue<ByteBuffer> outQueue
Queue of buffers that need to be sent to the client.
-
inQueue
public final BlockingQueue<ByteBuffer> inQueue
Queue of buffers that need to be processed
-
key
public SelectionKey key
-
channel
public ByteChannel channel
the possibly wrapped channel object whose selection is controlled bykey
-
workerThread
public volatile WebSocketServer.WebSocketWorker workerThread
Helper variable meant to store the thread which ( exclusively ) triggers this objects decode method.
-
-
Constructor Detail
-
WebSocketImpl
public 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
public WebSocketImpl(WebSocketListener listener, Draft draft)
creates a websocket with client role- Parameters:
listener- The listener for this instancedraft- 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
-
decode
public void decode(ByteBuffer socketBuffer)
Method to decode the provided ByteBuffer- Parameters:
socketBuffer- the ByteBuffer to decode
-
close
public void close(int code, String message, boolean remote)
-
close
public void close(int code, String message)Description copied from interface:WebSocketsends the closing handshake. may be send in response to an other handshake.
-
closeConnection
public 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 codemessage- the closing messageremote- Indicates who "generated"code.
truemeans that this endpoint received thecodefrom 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 thecodebut close the connection the same time this endpoint does do but with an othercode.
-
closeConnection
protected void closeConnection(int code, boolean remote)
-
closeConnection
public void closeConnection()
-
closeConnection
public 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 interfaceWebSocket- Parameters:
code- the closing codemessage- the closing message
-
flushAndClose
public void flushAndClose(int code, String message, boolean remote)
-
eot
public void eot()
-
close
public void close(int code)
Description copied from interface:WebSocketsends the closing handshake. may be send in response to an other handshake.
-
close
public void close(InvalidDataException e)
-
send
public void send(String text) throws WebsocketNotConnectedException
Send Text data to the other end.- Specified by:
sendin interfaceWebSocket- Parameters:
text- the text data to send- Throws:
NotYetConnectedException- websocket is not yet connectedWebsocketNotConnectedException
-
send
public void send(ByteBuffer bytes) throws IllegalArgumentException, WebsocketNotConnectedException
Send Binary data (plain bytes) to the other end.- Specified by:
sendin interfaceWebSocket- Parameters:
bytes- the binary data to send- Throws:
IllegalArgumentException- the data is nullNotYetConnectedException- websocket is not yet connectedWebsocketNotConnectedException
-
send
public void send(byte[] bytes) throws IllegalArgumentException, WebsocketNotConnectedExceptionDescription copied from interface:WebSocketSend Binary data (plain bytes) to the other end.- Specified by:
sendin interfaceWebSocket- Parameters:
bytes- the byte array to send- Throws:
IllegalArgumentException- the data is nullWebsocketNotConnectedException
-
sendFragmentedFrame
public 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 interfaceWebSocket- 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.
-
sendFrame
public void sendFrame(Collection<Framedata> frames)
Description copied from interface:WebSocketSend a collection of frames to the other end
-
sendFrame
public void sendFrame(Framedata framedata)
Description copied from interface:WebSocketSend a frame to the other end
-
sendPing
public void sendPing() throws NotYetConnectedExceptionDescription copied from interface:WebSocketSend a ping to the other end- Specified by:
sendPingin interfaceWebSocket- Throws:
NotYetConnectedException- websocket is not yet connected
-
hasBufferedData
public boolean hasBufferedData()
Description copied from interface:WebSocketChecks if the websocket has buffered data- Specified by:
hasBufferedDatain interfaceWebSocket- Returns:
- has the websocket buffered data
-
startHandshake
public 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 interfaceWebSocket- Returns:
- state equals READYSTATE.CONNECTING
-
isOpen
public boolean isOpen()
Description copied from interface:WebSocketIs the websocket in the state OPEN
-
isClosing
public boolean isClosing()
Description copied from interface:WebSocketIs the websocket in the state CLOSING
-
isFlushAndClose
public 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 interfaceWebSocket- Returns:
- true when no further frames may be submitted
-
isClosed
public boolean isClosed()
Description copied from interface:WebSocketIs the websocket in the state CLOSED
-
getReadyState
public 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 interfaceWebSocket- Returns:
- Returns '0 = CONNECTING', '1 = OPEN', '2 = CLOSING' or '3 = CLOSED'
-
getRemoteSocketAddress
public InetSocketAddress getRemoteSocketAddress()
Description copied from interface:WebSocketReturns the address of the endpoint this socket is connected to, ornullif it is unconnected.- Specified by:
getRemoteSocketAddressin interfaceWebSocket- Returns:
- never returns null
-
getLocalSocketAddress
public InetSocketAddress getLocalSocketAddress()
Description copied from interface:WebSocketReturns the address of the endpoint this socket is bound to.- Specified by:
getLocalSocketAddressin interfaceWebSocket- Returns:
- never returns null
-
close
public void close()
Description copied from interface:WebSocketConvenience function which behaves like close(CloseFrame.NORMAL)
-
getResourceDescriptor
public 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 interfaceWebSocket- Returns:
- Returns the decoded path component of this URI.
-
updateLastPong
public void updateLastPong()
Update the timestamp when the last pong was received
-
getWebSocketListener
public WebSocketListener getWebSocketListener()
Getter for the websocket listener- Returns:
- the websocket listener associated with this instance
-
getAttachment
public <T> T getAttachment()
Description copied from interface:WebSocketGetter for the connection attachment.- Specified by:
getAttachmentin interfaceWebSocket- Type Parameters:
T- The type of the attachment- Returns:
- Returns the user attachment
-
setAttachment
public <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 interfaceWebSocket- Type Parameters:
T- The type of the attachment- Parameters:
attachment- The object to be attached to the user
-
-