Class ObjectMessageRequest

  • All Implemented Interfaces:
    sfs2x.client.requests.IRequest

    public class ObjectMessageRequest
    extends sfs2x.client.requests.GenericMessageRequest
    Sends an object containing custom data to all users in a Room, or a subset of them.

    The data object is delivered to the selected users (or all users excluding the sender) inside the target Room by means of the objectMessage event. It can be useful to send game data, like for example the target coordinates of the user's avatar in a virtual world.

    Example
    The following example sends the player's character movement coordinates and handles the respective event (note: the myCharacter instance is supposed to be the user sprite on the stage, while the getUserCharacter method retrieves the sprite of other users' characters):

     private void someMethod() {
         sfs.addEventListener(SFSEvent.OBJECT_MESSAGE, new IEventListener() {
             public void dispatch(BaseEvent evt) throws SFSException {
                 ISFSObject dataObj = (SFSObject)evt.getArguments().get("message");
                 
                 User sender = evt.getArguments().get("sender");
                 UserCharacter character = getUserCharacter(sender.getId());
                 
                 character.x = dataObj.getInt("x");
                 character.y = dataObj.getInt("y");
             }
         });
         
         // Send my movement to all players
         ISFSObject dataObj = new SFSObject();
         dataObj.putInt("x", myCharacter.x);
         dataObj.putInt("y", myCharacter.y);
         
         sfs.send(new ObjectMessageRequest(dataObj));
     }
     
    See Also:
    SFSEvent.OBJECT_MESSAGE
    • Constructor Detail

      • ObjectMessageRequest

        public ObjectMessageRequest​(com.smartfoxserver.v2.entities.data.ISFSObject obj,
                                    Room targetRoom,
                                    java.util.List<User> recipients)
        Creates a new ObjectMessageRequest instance. The instance must be passed to the SmartFox.send() method for the request to be performed.
        Parameters:
        obj - An instance of SFSObject containing custom parameters to be sent to the message recipients.
        targetRoom - The Room object corresponding to the Room where the message should be dispatched; if null, the last Room joined by the user is used.
        recipients - A list of User objects corresponding to the message recipients; if null, the message is sent to all users in the target Room (except the sender himself).
        See Also:
        SmartFox.send(sfs2x.client.requests.IRequest), SFSObject, User