Correct node acknowledgement
This commit is contained in:
@@ -2,11 +2,34 @@ package io.github.lumijiez;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.json.JavalinGson;
|
||||
import io.javalin.websocket.WsContext;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class JavalinConfig {
|
||||
public static Gson gson = new Gson();
|
||||
private static final Map<String, WsContext> users = new ConcurrentHashMap<>();
|
||||
|
||||
public static void setup(Javalin app) {
|
||||
app.get("/check", ctx -> ctx.result("OK"));
|
||||
|
||||
app.ws("/discovery", ws -> {
|
||||
ws.onConnect(ctx -> {
|
||||
String id = ctx.sessionId();
|
||||
users.put(id, ctx);
|
||||
broadcast("Discovery", "Join");
|
||||
});
|
||||
|
||||
ws.onClose(ctx -> {
|
||||
String id = ctx.sessionId();
|
||||
users.remove(id);
|
||||
broadcast("Discovery", "Leave");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private static void broadcast(String sender, String message) {
|
||||
users.values().forEach(ctx -> ctx.send(users.size()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package io.github.lumijiez;
|
||||
|
||||
import io.javalin.Javalin;
|
||||
import io.javalin.json.JavalinGson;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Javalin app = Javalin.create().start(8083);
|
||||
Javalin app = Javalin.create(config -> {
|
||||
config.jsonMapper(new JavalinGson());
|
||||
}).start(8083);
|
||||
|
||||
JavalinConfig.setup(app);
|
||||
|
||||
System.out.print("Discovery service up and running");
|
||||
|
||||
Reference in New Issue
Block a user