Simple session structure
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package dev.selimaj.session;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
|
||||
import dev.selimaj.session.types.Message;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.WebSocket;
|
||||
@@ -8,12 +11,10 @@ import java.net.http.WebSocket;
|
||||
public final class Session {
|
||||
|
||||
private final WebSocket ws;
|
||||
private final ExecutorService executor;
|
||||
private final SessionListener listener;
|
||||
|
||||
public Session(WebSocket ws, SessionListener listener, ExecutorService executor) {
|
||||
public Session(WebSocket ws, SessionListener listener) {
|
||||
this.ws = ws;
|
||||
this.executor = executor;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@@ -22,16 +23,29 @@ public final class Session {
|
||||
|
||||
WebSocket ws = HttpClient.newHttpClient()
|
||||
.newWebSocketBuilder()
|
||||
.buildAsync(URI.create("ws://example.com/echo"), listener)
|
||||
.buildAsync(URI.create(uri), listener)
|
||||
.join();
|
||||
|
||||
return new Session(ws, listener, Executors.newCachedThreadPool());
|
||||
return new Session(ws, listener);
|
||||
}
|
||||
|
||||
public void startReceiver() {
|
||||
executor.execute(() -> {
|
||||
|
||||
});
|
||||
public void send(Message msg) throws Exception {
|
||||
listener.send(ws, msg);
|
||||
}
|
||||
|
||||
public void respond(int to, JsonNode val) throws Exception {
|
||||
listener.respond(ws, to, val);
|
||||
}
|
||||
|
||||
public void respondError(int to, JsonNode val) throws Exception {
|
||||
listener.respondError(ws, to, val);
|
||||
}
|
||||
|
||||
public void notify(String method, Object data) throws Exception {
|
||||
listener.notify(ws, method, data);
|
||||
}
|
||||
|
||||
public void close() throws Exception {
|
||||
ws.sendClose(0, "Session closed");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user