Class WebSocketResponse

java.lang.Object
net.targetr.wtm3.net.http.HttpResponse
net.targetr.wtm3.net.http.websocket.WebSocketResponse
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
HttpRequestMonitoringWebSocketResponse, WebSocketFrameMonitoringWebSocketResponse

public abstract class WebSocketResponse extends HttpResponse implements Runnable
Performs the HTTP upgrade, reads text on a virtual thread and runs the writer on the response thread. Either side finishing aborts the transport.
Author:
Dr Michael Gardiner
  • Field Details

    • TIMEOUT

      public static final int TIMEOUT
      The default timeout in milliseconds.
      See Also:
    • webSocket

      protected WebSocket webSocket
      The underlying WebSocket connection.
  • Constructor Details

    • WebSocketResponse

      public WebSocketResponse()
      Creates a new WebSocketResponse instance for maintaining a WebSocket connection with a client.
  • Method Details

    • setInterceptor

      public void setInterceptor(WebSocketFrameInterceptor interceptor)
      Sets the interceptor for WebSocket frames.
      Parameters:
      interceptor - the interceptor to set
    • writeResponse

      protected abstract void writeResponse() throws Exception
      Called when WebSocket connection is first opened. This method is called once and should not return until all communication with the client is complete. Implementations of this method will typically call WebSocket.writeText(String) through webSocket to send data.
      Throws:
      Exception - If an error occurs while responding to the client.
    • onReceiveText

      protected abstract void onReceiveText(ReusableBufferList buffers) throws Exception
      Called when text is received from the WebSocket.
      Parameters:
      buffers - The raw data received from the WebSocket as a buffer list
      Throws:
      Exception - If an error occurs while processing the received text.
    • onClose

      protected abstract void onClose()
      Called once when the WebSocket transport is closed, from the reader, writer, or graceful-close timeout thread.
    • write

      public void write(HttpRequest httpReq, OutputStream out) throws Exception
      Writes the upgrade response, starts the reader and runs writeResponse().
      Overrides:
      write in class HttpResponse
      Parameters:
      httpReq - the client's WebSocket upgrade request
      out - the stream for the upgrade response and outgoing frames
      Throws:
      Exception - If an exception occurs while writing data to the socket.
    • run

      public void run()
      Executes the background thread that continuously reads frames from the WebSocket and forwards them to the onReceiveText callback until the connection is closed.
      Specified by:
      run in interface Runnable