Skip to content

Commit bb3178e

Browse files
committed
UI-Server: Avoid ipv4 trickery.
We're just going to bind to localhost and let that auto-resolve to v4 or v6.
1 parent 792bf74 commit bb3178e

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

examples/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
<version>${project.parent.version}</version>
3131
<scope>compile</scope>
3232
</dependency>
33+
<dependency>
34+
<groupId>dev.webview.webview_java</groupId>
35+
<artifactId>ui-server</artifactId>
36+
<version>${project.parent.version}</version>
37+
<scope>compile</scope>
38+
</dependency>
3339

3440
<dependency>
3541
<groupId>org.openjfx</groupId>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package dev.webview.webview_java.example.direct;
2+
3+
import java.io.IOException;
4+
5+
import co.casterlabs.rhs.protocol.StandardHttpStatus;
6+
import co.casterlabs.rhs.server.HttpResponse;
7+
import dev.webview.webview_java.Webview;
8+
import dev.webview.webview_java.uiserver.UIServer;
9+
10+
public class ExampleUIServer {
11+
12+
public static void main(String[] args) throws IOException {
13+
Webview wv = new Webview(true); // Can optionally be created with an AWT component to be painted on.
14+
UIServer server = new UIServer();
15+
16+
server.setHandler((session) -> {
17+
return HttpResponse.newFixedLengthResponse(StandardHttpStatus.OK, "Hello world!")
18+
.setMimeType("text/plain");
19+
});
20+
21+
server.start();
22+
23+
// Calling `await echo(1,2,3)` will return `[1,2,3]`
24+
wv.bind("echo", (arguments) -> {
25+
return arguments;
26+
});
27+
28+
wv.setTitle("My Webview App");
29+
// wv.setSize(800, 600);
30+
31+
// load a URL
32+
wv.loadURL(server.getLocalAddress());
33+
34+
/*
35+
36+
Or, load raw html from a file with:
37+
wv.setHTML("<h1>This is a test!<h1>");
38+
39+
String htmlContent = loadContentFromFile("index.html");
40+
wv.setHTML(htmlContent);
41+
42+
*/
43+
44+
wv.run(); // Run the webview event loop, the webview is fully disposed when this returns.
45+
wv.close(); // Free any resources allocated.
46+
server.close();
47+
}
48+
49+
}

ui-server/src/main/java/dev/webview/webview_java/uiserver/UIServer.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.io.IOException;
2828
import java.net.InetSocketAddress;
2929
import java.net.ServerSocket;
30-
import java.util.concurrent.ThreadLocalRandom;
3130
import java.util.function.Function;
3231

3332
import org.jetbrains.annotations.Nullable;
@@ -60,10 +59,7 @@ public class UIServer implements Closeable {
6059

6160
@SneakyThrows
6261
public UIServer() {
63-
String hostname = "127"
64-
+ '.' + ThreadLocalRandom.current().nextInt(256)
65-
+ '.' + ThreadLocalRandom.current().nextInt(256)
66-
+ '.' + ThreadLocalRandom.current().nextInt(256);
62+
String hostname = "localhost";
6763

6864
// Find a random port.
6965
try (ServerSocket serverSocket = new ServerSocket()) {

0 commit comments

Comments
 (0)