2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.webthing.internal.client;
15 import java.io.IOException;
17 import java.time.Duration;
18 import java.util.concurrent.ScheduledExecutorService;
19 import java.util.function.Consumer;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jetty.websocket.client.WebSocketClient;
25 * Factory to create new instances of a WebSocket connection
27 * @author Gregor Roth - Initial contribution
30 interface WebSocketConnectionFactory {
33 * create (and opens) a new WebSocket connection
35 * @param webSocketURI the websocket uri
36 * @param executor the executor to use
37 * @param errorHandler the error handler
38 * @param pingPeriod the ping period to check the healthiness of the connection
39 * @return the newly opened WebSocket connection
40 * @throws IOException if the web socket connection can not be established
42 WebSocketConnection create(URI webSocketURI, ScheduledExecutorService executor, Consumer<String> errorHandler,
43 Duration pingPeriod) throws IOException;
46 * @param webSocketClient the web socket client to use
47 * @return the default instance of the factory
49 static WebSocketConnectionFactory instance(WebSocketClient webSocketClient) {
50 return (webSocketURI, executor, errorHandler, pingPeriod) -> {
51 var webSocketConnection = new WebSocketConnectionImpl(executor, errorHandler, pingPeriod);
52 webSocketClient.connect(webSocketConnection, webSocketURI);
53 return webSocketConnection;