Package com.esotericsoftware.kryonet

Examples of com.esotericsoftware.kryonet.Server


    /**
     * @param args the command line arguments
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        final Server server = new Server();
        server.start();
        server.bind(12_345);

        final Client client = new Client();
        client.start();
        client.connect(5_000, "localhost", 12_345);

        client.addListener(new Listener() {
            @Override
            public void received(Connection connection, Object object) {
                LOG.log(Level.INFO, "Received message {0} in thread {1}", new Object[]{object.toString(), Thread.currentThread().toString()});
                try {
                    Thread.sleep(100);
                } catch (InterruptedException ex) {
                    LOG.log(Level.SEVERE, null, ex);
                }
            }
        });

        final String text = "test";

        final Timer timer = new Timer("timer 1");
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                server.sendToAllTCP(text);
            }
        }, 0, 100);

        final Timer timer2 = new Timer("timer 2");
        timer2.schedule(new TimerTask() {
            @Override
            public void run() {
                LOG.log(Level.INFO, "cancel everything");
                timer.cancel();
                client.stop();
                server.stop();
                timer2.cancel();
            }
        }, 2_000);
    }
View Full Code Here


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        final Server server = new Server();
        Kryo kryo = server.getKryo();
        kryo.register(Packet.class);
       
        server.start();
        server.bind(12_345);
       
        final Client client = new Client();
        kryo = client.getKryo();
        kryo.register(Packet.class);       
       
        client.start();
        client.connect(5_000, "localhost", 12_345);       
       
        client.addListener(new Listener() {
            @Override
            public void received(Connection connection, Object object) {
                System.out.println("type of object " + object.getClass().getName());
                if (object instanceof Packet) {
                    System.out.println("type of content " + ((Packet) object).getContent().getClass().getName());
                }
            }
        });       
       
        server.sendToAllTCP(new Packet<String>("Hello world!"));
       

        // stopping timer after 2s
        final Timer timer = new Timer("timer");
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                timer.cancel();
                client.stop();
                server.stop();
            }
        }, 200_000);       
       
    }
View Full Code Here

     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {

        // start server and add listener
        final Server server = new Server();
        server.start();
        server.bind(12_345);
        LogListener listener = new LogListener();
        server.addListener(listener);

        // start and connect client
        final Client client = new Client();
        client.start();
        client.connect(1_000, "localhost", 12_345);

        // send some messages
        client.sendTCP("hallo");
        client.sendTCP("bye");

        // force disconnect
        listener.disconnect(client);


        // set timer and stop after 5s
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                timer.cancel();
                client.stop();
                server.stop();
            }
        }, 5_000);
    }
View Full Code Here

TOP

Related Classes of com.esotericsoftware.kryonet.Server

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.