Package org.java_websocket
Class WebSocketImpl
java.lang.Object
org.java_websocket.WebSocketImpl
- All Implemented Interfaces:
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
FieldsModifier and TypeFieldDescriptionstatic final intThe default port of WebSockets, as defined in the spec.static final intThe default wss port of WebSockets, as defined in the spec.final BlockingQueue<ByteBuffer>Queue of buffers that need to be processedfinal BlockingQueue<ByteBuffer>Queue of buffers that need to be sent to the client. -
Constructor Summary
ConstructorsConstructorDescriptionWebSocketImpl(WebSocketListener listener, List<Draft> drafts) Creates a websocket with server roleWebSocketImpl(WebSocketListener listener, Draft draft) creates a websocket with client role -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Convenience function which behaves like close(CloseFrame.NORMAL)voidclose(int code) sends the closing handshake. may be send in response to an other handshake.voidsends the closing handshake. may be send in response to an other handshake.voidvoidvoidprotected 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> TGetter for the connection attachment.getDraft()Getter for the draftReturns the address of the endpoint this socket is bound to, ornullif it is not bound.Returns the used Sec-WebSocket-Protocol for this websocket connectionRetrieve the WebSocket 'ReadyState'.Returns the address of the endpoint this socket is connected to, ornullif it is unconnected.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.Returns the ssl session of websocket, if ssl/wss is used for this instance.Getter for the websocket listenerbooleanChecks if the websocket has buffered databooleanDoes this websocket use an encrypted (wss/ssl) or unencrypted (ws) connectionbooleanisClosed()Is the websocket in the state CLOSEDbooleanIs the websocket in the state CLOSINGbooleanReturns 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 Text data to the other end.voidsend(ByteBuffer 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 endvoidSend 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.voidsetChannel(ByteChannel channel) voidvoidsetWorkerThread(WebSocketServer.WebSocketWorker workerThread) voidstartHandshake(ClientHandshakeBuilder handshakedata) toString()voidUpdate the timestamp when the last pong was received
-
Field Details
-
DEFAULT_PORT
public static final int DEFAULT_PORTThe default port of WebSockets, as defined in the spec. If the nullary constructor is used, DEFAULT_PORT will be the port the WebSocketServer is binded to. Note that ports under 1024 usually require root permissions.- See Also:
-
DEFAULT_WSS_PORT
public static final int DEFAULT_WSS_PORTThe default wss port of WebSockets, as defined in the spec. If the nullary constructor is used, DEFAULT_WSS_PORT will be the port the WebSocketServer is binded to. Note that ports under 1024 usually require root permissions.- See Also:
-
outQueue
Queue of buffers that need to be sent to the client. -
inQueue
Queue of buffers that need to be processed
-
-
Constructor Details
-
WebSocketImpl
Creates a websocket with server role- Parameters:
listener- The listener for this instancedrafts- The drafts which should be used
-
WebSocketImpl
creates a websocket with client role- Parameters:
listener- The listener for this instancedraft- The draft which should be used
-
-
Method Details
-
decode
Method to decode the provided ByteBuffer- Parameters:
socketBuffer- the ByteBuffer to decode
-
close
-
close
Description copied from interface:WebSocketsends the closing handshake. may be send in response to an other handshake. -
closeConnection
This will close the connection immediately without a proper close handshake. The code and the message therefore won't be transferred 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
Description copied from interface:WebSocketThis will close the connection immediately without a proper close handshake. The code and the message therefore won't be transferred over the wire also they will be forwarded to onClose/onWebsocketClose.- Specified by:
closeConnectionin interfaceWebSocket- Parameters:
code- the closing codemessage- the closing message
-
flushAndClose
-
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
-
send
Send Text data to the other end.- Specified by:
sendin interfaceWebSocket- Parameters:
text- the text data to send- Throws:
WebsocketNotConnectedException- websocket is not yet connected
-
send
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 nullWebsocketNotConnectedException- websocket is not yet connected
-
send
public void send(byte[] bytes) Description copied from interface:WebSocketSend Binary data (plain bytes) to the other end. -
sendFragmentedFrame
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
Description copied from interface:WebSocketSend a collection of frames to the other end -
sendFrame
Description copied from interface:WebSocketSend a frame to the other end -
sendPing
Description copied from interface:WebSocketSend a ping to the other end- Specified by:
sendPingin interfaceWebSocket- Throws:
NullPointerException
-
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
- Throws:
InvalidHandshakeException
-
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
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'
-
setSelectionKey
- Parameters:
key- the selection key of this implementation
-
getSelectionKey
- Returns:
- the selection key of this implementation
-
toString
-
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:
- the remote socket address or null, if this socket is unconnected
-
getLocalSocketAddress
Description copied from interface:WebSocketReturns the address of the endpoint this socket is bound to, ornullif it is not bound.- Specified by:
getLocalSocketAddressin interfaceWebSocket- Returns:
- the local socket address or null, if this socket is not bound
-
getDraft
Description copied from interface:WebSocketGetter for the draft -
close
public void close()Description copied from interface:WebSocketConvenience function which behaves like close(CloseFrame.NORMAL) -
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
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
-
hasSSLSupport
public boolean hasSSLSupport()Description copied from interface:WebSocketDoes this websocket use an encrypted (wss/ssl) or unencrypted (ws) connection- Specified by:
hasSSLSupportin interfaceWebSocket- Returns:
- true, if the websocket does use wss and therefore has a SSLSession
-
getSSLSession
Description copied from interface:WebSocketReturns the ssl session of websocket, if ssl/wss is used for this instance.- Specified by:
getSSLSessionin interfaceWebSocket- Returns:
- the ssl session of this websocket instance
-
getProtocol
Description copied from interface:WebSocketReturns the used Sec-WebSocket-Protocol for this websocket connection- Specified by:
getProtocolin interfaceWebSocket- Returns:
- the Sec-WebSocket-Protocol or null, if no draft available
-
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
-
getChannel
-
setChannel
-
getWorkerThread
-
setWorkerThread
-