Intial commit
This commit is contained in:
parent
7329fb8dea
commit
73f201bdac
268 changed files with 11220 additions and 5 deletions
16
no2all-wire-jre/pom.xml
Normal file
16
no2all-wire-jre/pom.xml
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>love.distributedrebirth.no2all</groupId>
|
||||
<artifactId>no2all</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>no2all-wire-jre</artifactId>
|
||||
<name>No2All-Wire-Jre</name>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>no2all-wire</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
package love.distributedrebirth.no2all.wire.jre;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.WebSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import love.distributedrebirth.no2all.wire.WireClient;
|
||||
import love.distributedrebirth.no2all.wire.WireClientHandler;
|
||||
|
||||
public final class JreWireClient implements WireClient {
|
||||
|
||||
private final URI uri;
|
||||
private final WebSocket.Builder wsBuilder;
|
||||
private final WireClientHandler handler;
|
||||
private WebSocket webSocket;
|
||||
|
||||
protected JreWireClient(URI uri, WebSocket.Builder wsBuilder, WireClientHandler handler) {
|
||||
this.uri = uri;
|
||||
this.wsBuilder = wsBuilder;
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connect() {
|
||||
try {
|
||||
webSocket = wsBuilder.buildAsync(uri, new JreWireClientHandler(handler)).get();
|
||||
} catch (InterruptedException | ExecutionException error) {
|
||||
handler.onError(error); // handles: Connection refused
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(int code, String message) {
|
||||
if (webSocket != null) {
|
||||
webSocket.sendClose(code, message);
|
||||
webSocket = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String message) {
|
||||
if (webSocket != null) {
|
||||
webSocket.sendText(message, true);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendBinary(ByteBuffer message) {
|
||||
if (webSocket != null) {
|
||||
webSocket.sendBinary(message, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
package love.distributedrebirth.no2all.wire.jre;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.WebSocket;
|
||||
|
||||
import love.distributedrebirth.no2all.wire.WireClient;
|
||||
import love.distributedrebirth.no2all.wire.WireClientEndpoint;
|
||||
import love.distributedrebirth.no2all.wire.WireClientHandler;
|
||||
|
||||
public final class JreWireClientEndpoint implements WireClientEndpoint {
|
||||
|
||||
private final URI uri;
|
||||
private final WebSocket.Builder wsBuilder;
|
||||
|
||||
public JreWireClientEndpoint(URI uri, WebSocket.Builder wsBuilder) {
|
||||
this.wsBuilder = wsBuilder;
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String wireId() {
|
||||
return uri.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public WireClient wireClient(WireClientHandler handler) {
|
||||
return new JreWireClient(uri, wsBuilder, handler);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
package love.distributedrebirth.no2all.wire.jre;
|
||||
|
||||
import java.net.http.WebSocket;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
|
||||
import love.distributedrebirth.no2all.wire.WireClientHandler;
|
||||
|
||||
public final class JreWireClientHandler implements WebSocket.Listener {
|
||||
|
||||
private final WireClientHandler handler;
|
||||
|
||||
protected JreWireClientHandler(WireClientHandler handler) {
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(WebSocket ws) {
|
||||
handler.onOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onClose(WebSocket ws, int code, String reason) {
|
||||
handler.onClose(code, reason, true);
|
||||
return CompletableFuture.completedFuture("");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(WebSocket ws, Throwable error) {
|
||||
handler.onError(error);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onText(WebSocket ws, CharSequence data, boolean last) {
|
||||
handler.onMessage(data.toString());
|
||||
return CompletableFuture.completedFuture("");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onBinary(WebSocket ws, ByteBuffer data, boolean last) {
|
||||
handler.onBinary(data);
|
||||
return CompletableFuture.completedFuture("");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue