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
Helps reading from a WebSocket and streaming responses.
- 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 when WebSocket connection is closed by the client.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 HttpRequest object to the output stream as per HTTP spec.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 call writeText() or writeBinary() to send data to the client.- 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 when WebSocket connection is closed by the client. -
write
Writes the HttpRequest object to the output stream as per HTTP spec.- Overrides:
writein classHttpResponse- 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.
-