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.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Closes this WebSocket and its associated input and output streams.
    long
    Returns the timestamp of the last write operation on this WebSocket.
    boolean
    Returns whether this WebSocket has been closed.
    Reads a WebSocket frame from the input stream.
    Reads a text message from the WebSocket.
    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 given byte array as a text message to the WebSocket.

    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
  • 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 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

      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 text message to write.
      Throws:
      IOException - if an I/O error occurs
    • writeText

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

      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.
      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
      Reads a text message from the WebSocket.
      Returns:
      the text message as a byte array
      Throws:
      IOException - if an I/O error occurs
    • read

      public WebSocketFrame read() throws IOException
      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

      public void close() throws IOException
      Closes this WebSocket and its associated input and output streams.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an I/O error occurs