0

所以我试图用 JMonkey 做一个网络项目。我按照网络教程将消息(字符串)从客户端传递到服务器,反之亦然,没有问题。但是,当我尝试制作自己的版本并发送几何时,程序停止了。我相信一切都与我发送字符串时完全相同。

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

它崩溃的代码是这样的:

 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
4

1 回答 1

0

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.

于 2014-05-20T05:21:34.630 回答