|
| 1 | +/* |
| 2 | + * Copyright 2000-2022 Vaadin Ltd. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.vaadin.base.devserver.viteproxy; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | +import java.net.http.HttpClient; |
| 20 | +import java.net.http.WebSocket; |
| 21 | +import java.net.http.WebSocket.Listener; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.concurrent.CompletionStage; |
| 24 | +import java.util.concurrent.ExecutionException; |
| 25 | +import java.util.function.Consumer; |
| 26 | + |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +import com.vaadin.base.devserver.ViteHandler; |
| 31 | + |
| 32 | +import jakarta.websocket.CloseReason.CloseCodes; |
| 33 | + |
| 34 | +/** |
| 35 | + * Communicates with a Vite server through a websocket connection. |
| 36 | + */ |
| 37 | +public class ViteWebsocketConnection implements Listener { |
| 38 | + |
| 39 | + private final Consumer<String> onMessage; |
| 40 | + private final WebSocket clientWebSocket; |
| 41 | + private final Runnable onClose; |
| 42 | + |
| 43 | + private Logger getLogger() { |
| 44 | + return LoggerFactory.getLogger(getClass()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Established a connection with a Vite server running on the given port, |
| 49 | + * using the given sub protocol. |
| 50 | + * |
| 51 | + * @param port |
| 52 | + * the port Vite is running on |
| 53 | + * @param subProtocol |
| 54 | + * the sub protocol to use |
| 55 | + * @param onMessage |
| 56 | + * a callback to invoke when a message arrives. |
| 57 | + * @param onClose |
| 58 | + * a callback to invoke if the connection to Vite is closed |
| 59 | + * |
| 60 | + * @throws InterruptedException |
| 61 | + * if there is a problem with the connection |
| 62 | + * @throws ExecutionException |
| 63 | + * if there is a problem with the connection |
| 64 | + */ |
| 65 | + public ViteWebsocketConnection(int port, String subProtocol, |
| 66 | + Consumer<String> onMessage, Runnable onClose) |
| 67 | + throws InterruptedException, ExecutionException { |
| 68 | + this.onMessage = onMessage; |
| 69 | + this.onClose = onClose; |
| 70 | + String wsHost = ViteHandler.DEV_SERVER_HOST.replace("http://", "ws://"); |
| 71 | + URI uri = URI.create(wsHost + ":" + port + "/VAADIN/"); |
| 72 | + clientWebSocket = HttpClient.newHttpClient().newWebSocketBuilder() |
| 73 | + .subprotocols(subProtocol).buildAsync(uri, this).get(); |
| 74 | + getLogger().debug("Connecting to {} using the {} protocol", uri, |
| 75 | + clientWebSocket.getSubprotocol()); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onOpen(WebSocket webSocket) { |
| 80 | + getLogger().debug("Connected using the {} protocol", |
| 81 | + webSocket.getSubprotocol()); |
| 82 | + |
| 83 | + Listener.super.onOpen(webSocket); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public CompletionStage<?> onClose(WebSocket webSocket, int statusCode, |
| 88 | + String reason) { |
| 89 | + onClose.run(); |
| 90 | + return Listener.super.onClose(webSocket, statusCode, reason); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, |
| 95 | + boolean last) { |
| 96 | + // Message from Vite |
| 97 | + String msg = data.toString(); |
| 98 | + getLogger().debug("Message from Vite: {}", msg); |
| 99 | + onMessage.accept(msg); |
| 100 | + return Listener.super.onText(webSocket, data, last); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Sends the given message to the Vite server. |
| 105 | + * |
| 106 | + * @param message |
| 107 | + * the message to send |
| 108 | + * @throws InterruptedException |
| 109 | + * if there is a problem with the connection |
| 110 | + * @throws ExecutionException |
| 111 | + * if there is a problem with the connection |
| 112 | + */ |
| 113 | + public void send(String message) |
| 114 | + throws InterruptedException, ExecutionException { |
| 115 | + CompletableFuture<WebSocket> send = clientWebSocket.sendText(message, |
| 116 | + false); |
| 117 | + send.get(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Closes the connection. |
| 122 | + * |
| 123 | + * @throws ExecutionException |
| 124 | + * if there is a problem with the connection |
| 125 | + * @throws InterruptedException |
| 126 | + * if there is a problem with the connection |
| 127 | + */ |
| 128 | + public void close() throws InterruptedException, ExecutionException { |
| 129 | + getLogger().debug("Closing the connection"); |
| 130 | + CompletableFuture<WebSocket> closeRequest = clientWebSocket |
| 131 | + .sendClose(CloseCodes.NORMAL_CLOSURE.getCode(), ""); |
| 132 | + closeRequest.get(); |
| 133 | + } |
| 134 | +} |
0 commit comments