Class PublicMessageRequest

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

    public class PublicMessageRequest
    extends sfs2x.client.requests.GenericMessageRequest
    Sends a public chat message.

    A public message is dispatched to all the users in the specified Room, including the message sender (this allows showing messages in the correct order in the application interface); the corresponding event is the publicMessage event. It is also possible to send an optional object together with the message: it can contain custom parameters useful to transmit, for example, additional informations related to the message, like the text font or color, or other formatting details.

    In case the target Room is not specified, the message is sent in the last Room joined by the sender.

    NOTE: the publicMessage event is dispatched if the Room is configured to allow public messaging only (see the RoomSettings.permissions parameter).

    Example
    The following example sends a public message and handles the respective event:

     private void someMethod() {
         sfs.addEventListener(SFSEvent.PUBLIC_MESSAGE, new IEventListener() {
             public void dispatch(BaseEvent evt) throws SFSException {
                 // As messages are forwarded to the sender too,
                 // I have to check if I am the sender
                 User sender = (User)evt.getArguments().get("sender");
                 
                 if (sender == sfs.getMySelf())
                     System.out.println("I said:" + evt.getArguments().get("message"));
                 else
                     System.out.println("User " + sender.getName() + " said:" + evt.getArguments().get("message"));
             }
         });
     
         // Send a public message
         sfs.send(new PublicMessageRequest("Hello everyone!"));
     }
     
    See Also:
    SFSEvent.PUBLIC_MESSAGE, RoomSettings.getPermissions()
    • Constructor Detail

      • PublicMessageRequest

        public PublicMessageRequest​(java.lang.String message,
                                    com.smartfoxserver.v2.entities.data.ISFSObject params,
                                    Room targetRoom)
        Creates a new PublicMessageRequest instance. The instance must be passed to the SmartFox.send() method for the request to be performed.
        Parameters:
        message - The message to be sent to all the users in the target Room.
        params - An instance of SFSObject containing additional custom parameters to be sent to the message recipients (for example the color of the text, etc).
        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.
        See Also:
        SmartFox.send(sfs2x.client.requests.IRequest), SFSObject