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 ByteChannel
channel
the possibly wrapped channel object whose selection is controlled bykey
static boolean
DEBUG
Activate debug mode for additional infosBlockingQueue<ByteBuffer>
inQueue
Queue of buffers that need to be processedSelectionKey
key
BlockingQueue<ByteBuffer>
outQueue
Queue of buffers that need to be sent to the client.static int
RCVBUF
WebSocketServer.WebSocketWorker
workerThread
Helper 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 void
close()
Convenience function which behaves like close(CloseFrame.NORMAL)void
close(int code)
sends the closing handshake.void
close(int code, String message)
sends the closing handshake.void
close(int code, String message, boolean remote)
void
close(InvalidDataException e)
void
closeConnection()
protected void
closeConnection(int code, boolean remote)
void
closeConnection(int code, String message)
This will close the connection immediately without a proper close handshake.void
closeConnection(int code, String message, boolean remote)
This will close the connection immediately without a proper close handshake.void
decode(ByteBuffer socketBuffer)
Method to decode the provided ByteBuffervoid
eot()
void
flushAndClose(int code, String message, boolean remote)
<T> T
getAttachment()
Getter for the connection attachment.Draft
getDraft()
Getter for the draftInetSocketAddress
getLocalSocketAddress()
Returns the address of the endpoint this socket is bound to.WebSocket.READYSTATE
getReadyState()
Retrieve the WebSocket 'readyState'.InetSocketAddress
getRemoteSocketAddress()
Returns the address of the endpoint this socket is connected to, ornull
if it is unconnected.String
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.WebSocketListener
getWebSocketListener()
Getter for the websocket listenerboolean
hasBufferedData()
Checks if the websocket has buffered dataint
hashCode()
boolean
isClosed()
Is the websocket in the state CLOSEDboolean
isClosing()
Is the websocket in the state CLOSINGboolean
isConnecting()
Deprecated.boolean
isFlushAndClose()
Returns true when no further frames may be submitted
This happens before the socket connection is closed.boolean
isOpen()
Is the websocket in the state OPENvoid
send(byte[] bytes)
Send Binary data (plain bytes) to the other end.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
sendFragmentedFrame(Framedata.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 endvoid
sendFrame(Framedata framedata)
Send a frame to the other endvoid
sendPing()
Send a ping to the other end<T> void
setAttachment(T attachment)
Setter for an attachment on the socket connection.void
startHandshake(ClientHandshakeBuilder handshakedata)
String
toString()
void
updateLastPong()
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:WebSocket
sends 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
.
true
means that this endpoint received thecode
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 thecode
but 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:WebSocket
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.- Specified by:
closeConnection
in 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:WebSocket
sends 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:
send
in 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:
send
in 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, WebsocketNotConnectedException
Description copied from interface:WebSocket
Send Binary data (plain bytes) to the other end.- Specified by:
send
in 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:WebSocket
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.- Specified by:
sendFragmentedFrame
in 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:WebSocket
Send a collection of frames to the other end
-
sendFrame
public void sendFrame(Framedata framedata)
Description copied from interface:WebSocket
Send a frame to the other end
-
sendPing
public void sendPing() throws NotYetConnectedException
Description copied from interface:WebSocket
Send a ping to the other end- Specified by:
sendPing
in interfaceWebSocket
- Throws:
NotYetConnectedException
- websocket is not yet connected
-
hasBufferedData
public boolean hasBufferedData()
Description copied from interface:WebSocket
Checks if the websocket has buffered data- Specified by:
hasBufferedData
in 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:WebSocket
Is the websocket in the state CONNECTING- Specified by:
isConnecting
in interfaceWebSocket
- Returns:
- state equals READYSTATE.CONNECTING
-
isOpen
public boolean isOpen()
Description copied from interface:WebSocket
Is the websocket in the state OPEN
-
isClosing
public boolean isClosing()
Description copied from interface:WebSocket
Is the websocket in the state CLOSING
-
isFlushAndClose
public boolean isFlushAndClose()
Description copied from interface:WebSocket
Returns true when no further frames may be submitted
This happens before the socket connection is closed.- Specified by:
isFlushAndClose
in interfaceWebSocket
- Returns:
- true when no further frames may be submitted
-
isClosed
public boolean isClosed()
Description copied from interface:WebSocket
Is the websocket in the state CLOSED
-
getReadyState
public WebSocket.READYSTATE getReadyState()
Description copied from interface:WebSocket
Retrieve the WebSocket 'readyState'. This represents the state of the connection. It returns a numerical value, as per W3C WebSockets specs.- Specified by:
getReadyState
in interfaceWebSocket
- Returns:
- Returns '0 = CONNECTING', '1 = OPEN', '2 = CLOSING' or '3 = CLOSED'
-
getRemoteSocketAddress
public InetSocketAddress getRemoteSocketAddress()
Description copied from interface:WebSocket
Returns the address of the endpoint this socket is connected to, ornull
if it is unconnected.- Specified by:
getRemoteSocketAddress
in interfaceWebSocket
- Returns:
- never returns null
-
getLocalSocketAddress
public InetSocketAddress getLocalSocketAddress()
Description copied from interface:WebSocket
Returns the address of the endpoint this socket is bound to.- Specified by:
getLocalSocketAddress
in interfaceWebSocket
- Returns:
- never returns null
-
close
public void close()
Description copied from interface:WebSocket
Convenience function which behaves like close(CloseFrame.NORMAL)
-
getResourceDescriptor
public String getResourceDescriptor()
Description copied from interface:WebSocket
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.- Specified by:
getResourceDescriptor
in 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:WebSocket
Getter for the connection attachment.- Specified by:
getAttachment
in 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:WebSocket
Setter for an attachment on the socket connection. The attachment may be of any type.- Specified by:
setAttachment
in interfaceWebSocket
- Type Parameters:
T
- The type of the attachment- Parameters:
attachment
- The object to be attached to the user
-
-