mirror of
https://github.com/PhotonVision/photonvision
synced 2026-06-20 00:51:41 +00:00
added website and web socket handler
This commit is contained in:
@@ -1,9 +1,38 @@
|
||||
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().start(port);
|
||||
app.get("/", ctx -> ctx.result("Hello World"));
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
package Handlers.Web;
|
||||
|
||||
public class Socket {
|
||||
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package Handlers.Web;
|
||||
|
||||
public class Web {
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
import Handlers.Web.Server;
|
||||
public class Main {
|
||||
public static void main(String [] args) {
|
||||
|
||||
Server.main(8888);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user