Client to server message problem

I’m having problems sending messages from the client to server, and I just realize that I haven’t tried sending messages from server to client… so there’s a chance that’s not working either. The message leaves the client but the server isn’t receiving it… :frowning:



I saw there was a topic with the same problem, but it was due to the fact he was using the old version. I’m using the new version of the api (I hope). So would somebody be kind enough to check out my code?

thx



Client:

[java]

package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.network.Client;

import com.jme3.network.ClientStateListener;

import com.jme3.network.Message;

import com.jme3.network.MessageListener;

import com.jme3.network.Network;

import com.jme3.network.NetworkClient;

import com.jme3.renderer.RenderManager;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;



public class Main extends SimpleApplication implements MessageListener, ClientStateListener {



private NetworkClient client;



public static void main(String[] args) {

Main app = new Main();

app.start();

}



@Override

public void simpleInitApp() {

client = Network.createClient();



try {

client.connectToServer(“127.0.0.1”, 5050, 5050);

client.start();

} catch (IOException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}



listener();



}



@Override

public void simpleUpdate(float tpf) {

//TODO: add update code

}



@Override

public void simpleRender(RenderManager rm) {

//TODO: add render code

}



private void listener() {

client.addClientStateListener(this);

client.addMessageListener(this, ChatMessage.class);

}



public void messageReceived(Object source, Message m) {



}



public void clientConnected(Client c) {

client.send(new ChatMessage(“jeroen”, “chat message text”));

Logger.getLogger(Main.class.getName()).log(Level.INFO, “chatmessage sent”);

}



public void clientDisconnected(Client c, DisconnectInfo info) {



}

}

[/java]



server:

[java]package mygame;



import com.jme3.app.SimpleApplication;

import com.jme3.network.ConnectionListener;

import com.jme3.network.HostedConnection;

import com.jme3.network.Message;

import com.jme3.network.MessageListener;

import com.jme3.network.Network;

import com.jme3.network.Server;

import com.jme3.renderer.RenderManager;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;



public class Main extends SimpleApplication implements MessageListener<HostedConnection>, ConnectionListener {



private Server server;



public static void main(String[] args) {

Main app = new Main();

app.start();

}



@Override

public void simpleInitApp() {

try {

//Starten van server

server = Network.createServer(5050);

server.start();

} catch (IOException ex) {

Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);

}



listener();

}



@Override

public void simpleUpdate(float tpf) {

//TODO: add update code

}



@Override

public void simpleRender(RenderManager rm) {

//TODO: add render code

}



private void listener() {

server.addConnectionListener(this);

server.addMessageListener(this, ChatMessage.class);

}



public void messageReceived(HostedConnection source, Message m) {

if(m instanceof ChatMessage){

Logger.getLogger(Main.class.getName()).log(Level.INFO, null, “recieved chat message”);

}

}



public void connectionAdded(Server server, HostedConnection conn) {

Logger.getLogger(Main.class.getName()).log(Level.INFO, null, “Client joined”);

}



public void connectionRemoved(Server server, HostedConnection conn) {

Logger.getLogger(Main.class.getName()).log(Level.INFO, null, “Cliened removed”);

}

}

[/java]



ChatMessage:

[java]

package mygame;



import com.jme3.network.AbstractMessage;

import com.jme3.network.serializing.Serializable;

@Serializable()

public class ChatMessage extends AbstractMessage{

public String text;

public String name;



public ChatMessage() {

}



public ChatMessage(String text) {

this.text = text;

}



public ChatMessage(String name, String text) {

this.text = text;

this.name = name;

}



}

[/java]

ok then… So I updated my code that the server send a chatmessage when client connects… Now the Server receives the message of the client, but the client isn’t getting the message of the server :s



Could this be a time issue? like the client isn’t ready yet to receive the message? Just thinking out loud…

Do you see the Client Added message from the server?

yes, client is added

Is there any way to tell that the message left from the client?

I have not worked with the new api until now but is it possible, that you still need to register the class “ChatMessage” to the Serializer using this (in both, client and server):

[java]Serializer.registerClass(ChatMessage.class);[/java]

or is this deprecated too?

omg… darthaffe thanks :d

The latest nightlies have improved the error reporting in this regard. I’m not sure what version you are using… but if one of these things slipped through the latest code then I’d like to track it down.



The issue is that I rewrote the networking side of SpiderMonkey but I didn’t really touch the Serializer stuff and there are some ways that it silently fails when it doesn’t understand how to deal with something. I’m trying to find these cases and instead throw exceptions but I may still have missed some places.



I ran into exactly the same issue you did only in my case it was because I hadn’t marked my class @Serialized and so it was never registering when I registered it. My messages would go out but then I’d never see them show up. :stuck_out_tongue: