public abstract class WebSocket extends Object
Support for "draft-hixie-thewebsocketprotocol-76" is also implemented.
See http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-07 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 protocol version 4 or better, used for the
|
protected static byte[] |
clientToServerMask
The binary mask for frames sent from client to server.
|
protected static String |
CRLF
CR/LF string.
|
protected static String |
CS_FRAME_GUID
The GUID mask for frames sent from client to server.
|
BufferedInputStream |
input
The input stream.
|
boolean |
isSecure
If this is secured with SSL or not (WS or WSS).
|
protected static boolean |
isVerbose
The verbose flag.
|
BufferedOutputStream |
output
The output stream.
|
protected int |
protocolVersion
Protocol version (only defined for version 3 or above).
|
Socket |
socket
The Socket used for transport.
|
protected String |
ssubProtocol
The server-accepted sub-protocol, null for none.
|
protected WebSocket.State |
state
State of connection.
|
Constructor and Description |
---|
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.
|
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.
|
static boolean |
isVerbose()
Checks if verbose output 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 |
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 boolean isVerbose
protected static final String CRLF
protected static final String ACCEPT_GUID
protected static final String CS_FRAME_GUID
protected static byte[] clientToServerMask
public final Socket socket
public final boolean isSecure
public final BufferedInputStream input
public final BufferedOutputStream output
protected String ssubProtocol
protected 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.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()
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 should be 3 or greater, according to the WebSocket protocol specification,
currently only 3 to 7 is supported.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 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.
© © Mindus SARL, 2024. All rights reserved.