Class WebSocket
java.lang.Object
net.targetr.wtm3.net.http.websocket.WebSocket
- All Implemented Interfaces:
Closeable,AutoCloseable
Reads and writes data frames to a WebSocket.
- Author:
- Dr Michael Gardiner
-
Constructor Summary
ConstructorsConstructorDescriptionWebSocket(InputStream in, OutputStream out) Constructs a new WebSocket instance with the given input and output streams. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes this WebSocket and its associated input and output streams.longReturns the timestamp of the last write operation on this WebSocket.booleanisClosed()Returns whether this WebSocket has been closed.read()Reads a WebSocket frame from the input stream.readText()Reads a text message from the WebSocket.voidsetInterceptor(WebSocketFrameInterceptor interceptor) Sets the interceptor for WebSocket frames.static voidunmask(byte[] payload, byte[] maskingKey) Unmasks a WebSocket payload.static voidunmaskFast(byte[] payload, byte[] maskingKey) Unmasks a WebSocket payload using a fast 8-byte unrolling technique.voidwrite(WebSocketFrame frame) Writes the given WebSocket frame to the output stream.voidwriteClose(int code) Writes a WebSocket close frame with the given status code.voidWrites a WebSocket ping frame.voidWrites a WebSocket pong frame.voidwriteSimpleOp(int op, byte[] data) Writes a simple WebSocket frame with the given opcode and data.voidwriteText(byte[] bytes) Writes the given text message to the WebSocket.voidWrites the given text message to the WebSocket.voidwriteText(ReusableBufferList buffers) Writes the given byte array as a text message to the WebSocket.
-
Constructor Details
-
WebSocket
Constructs a new WebSocket instance with the given input and output streams.- Parameters:
in- the input stream for reading data from the WebSocketout- the output stream for writing data to the WebSocket
-
-
Method Details
-
setInterceptor
Sets the interceptor for WebSocket frames.- Parameters:
interceptor- the interceptor to set
-
isClosed
public boolean isClosed()Returns whether this WebSocket has been closed.- Returns:
- true if the WebSocket is closed, false otherwise
-
getLastWriteTime
public long getLastWriteTime()Returns the timestamp of the last write operation on this WebSocket.- Returns:
- the timestamp of the last write operation
-
writeText
Writes the given text message to the WebSocket.- Parameters:
message- the text message to write- Throws:
IOException- if an I/O error occurs
-
writeText
Writes the given text message to the WebSocket.- Parameters:
bytes- the text message to write.- Throws:
IOException- if an I/O error occurs
-
writeText
Writes the given byte array as a text message to the WebSocket.- Parameters:
buffers- one or more resuable buffers to write as a text frame- Throws:
IOException- if an I/O error occurs
-
write
Writes the given WebSocket frame to the output stream.- Parameters:
frame- the WebSocket frame to write- Throws:
IOException- if an I/O error occurs
-
writeClose
Writes a WebSocket close frame with the given status code.- Parameters:
code- the status code for the close frame- Throws:
IOException- if an I/O error occurs
-
writePing
Writes a WebSocket ping frame.- Throws:
IOException- if an I/O error occurs
-
writePong
Writes a WebSocket pong frame.- Throws:
IOException- if an I/O error occurs
-
writeSimpleOp
Writes a simple WebSocket frame with the given opcode and data.- Parameters:
op- the opcode for the framedata- the data for the frame- Throws:
IOException- if an I/O error occurs
-
readText
Reads a text message from the WebSocket.- Returns:
- the text message as a byte array
- Throws:
IOException- if an I/O error occurs
-
read
Reads a WebSocket frame from the input stream.- Returns:
- the WebSocket frame that was read
- Throws:
IOException- if an I/O error occurs
-
unmask
public static void unmask(byte[] payload, byte[] maskingKey) Unmasks a WebSocket payload.- Parameters:
payload- The masked data received from the client.maskingKey- The 4-byte masking key extracted from the WebSocket frame.
-
unmaskFast
public static void unmaskFast(byte[] payload, byte[] maskingKey) Unmasks a WebSocket payload using a fast 8-byte unrolling technique.- Parameters:
payload- The masked data received from the client.maskingKey- The 4-byte masking key extracted from the WebSocket frame.
-
close
Closes this WebSocket and its associated input and output streams.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if an I/O error occurs
-