Sending packets

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

Laxika
Posts: 10
Joined: 14 Jun 2010, 12:41

Sending packets

Postby Laxika » 13 Apr 2011, 19:33

Hello guys

I try to minimise my questations on the forum and use search and docks, but I stuck again.

I send my packet this way to the clients:

Code: Select all

package sfsext;

import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import java.util.Iterator;

public class JoinMapEventHandler extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {

        User user = (User) event.getParameter(SFSEventParam.USER);
        Room room = user.getLastJoinedRoom();
       
        Iterator<User> iterator = room.getPlayersList().iterator();
        while (iterator.hasNext()) {
            User now = iterator.next();
            //User user = (User) event.getParameter(SFSEventParam.USER);

            if (user.getId() != now.getId()) {
                ISFSObject responseObj = new SFSObject();
                responseObj.putInt("xPos", (Integer) user.getProperty("xPos"));
                responseObj.putInt("xPos", (Integer) user.getProperty("yPos"));

               
                send("newPlayer", responseObj, now);
                trace("Packet sent to user: " + now.getName());
            }
        }
    }
}


This code send the position data of the joining player to everyone else in the room. I checked that both of my clients connect the same room as players.

I try to recieve the packet on clienside with this: (I use java as a client)

Code: Select all

sfs.addEventListener("newPlayer", new NewPlayerListener());


Code: Select all

package dragontale;

import com.smartfoxserver.v2.exceptions.SFSException;
import sfs2x.client.core.BaseEvent;
import sfs2x.client.core.IEventListener;

/**
 *
 * @author Laxika
 */
class NewPlayerListener implements IEventListener {

    @Override
    public void dispatch(BaseEvent be) throws SFSException {
        System.out.println("GOT DATA FROM SERVER");
        System.out.println(be.getArguments());

    }
}


The server side code runs OK, but I don't get the packet clientside. (Or I try to get it on a wrong way.)

I heard somthing about public messanges, to broadcast a packet, but I don't klnow how to use that.

Some one can help me please?

Thanks, Laxi
Democre
Posts: 77
Joined: 16 Sep 2010, 17:58

Postby Democre » 13 Apr 2011, 21:06

I haven't done anything in the Java client yet, but it seems like it should operate in a similar manner as the other client libraries.

I think the problem is that when you add the event listener, you need to add it for SFSEvent.EXTENSION_RESPONSE instead of "newPlayer".

Inside your handler.dispatch you would need to get the command parameter from the event and act on that if it equals "newPlayer".
ThomasLund
Posts: 1297
Joined: 14 Mar 2008, 07:52
Location: Sweden

Postby ThomasLund » 14 Apr 2011, 08:23

Java API works 99% similar to the other APIs:

Code: Select all

      sfs.addEventListener(SFSEvent.ROOM_GROUP_SUBSCRIBE_ERROR, new IEventListener() {
         @Override
         public void dispatch(BaseEvent evt) throws SFSException {
            delegates.onSubscribeRoomGroupError(evt);
         }
      });


Full Control - maker of Unity/C# and Java SFS API and indie games
Follow on twitter: http://twitter.com/thomas_h_lund
Laxika
Posts: 10
Joined: 14 Jun 2010, 12:41

Postby Laxika » 14 Apr 2011, 12:06

Ty Andy! Now it works! Thanks a lot!

Return to “SFS2X Questions”

Who is online

Users browsing this forum: Google [Bot] and 4 guests