Search in sources :

Example 1 with Connect

use of io.anuke.mindustry.net.Packets.Connect in project Mindustry by Anuken.

the class WebsocketClient method connect.

@Override
public void connect(String ip, int port) {
    socket = new Websocket("ws://" + ip + ":" + webPort);
    socket.addListener(new WebsocketListener() {

        public void onMessage(byte[] bytes) {
            try {
                ByteBuffer buffer = ByteBuffer.wrap(bytes);
                byte id = buffer.get();
                if (id != -2) {
                    // ignore framework messages
                    Class<?> type = Registrator.getByID(id);
                    Packet packet = (Packet) ClassReflection.newInstance(type);
                    packet.read(buffer);
                    Net.handleClientReceived(packet);
                }
            } catch (ReflectionException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public void onClose() {
            Disconnect disconnect = new Disconnect();
            Net.handleClientReceived(disconnect);
        }

        @Override
        public void onMessage(String msg) {
            onMessage(Base64Coder.decode(msg));
        }

        @Override
        public void onOpen() {
            Connect connect = new Connect();
            Net.handleClientReceived(connect);
        }
    });
    socket.open();
}
Also used : ReflectionException(com.badlogic.gdx.utils.reflect.ReflectionException) Disconnect(io.anuke.mindustry.net.Packets.Disconnect) Websocket(com.sksamuel.gwt.websockets.Websocket) Connect(io.anuke.mindustry.net.Packets.Connect) WebsocketListener(com.sksamuel.gwt.websockets.WebsocketListener) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ReflectionException (com.badlogic.gdx.utils.reflect.ReflectionException)1 Websocket (com.sksamuel.gwt.websockets.Websocket)1 WebsocketListener (com.sksamuel.gwt.websockets.WebsocketListener)1 Connect (io.anuke.mindustry.net.Packets.Connect)1 Disconnect (io.anuke.mindustry.net.Packets.Disconnect)1 ByteBuffer (java.nio.ByteBuffer)1