Search in sources :

Example 1 with ConsoleOutput

use of com.almasb.fxgl.logging.ConsoleOutput 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();
}
Also used : ConsoleOutput(com.almasb.fxgl.logging.ConsoleOutput) Bundle(com.almasb.fxgl.core.serialization.Bundle) NetService(com.almasb.fxgl.net.NetService) LoggerConfig(com.almasb.fxgl.logging.LoggerConfig)

Aggregations

Bundle (com.almasb.fxgl.core.serialization.Bundle)1 ConsoleOutput (com.almasb.fxgl.logging.ConsoleOutput)1 LoggerConfig (com.almasb.fxgl.logging.LoggerConfig)1 NetService (com.almasb.fxgl.net.NetService)1