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.WebSocket(InputStream in, OutputStream out, Closeable abortTransport) Constructs a WebSocket with an independent transport abort operation. -
Method Summary
Modifier and TypeMethodDescriptionvoidabort()Aborts this WebSocket without waiting for an active frame writer.voidclose()Closes the connection using the independent abort path.longReturns the timestamp of the last write operation on this WebSocket.booleanisClosed()Returns whether a close frame has been sent or the connection has been aborted.read()Reads and unmasks a frame, enforcing the configured frame-size limit.readText()Collects text and continuation frames into one message, enforcing the configured message-size limit.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 supplied buffers as one text frame.
-
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
-
WebSocket
Constructs a WebSocket with an independent transport abort operation.- Parameters:
in- the input streamout- the output streamabortTransport- closes the underlying transport without acquiring stream locks or flushing output, and notifies the connection owner
-
-
Method Details
-
setInterceptor
Sets the interceptor for WebSocket frames.- Parameters:
interceptor- the interceptor to set
-
isClosed
public boolean isClosed()Returns whether a close frame has been sent or the connection has been aborted.- 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 last successful frame write in
Clock.realtime()milliseconds, or zero before the first write
-
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 UTF-8 encoded text message- Throws:
IOException- if an I/O error occurs
-
writeText
Writes the supplied buffers as one text frame.- Parameters:
buffers- reusable buffers containing UTF-8 encoded text- 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. Aborts the transport if the close frame cannot be sent within one second.- 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
Collects text and continuation frames into one message, enforcing the configured message-size limit. Other returned frame types are skipped.- Returns:
- the message bytes as a buffer list
- Throws:
IOException- if an I/O error occurs
-
read
Reads and unmasks a frame, enforcing the configured frame-size limit. Ping frames are answered and skipped; close frames are answered and end the read.- Returns:
- the WebSocket frame that was read
- Throws:
EOFException- if the peer closes the connection or sends a close frameIOException- 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.
-
abort
Aborts this WebSocket without waiting for an active frame writer. Transport-backed instances close the transport before notifying their owner. Stream-only instances use their streams' close operations.- Throws:
IOException- if an I/O error occurs
-
close
Closes the connection using the independent abort path.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException- if closing the transport fails
-