public abstract class WebSocket extends Object
Original implementation based on "HyBi Working Group, draft-ietf-hybi-thewebsocketprotocol-07, April 22, 2011, by I. Fette, Google Inc." — updated to RFC 6455 in March 2026.
See https://tools.ietf.org/html/rfc6455 for more information.
The WebSocket protocol enables two-way communication between a user agent running untrusted code running in a controlled environment to a remote host that has opted-in to communications from that code. The security model used for this is the Origin-based security model commonly used by Web browsers. The protocol consists of an initial handshake followed by basic message framing, layered over TCP. The goal of this technology is to provide a mechanism for browser-based applications that need two-way communication with servers that does not rely on opening multiple HTTP connections (e.g. using XMLHttpRequest or <iframe>s and long polling).
| Modifier and Type | Class and Description |
|---|---|
static class |
WebSocket.State
The states enumeration for client web sockets.
|
| Modifier and Type | Field and Description |
|---|---|
protected static String |
ACCEPT_GUID
The GUID for the WebSocket handshake accept computation (RFC 6455 Section 4.2.2).
|
static int |
CLOSE_GOING_AWAY
RFC 6455 close status code: Going Away (1001).
|
static int |
CLOSE_NORMAL
RFC 6455 close status code: Normal Closure (1000).
|
static int |
CLOSE_PROTOCOL_ERROR
RFC 6455 close status code: Protocol Error (1002).
|
protected static String |
CRLF
CR/LF string.
|
protected BufferedInputStream |
input
The input stream.
|
protected boolean |
isSecure
If this is secured with SSL or not (WS or WSS).
|
protected BufferedOutputStream |
output
The output stream.
|
protected int |
protocolVersion
Protocol version (13 for RFC 6455, 0 for old Hixie-76).
|
protected String |
ssubProtocol
The server-accepted sub-protocol, null for none.
|
protected WebSocket.State |
state
State of connection.
|
| Constructor and Description |
|---|
WebSocket(InputStream input,
OutputStream output,
boolean isSecure)
Establishes the WebSocket connection using streams only (no raw Socket reference).
|
WebSocket(Socket socket,
boolean isSecure)
Establishes the WebSocket connection using an already connected
socket.
|
WebSocket(Socket socket,
InputStream input,
OutputStream output,
boolean isSecure)
Establishes the WebSocket connection using an already connected
socket.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
close()
Closes the connection gracefully.
|
void |
closeSocket()
Closes the socket connection without informing the other WebSocket party.
|
void |
closeSocketHard()
Closes the socket connection in a very hard way.
|
byte[] |
getBinaryMessage()
Gets a binary message.
|
boolean |
getOldTextProtocol()
Gets the flag for "old" protocol for UTF-8 text, i.e.
|
int |
getProtocolVersion()
Gets the protocol version.
|
String |
getServerSubProtocol()
Gets the server-accepted sub-protocol, null for none.
|
WebSocket.State |
getState()
Gets the current state of the connection.
|
String |
getTextMessage()
Gets a text message.
|
boolean |
hasBinaryMessage()
Checks if the message received is a binary message.
|
boolean |
hasTextMessage()
Checks if the message received is a text message.
|
protected abstract boolean |
isServerSide()
Returns true if this is the server side of the WebSocket connection.
|
static boolean |
isVerbose()
Checks if verbose output is enabled.
|
protected static void |
log(String msg)
Logs a message if verbose mode is enabled.
|
protected static void |
log(String msg,
Throwable t)
Logs a message with an exception if verbose mode is enabled.
|
int |
readMessage()
Reads a message.
|
void |
sendBinaryData(byte[] data)
Sends binary data.
|
void |
sendBinaryData(byte[] data,
int offset,
int length)
Sends binary data.
|
void |
sendCloseFrame(int statusCode,
String reason)
Sends a close frame with a status code and optional reason (RFC 6455 Section 5.5.1).
|
void |
sendText(String message)
Sends a text message.
|
boolean |
setOldTextProtocol(boolean on)
Sets the "old" protocol for UTF-8 text, i.e.
|
void |
setProtocolVersion(int version)
Sets the protocol version.
|
static void |
setVerbose(boolean on)
Sets the verbose flag.
|
protected static final String CRLF
protected static final String ACCEPT_GUID
public static final int CLOSE_NORMAL
public static final int CLOSE_GOING_AWAY
public static final int CLOSE_PROTOCOL_ERROR
protected final boolean isSecure
protected final BufferedInputStream input
protected final BufferedOutputStream output
protected String ssubProtocol
protected volatile WebSocket.State state
protected int protocolVersion
public WebSocket(Socket socket, boolean isSecure) throws IOException
socket - The established socket.isSecure - If the connection is secure.IOException - For failures.public WebSocket(Socket socket, InputStream input, OutputStream output, boolean isSecure) throws IOException
socket - The established socket.input - The input stream.output - The output stream.isSecure - If the connection is secure.IOException - For failures.public WebSocket(InputStream input, OutputStream output, boolean isSecure) throws IOException
Note: close operations will only close the streams, not the underlying socket. The caller (e.g. HttpRequestParser) is responsible for socket lifecycle.
input - The input stream.output - The output stream.isSecure - If the connection is secure.IOException - For failures.public static void setVerbose(boolean on)
on - The state of the verbose output.public static boolean isVerbose()
protected static void log(String msg)
msg - The message to log.protected static void log(String msg, Throwable t)
msg - The message to log.t - The exception to log.protected abstract boolean isServerSide()
public final boolean setOldTextProtocol(boolean on)
on - Flag to turn on or off the mode.public final boolean getOldTextProtocol()
public final int getProtocolVersion()
public final void setProtocolVersion(int version)
version - The version (13 for RFC 6455).public final WebSocket.State getState()
public final String getServerSubProtocol()
public final int readMessage()
throws IOException
IOException - for frame and protocol errors.public final boolean hasTextMessage()
public final boolean hasBinaryMessage()
public String getTextMessage()
public byte[] getBinaryMessage()
public final void sendText(String message) throws IOException
message - A text message.IOException - for frame and protocol errors.IllegalArgumentException - for illegal arguments.public final void sendBinaryData(byte[] data)
throws IOException
data - the binary data to send.IOException - for frame and protocol errors.IllegalArgumentException - for illegal arguments.public final void sendBinaryData(byte[] data,
int offset,
int length)
throws IOException
data - the binary data to send.offset - the starting offset in the data.length - the length of data to send.IOException - for frame and protocol errors.IllegalArgumentException - for illegal arguments.public final void sendCloseFrame(int statusCode,
String reason)
throws IOException
statusCode - The close status code (e.g. CLOSE_NORMAL).reason - Optional human-readable reason, may be null.IOException - For I/O errors.public final boolean close()
throws IOException
IOException - for frame and protocol errors.public final void closeSocket()
public final void closeSocketHard()
Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.