ClienstStateListener don't fire clientConncted event

Hi there,

this is my second post here, so I am very new to here. Also to jME.

I’ve read the Beginner’s Guide Book for jME and I am currently on the topic “network”. So I have a problem which i can’t solve and can’t understand why this problem appear.

The server for the network is running without problems. When I am going to connect to the server with a client the ClientSateListener should fire “clienctConnected” event. But it doesn’t.

Here is my Code:

[java]
package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.network.Client;
import com.jme3.network.ClientStateListener;
import com.jme3.network.Message;
import com.jme3.network.Network;
import com.jme3.network.serializing.Serializer;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.system.JmeContext;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/** This package contains an example that shows

  • how you enqueue changes to the scene graph

  • correctly from the network thread – see ClientListener. */
    public class ClientMain extends SimpleApplication implements ClientStateListener {

    private Client myClient;
    private static final Logger logger = Logger.getLogger(ClientMain.class.getName());

    public static void main(String[] args) {
    logger.setLevel(Level.WARNING);
    ClientMain app = new ClientMain();
    app.start(JmeContext.Type.Display);
    }

    @Override
    public void simpleInitApp() {
    try {
    myClient = Network.connectToServer(“My Cool Game”, 1, “localhost”, 6143);
    myClient.start();
    logger.log(Level.WARNING, “Client started.”);
    } catch (IOException ex) {
    }
    Serializer.registerClass(CubeMessage.class);
    myClient.addMessageListener(new ClientListener(this,myClient),CubeMessage.class);
    myClient.addClientStateListener(this);

     attachCube("One Cube");
    

    }

    /* Add some demo content */
    public void attachCube(String name) {
    Box box = new Box(1,1,1);
    Geometry geom = new Geometry(name, box);
    Material mat = new Material(assetManager,
    “Common/MatDefs/Misc/Unshaded.j3md”);
    mat.setColor(“Color”, ColorRGBA.White);
    geom.setMaterial(mat);
    rootNode.attachChild(geom);
    }

    @Override
    public void destroy() {
    try {
    myClient.close();
    logger.log(Level.WARNING, “Client closed.”);
    } catch (Exception ex) {
    }
    super.destroy();
    }

    /** Specify what happens when this client connects to server /
    public void clientConnected(Client client) {
    logger.log(Level.WARNING, “Fully connected.”);
    /
    example for client-server communication that changes the scene graph */
    Message m = new CubeMessage();
    myClient.send(m);
    }

    /** Specify what happens when this client disconnects from server */
    public void clientDisconnected(Client client, DisconnectInfo info) {
    logger.log(Level.WARNING, “Fully disconnected.”);
    }
    }
    [/java]

Anyone have an idea what is wrong?

The other log message appear like they should, even the “fully disconnected”.

Here is what you’ve done written out… see if you can spot the error:

  1. connect to server, ie: establish a connection to the server (this is what start() is doing)
  2. add a listener to see when we are connected to the server

Spot the issue?

1 Like

Lol. I think I know what you mean.

I should first register the listener then start the client. Would be the logical way. :smiley:

Ty if this was my mistake.

@Sonnenspeer said: Lol. I think I know what you mean.

I should first register the listener then start the client. Would be the logical way. :smiley:

Ty if this was my mistake.

Yep. You can’t get notified about the connection if it has already happened before you add the listener. It’s like playing the winning lottery numbers a day late. :slight_smile:

Hehe. Thank you for the help.

Just wanted to say that this solved the problem. :slight_smile: