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
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 Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe default timeout in milliseconds.protected WebSocketThe underlying WebSocket connection.Fields inherited from class net.targetr.wtm3.net.http.HttpResponse
bytesWritten, code, headerLength, headers, info, version, writeErrorHandler -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new WebSocketResponse instance for maintaining a WebSocket connection with a client. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidonClose()Called once when the WebSocket transport is closed, from the reader, writer, or graceful-close timeout thread.protected abstract voidonReceiveText(ReusableBufferList buffers) Called when text is received from the WebSocket.voidrun()Executes the background thread that continuously reads frames from the WebSocket and forwards them to the onReceiveText callback until the connection is closed.voidsetInterceptor(WebSocketFrameInterceptor interceptor) Sets the interceptor for WebSocket frames.voidwrite(HttpRequest httpReq, OutputStream out) Writes the upgrade response, starts the reader and runswriteResponse().protected abstract voidCalled when WebSocket connection is first opened.Methods inherited from class net.targetr.wtm3.net.http.HttpResponse
getBodyInputStream, getContentLength, parseResponse, toString
-
Field Details
-
TIMEOUT
public static final int TIMEOUTThe default timeout in milliseconds.- See Also:
-
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
Sets the interceptor for WebSocket frames.- Parameters:
interceptor- the interceptor to set
-
writeResponse
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 callWebSocket.writeText(String)throughwebSocketto send data.- Throws:
Exception- If an error occurs while responding to the client.
-
onReceiveText
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
Writes the upgrade response, starts the reader and runswriteResponse().- Overrides:
writein classHttpResponse- Parameters:
httpReq- the client's WebSocket upgrade requestout- 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.
-