JMonkey Networking中的消息出错

时间:2014-05-18 07:33:36

标签: exception networking message serializer monkey

所以我尝试使用JMonkey进行网络项目。我按照网络教程将消息(String)从客户端传递到服务器,反之亦然,没有问题。然而,当我试图制作我自己的版本并发送Geometry时,程序停止。我相信一切都与发送String时完全一样。

我读了一些其他问题,他们有一个非常相似的问题,他们通过使用序列化程序注册类显然解决了所以我检查了,我看不出有什么问题我正在做什么,有人可以帮忙吗?

崩溃的代码是:

 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();      
}

最后这是GeomPos Message类:

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;
}

}

我收到以下错误:

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

1 个答案:

答案 0 :(得分:0)

以防万一将来遇到类似问题,问题是Geometry类不可序列化,因此无法通过消息发送。应该做的是创建自己的可序列化类并注册到序列化器。 Yor类应该具有您需要发送的所有属性,但是所有属性都应该是可序列化的。