use of com.almasb.fxgl.net.NetService in project FXGL by AlmasB.
the class UDPSample method main.
public static void main(String[] args) throws Exception {
Logger.configure(new LoggerConfig());
Logger.addOutput(new ConsoleOutput(), LoggerLevel.DEBUG);
var server = new NetService().newUDPServer(55555);
server.setOnConnected(connection -> {
connection.addMessageHandler((conn, message) -> {
System.out.println("Server got: " + message);
if (message.getName().equals("Bye!!!")) {
System.out.println("Stopping now");
server.stop();
}
});
});
var client = new NetService().newUDPClient("localhost", 55555);
client.setOnConnected(connection -> {
connection.addMessageHandler((conn, message) -> {
System.out.println("Client got: " + message);
conn.send(new Bundle("Bye!!!"));
client.disconnect();
});
});
new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Client connecting");
client.connectTask().run();
}).start();
new Thread(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Broadcasting hello");
server.broadcast(new Bundle("HELLO!"));
}).start();
server.startTask().run();
}
Aggregations