mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
39 lines
1.1 KiB
Java
39 lines
1.1 KiB
Java
package Handlers.Web;
|
|
|
|
import io.javalin.Javalin;
|
|
import io.javalin.websocket.WsContext;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import org.json.JSONObject;
|
|
|
|
public class Server {
|
|
private static List<WsContext> users = new ArrayList<WsContext>();
|
|
public static void main(int port) {
|
|
Javalin app = Javalin.create();
|
|
app.config.addStaticFiles("web");
|
|
app.ws("/websocket", ws ->{
|
|
ws.onConnect(ctx -> {
|
|
users.add(ctx);
|
|
System.out.println("Socket Connected");
|
|
});
|
|
ws.onClose(ctx -> {
|
|
users.remove(ctx);
|
|
System.out.println("Socket Disconnected");
|
|
});
|
|
ws.onMessage(ctx -> {
|
|
broadcastMessage(ctx, ctx.message());
|
|
});
|
|
});
|
|
app.start(port);
|
|
}
|
|
private static void broadcastMessage(WsContext sendingUser, String message){
|
|
for (var user : users)
|
|
{
|
|
if (user != sendingUser){
|
|
user.send(message);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|