SpiderMonkey multi client

Hi,



I just finished the spidermonkey tutorial trail, but I have no clue on how to handle multiple clients. I guess it’s creating threads just like the standard java networking but doing that implies getting the client and starting another thread in order to wait for another client, the thing is, I can’t see how to do it with spidermonkey.



Thanks in advance.

Hi,



you don’t need threads to handle multiple Clients with SpiderMonkey. You just have to start the Server and add a ConnectionListener to the Server and as soon as a new Client connects the method clientConnected(Client client) will be called and if he disconnects again clientDisconnected(Client client) is called.

In MarbleMania I keep a list of all Clients currently connected to keep track of the Clients and to be able to identify them, when the Server receives a Message.



And to receive Messages from the Clients you’ll need to add a MessageListener to your Server. When the Server receives a Message, the method messageReceived(Message msg) in the MessageListener is called and with msg.getClient() you can identify, which Client was sending the Message.



On the Client-Side you can do the same thing to receive Messages from the Server;)

2 Likes

On the server object, there is a method to retreive all the currently connected clients. Wouldn’t it be nice to have a way to retreive a client from that list based on the clientId? I’m asking because I’m using the clientId as a session ID to be able to figure out which client belongs to which player when I want to send a message to a player. It seems a bit redundant that I would have to keep a second list in my network class, just because the clients in spidermonkey are in a List and not a Map…

I was running into a problem with spidermonkey where the client IDs were in a race condition and sometimes they would populate and other times they wouldnt. It made it hard to tell which client was which.



So I ended up writing my own networking piece and I am using a map instead of a list. It holds the ID of the connected client and the actual connection information. Upon opening a connection I have the server generate an available userid and send it to the client. The client receives it and uses that assigned ID as its own then sends back a confirmation to the server that it is ready. Sort of like a mini handshaking process.



The point of me bringing this up is that eventually I want to have a database create the players and use the primary ID key from the database to distinguish what player is actually connected to the server. I want to keep the network code I wrote abstract so in my game I would have to have another map which points the server assigned ID to the database client ID. So eventually it boils down to the architecture of your game. Jmonkey, and other network packages may not come perfectly giftwrapped to meet your needs. You might have to bridge the gap between them



In response to this quote

Wouldn’t it be nice to have a way to retreive a client from that list based on the clientId?


Couldn’t you just do the following. (This is pseduocode)





[java]

public Client getClientbyId(int id)

{

for(Client c : server.getConnectors)

{

if(c.getId == id)

{

return c;

}

}

return null;

}

[/java]



EDIT: After looking at the spidermonkey code, in the server class they have a function

[java]

public Client getClient(long playerId){

for(Client client : clients) {

if(client.getPlayerID() == playerId) return client;

}

return null;

}

[/java]

Hopefully I touched on your question because I kind of interpreted it a few different ways.

doing it that way was something I had put in myself already

but looping through the entire loist everytime I want to send a message to a client would be way to costly. Currently, I just put the Client object reference in my Session object and then I put all the session objects in a Map by clientId. This way, I can get the Client straight away without having to loop each time.

This works like a charm, I just find it odd that this isn’t standard functionality in spidermonkey.