Search in sources :

Example 1 with GameSessionSetupEvent

use of de.gg.event.GameSessionSetupEvent in project ProjektGG by eskalon.

the class NetworkHandler method setUpConnectionAsClient.

/**
 * Connects the client to the server. After it is finished a
 * {@link ConnectionEstablishedEvent} is posted on the
 * {@linkplain ProjektGG#getEventBus() event bus}.
 *
 * @param ip
 *            The server's ip.
 * @param port
 *            The server's port.
 */
public void setUpConnectionAsClient(String ip, int port) {
    client = new Client();
    client.start();
    NetworkRegisterer.registerClasses(client.getKryo());
    TypeListener listener = new TypeListener();
    // GAME SETUP MESSAGE (ON CLIENT CONNECT)
    listener.addTypeHandler(GameSetupMessage.class, (con, msg) -> {
        eventBus.post(new GameSessionSetupEvent(msg.getPlayers(), msg.getId(), msg.getSettings()));
        localClientId = msg.getId();
        Gdx.app.log("Client", "Netzwerk ID: " + localClientId);
    });
    // NEW CHAT MESSAGE
    listener.addTypeHandler(ChatMessageSentMessage.class, (con, msg) -> {
        eventBus.post(new NewChatMessagEvent(msg.getSenderId(), msg.getMessage()));
    });
    // PLAYER CHANGED
    listener.addTypeHandler(PlayerChangedMessage.class, (con, msg) -> {
        eventBus.post(new PlayerChangedEvent(msg.getId(), msg.getPlayer()));
    });
    // PLAYER JOINED
    listener.addTypeHandler(PlayerJoinedMessage.class, (con, msg) -> {
        eventBus.post(new PlayerConnectedEvent(msg.getId(), msg.getPlayer()));
    });
    // PLAYER LEFT
    listener.addTypeHandler(PlayerLeftMessage.class, (con, msg) -> {
        eventBus.post(new PlayerDisconnectedEvent(msg.getId()));
    });
    client.addListener(listener);
    Thread t = new Thread(new Runnable() {

        public void run() {
            IOException ex = null;
            try {
                client.connect(6000, ip, port);
                Gdx.app.log("Client", "Lobby beigetreten");
            } catch (IOException e) {
                ex = e;
                e.printStackTrace();
            }
            eventBus.post(new ConnectionEstablishedEvent(ex));
        }
    });
    t.start();
}
Also used : PlayerConnectedEvent(de.gg.event.PlayerConnectedEvent) ConnectionEstablishedEvent(de.gg.event.ConnectionEstablishedEvent) TypeListener(com.esotericsoftware.kryonet.Listener.TypeListener) GameSessionSetupEvent(de.gg.event.GameSessionSetupEvent) NewChatMessagEvent(de.gg.event.NewChatMessagEvent) PlayerChangedEvent(de.gg.event.PlayerChangedEvent) PlayerDisconnectedEvent(de.gg.event.PlayerDisconnectedEvent) IOException(java.io.IOException) Client(com.esotericsoftware.kryonet.Client)

Aggregations

Client (com.esotericsoftware.kryonet.Client)1 TypeListener (com.esotericsoftware.kryonet.Listener.TypeListener)1 ConnectionEstablishedEvent (de.gg.event.ConnectionEstablishedEvent)1 GameSessionSetupEvent (de.gg.event.GameSessionSetupEvent)1 NewChatMessagEvent (de.gg.event.NewChatMessagEvent)1 PlayerChangedEvent (de.gg.event.PlayerChangedEvent)1 PlayerConnectedEvent (de.gg.event.PlayerConnectedEvent)1 PlayerDisconnectedEvent (de.gg.event.PlayerDisconnectedEvent)1 IOException (java.io.IOException)1