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
Helps reading from a WebSocket and streaming responses.
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 writeText() or writeBinary() to send data to the client.
      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 when WebSocket connection is closed by the client.
    • write

      public void write(HttpRequest httpReq, OutputStream out) throws Exception
      Writes the HttpRequest object to the output stream as per HTTP spec.
      Overrides:
      write in class HttpResponse
      Parameters:
      httpReq - request to write to the output stream.
      out - the output stream to write the request.
      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