1 /* 2 * Copyright (c) 2002-2025 Gargoyle Software Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * https://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 package org.htmlunit.websocket; 16 17 import java.io.IOException; 18 import java.net.URI; 19 20 /** 21 * Helper to have no direct dependency to the WebSockt client 22 * implementation used by HtmlUnit. 23 * 24 * @author Ronald Brill 25 */ 26 public interface WebSocketAdapter { 27 /** 28 * Starts the client. 29 * 30 * @throws Exception in case of error 31 */ 32 void start() throws Exception; 33 34 /** 35 * Connects to the given {@link URI}. 36 * 37 * @param url the target url 38 * @throws Exception in case of error 39 */ 40 void connect(URI url) throws Exception; 41 42 /** 43 * Sends the provided content. 44 * 45 * @param content the content to be sent 46 * @throws IOException in case of error 47 */ 48 void send(Object content) throws IOException; 49 50 /** 51 * Close the incomming session. 52 * 53 * @throws Exception in case of error 54 */ 55 void closeIncommingSession() throws Exception; 56 57 /** 58 * Close the outgoing session. 59 * 60 * @throws Exception in case of error 61 */ 62 void closeOutgoingSession() throws Exception; 63 64 /** 65 * Close the client. 66 * 67 * @throws Exception in case of error 68 */ 69 void closeClient() throws Exception; 70 71 /** 72 * Callback to be called when connecting. 73 */ 74 void onWebSocketConnecting(); 75 76 /** 77 * Callback to be called when connected. 78 */ 79 void onWebSocketConnect(); 80 81 /** 82 * Callback to be called when closed. 83 * 84 * @param statusCode the status code 85 * @param reason the reason 86 */ 87 void onWebSocketClose(int statusCode, String reason); 88 89 /** 90 * Callback to be called when closed. 91 * 92 * @param message the message 93 */ 94 void onWebSocketText(String message); 95 96 /** 97 * Callback to be called when binary data retrieved. 98 * 99 * @param data the bytes 100 * @param offset start offset 101 * @param length the length 102 */ 103 void onWebSocketBinary(byte[] data, int offset, int length); 104 105 /** 106 * Callback to be called on connect error. 107 * 108 * @param cause the cause 109 */ 110 void onWebSocketConnectError(Throwable cause); 111 112 /** 113 * Callback to be called on error. 114 * 115 * @param cause the cause 116 */ 117 void onWebSocketError(Throwable cause); 118 }