[ Error with messages in JMonkey Networking ]
So i was trying to do a Networking project using JMonkey. I followed the networking tutorial to pass messages (String) from the client to the server and vice versa and there was no problem. Then however when I tried to make my own version of it and send a Geometry the program stops. I believe everything is exactly the same as when I sent the String.
I read a few other questions where they had a very similar problem and they solved apparently by registering the class with serializer and so I checked and i cannot see any problem with what I am doing, could anyone please help??
The code where it crashes is this:
Client myClient;
@Override
public void simpleInitApp() {
try {
myClient = Network.connectToServer("localhost", 6143);
myClient.start();
ClientListener listener = new ClientListener(rootNode);
Serializer.registerClass(HelloMessage.class);
myClient.addMessageListener(listener, HelloMessage.class);
Serializer.registerClass(GeomPos.class);
myClient.addMessageListener(listener, GeomPos.class);
Message message = new HelloMessage("Hello World!");
myClient.send(message);
} catch (IOException ex) {
Logger.getLogger(ClientMain.class.getName()).log(Level.SEVERE, null, ex);
}
flyCam.setEnabled(false);
// You must add a light to make the model visible
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f));
rootNode.addLight(sun);
assetManager.registerLocator("town.zip", ZipLocator.class);
Spatial gameLevel = assetManager.loadModel("main.scene");
gameLevel.setLocalTranslation(0, -5.2f, 0);
gameLevel.setLocalScale(2);
rootNode.attachChild(gameLevel);
Box b = new Box(1, 1, 1);
geom = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
geom.setLocalTranslation(0.0f, -3.80f, 0.0f);
rootNode.attachChild(geom);
Message msg = new GeomPos(geom);
myClient.send(msg); //This is the line where it crashes<--------------------------
initKeys();
}
Finally this is the GeomPos Message class:
package mygame;
import com.jme3.network.AbstractMessage;
import com.jme3.network.serializing.Serializable;
import com.jme3.scene.Geometry;
@Serializable
public class GeomPos extends AbstractMessage{
private Geometry geom;
public GeomPos() {
}
public GeomPos(Geometry g) {
geom = g;
}
public Geometry getGeometry() {
return geom;
}
}
I get the following error:
INFO: Audio max auxilary sends: 4
May 17, 2014 11:50:29 PM com.jme3.app.Application handleError
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.RuntimeException: Error serializing message
Answer 1
Just in case anyone has a similar problem in the future, the problem was that the class Geometry is not serializable and therefore cannot be sent through a message. What should be done is creating your own serializable class and register to the serializer. Yor class should have all the attributes that you need to send but all the attributes should aslo be serializable.