Multiplayer game using Spider Monkey questions

Since most of the posts on this forum seem out of date, some from 3 years ago, I’m going to make a new thread. I just started using JME and have been playing around with SpiderMonkey for the networking portion. The small test I’m trying to setup is multiple users connect to a server and control a character. For the purposes of this demo im trying to setup, the players will just control a box and have it spin. I am having trouble figuring out what to do next. I’m not quite sure the proper way of setting up my server logic and client logic. I wanted to get some ideas (or maybe there are some api calls I’m missing that will make it easier) on how to get this working.



Basically what code and where do I put it to get multiple boxes (1 per connected client) and all the clients see the boxes being rotated based on user input.



The code I have here has the client connect to a server telling the server its ready, server responds with an “ok” and the client laucnhes the app. Not how its going to be on final but its for testing purposes.



Client code.

[java]

public class myClient extends MessageAdapter{



public static void main(String[] args) throws IOException, InterruptedException {

Serializer.registerClass(SomeObject.class);

Serializer.registerClass(myMessage.class);

Client client = new Client(“localhost”, 5110, 5110);



client.start();

client.send(new myMessage(true));



// Thread.sleep(5000);

// client.disconnect(“Test”);

client.addMessageListener(new myClient(), myMessage.class);

}

private static SimpleApplication serverApp;



public void StartApp()

{

// serverApp = new SimpleApplication() {

// @Override

// public void simpleInitApp() {

// viewPort.setBackgroundColor(new ColorRGBA(0.7f, 0.8f, 1f, 1f));

// }

// };



serverApp = new TestShapeGame();

serverApp.setShowSettings(false);

serverApp.start();

}

@Override

public void messageReceived(Message msg){

myMessage cm = (myMessage) msg;

System.out.println(“Client received Message from server:” + cm.d);



if(cm.d == 99.9) //just a testing value

{

StartApp();

}

}

}[/java]



Server code

[java]

public class myServer extends MessageAdapter {



public static void main(String[] args) throws IOException, InterruptedException{

Serializer.registerClass(SomeObject.class);

Serializer.registerClass(myMessage.class);



Server server = new Server(5110, 5110);

server.start();



server.addMessageListener(new myServer(), myMessage.class);

}



@Override

public void messageReceived(Message msg){

myMessage cm = (myMessage) msg;

// System.out.println(cm.z);

// System.out.println(cm.b);

// System.out.println(cm.c);

// System.out.println(cm.s);

// System.out.println(cm.i);

// System.out.println(cm.f);

// System.out.println(cm.l);

// System.out.println(cm.d);

// System.out.println(Arrays.toString(cm.ia));

// System.out.println(cm.ls);

// System.out.println(cm.mp);

// System.out.println(cm.status1);

// System.out.println(cm.status2);

// System.out.println(cm.date);

if(cm.isReady)

{

cm.d = 99.9;



if(msg.getClient() != null)

try {

msg.getClient().send(cm);

} catch (IOException ex) {

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

}



}

}

}

[/java]



Simple game

[java]public class TestShapeGame extends SimpleApplication {



@Override

public void simpleInitApp() {



DirectionalLight sun = new DirectionalLight();

sun.setDirection(new Vector3f(-5.1f, 5.7f, -5.0f));

rootNode.addLight(sun);



Box b = new Box(Vector3f.ZERO, 1, 1, 1);

player = new Geometry(“blue cube”, b);

Material mat = new Material(assetManager, “Common/MatDefs/Misc/SolidColor.j3md”);

mat.setColor(“m_Color”, ColorRGBA.Blue);

player.setMaterial(mat);

rootNode.attachChild(player);

}

protected Geometry player;



@Override

public void simpleUpdate(float tpf) {

player.rotate(0, 2*tpf, 0);

}

}

[/java]



myMessage

[java]



@Serializable

public class myMessage extends Message {



@Serializable

public static class SomeObject {



private int val;



public SomeObject(){

}



public SomeObject(int val){

this.val = val;

}



public int getVal(){

return val;

}



@Override

public String toString(){

return “SomeObject[val=”+val+"]";

}

}

public enum Status {

High,

Middle,

Low;

}

boolean z;

byte b;

char c;

short s;

int i;

float f;

long l;

double d;



boolean isReady;



int[] ia;

ArrayList ls;

HashMap<String, SomeObject> mp;



Status status1;

Status status2;



Date date;



public myMessage(){

super(true);

}



public myMessage(boolean initIt){

super(true);

if (initIt){

z = true;

b = -88;

c = ‘Y’;

s = 9999;

i = 123;

f = -75.4e8f;

l = 9438345072805034L;

d = -854834.914703e88;

ia = new int[]{ 456, 678, 999 };



ls = new ArrayList();

ls.add(“hello”);

ls.add(new SomeObject(-22));



mp = new HashMap<String, SomeObject>();

mp.put(“abc”, new SomeObject(555));



status1 = Status.High;

status2 = Status.Middle;



date = new Date(System.currentTimeMillis());

isReady = true;

}

}

}[/java]

I know you would need to make some sort of game state to send back and forth between the two. Other than that I’m not that sure either. Hopefully someone else can help.

Ther eis actually a dedicated SpiderMonkey forum…



You might have more luck there

ractoc said:
Ther eis actually a dedicated SpiderMonkey forum...

You might have more luck there

Right you are! Moved.