Class WebSocket

java.lang.Object
net.targetr.wtm3.net.http.websocket.WebSocket
All Implemented Interfaces:
Closeable, AutoCloseable

public class WebSocket extends Object implements Closeable
Reads and writes data frames to a WebSocket.
Author:
Dr Michael Gardiner
  • Constructor Summary

    Constructors
    Constructor
    Description
    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 Type
    Method
    Description
    void
    Aborts this WebSocket without waiting for an active frame writer.
    void
    Closes the connection using the independent abort path.
    long
    Returns the timestamp of the last write operation on this WebSocket.
    boolean
    Returns whether a close frame has been sent or the connection has been aborted.
    Reads and unmasks a frame, enforcing the configured frame-size limit.
    Collects text and continuation frames into one message, enforcing the configured message-size limit.
    void
    Sets the interceptor for WebSocket frames.
    static void
    unmask(byte[] payload, byte[] maskingKey)
    Unmasks a WebSocket payload.
    static void
    unmaskFast(byte[] payload, byte[] maskingKey)
    Unmasks a WebSocket payload using a fast 8-byte unrolling technique.
    void
    Writes the given WebSocket frame to the output stream.
    void
    writeClose(int code)
    Writes a WebSocket close frame with the given status code.
    void
    Writes a WebSocket ping frame.
    void
    Writes a WebSocket pong frame.
    void
    writeSimpleOp(int op, byte[] data)
    Writes a simple WebSocket frame with the given opcode and data.
    void
    writeText(byte[] bytes)
    Writes the given text message to the WebSocket.
    void
    writeText(String message)
    Writes the given text message to the WebSocket.
    void
    Writes the supplied buffers as one text frame.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WebSocket

      public WebSocket(InputStream in, OutputStream out)
      Constructs a new WebSocket instance with the given input and output streams.
      Parameters:
      in - the input stream for reading data from the WebSocket
      out - the output stream for writing data to the WebSocket
    • WebSocket

      public WebSocket(InputStream in, OutputStream out, Closeable abortTransport)
      Constructs a WebSocket with an independent transport abort operation.
      Parameters:
      in - the input stream
      out - the output stream
      abortTransport - closes the underlying transport without acquiring stream locks or flushing output, and notifies the connection owner
  • Method Details

    • setInterceptor

      public void setInterceptor(WebSocketFrameInterceptor interceptor)
      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

      public void writeText(String message) throws IOException
      Writes the given text message to the WebSocket.
      Parameters:
      message - the text message to write
      Throws:
      IOException - if an I/O error occurs
    • writeText

      public void writeText(byte[] bytes) throws IOException
      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

      public void writeText(ReusableBufferList buffers) throws IOException
      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

      public void write(WebSocketFrame frame) throws IOException
      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

      public void writeClose(int code) throws IOException
      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

      public void writePing() throws IOException
      Writes a WebSocket ping frame.
      Throws:
      IOException - if an I/O error occurs
    • writePong

      public void writePong() throws IOException
      Writes a WebSocket pong frame.
      Throws:
      IOException - if an I/O error occurs
    • writeSimpleOp

      public void writeSimpleOp(int op, byte[] data) throws IOException
      Writes a simple WebSocket frame with the given opcode and data.
      Parameters:
      op - the opcode for the frame
      data - the data for the frame
      Throws:
      IOException - if an I/O error occurs
    • readText

      public ReusableBufferList readText() throws IOException
      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

      public WebSocketFrame read() throws IOException
      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 frame
      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.
    • abort

      public void abort() throws IOException
      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

      public void close() throws IOException
      Closes the connection using the independent abort path.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if closing the transport fails