Class SFSBuddyEvent


  • public class SFSBuddyEvent
    extends BaseEvent
    SFSBuddyEvent is the class representing all the events related to the Buddy List system dispatched by the SmartFoxServer 2X Java client API.

    The SFSBuddyEvent parent class provides a public property called arguments which contains specific parameters depending on the event type.

    See Also:
    SFSEvent
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String BUDDY_ADD
      The SFSBuddyEvent.BUDDY_ADD constant defines the value of the type property of the event object for a buddyAdd event.
      static java.lang.String BUDDY_BLOCK
      The SFSBuddyEvent.BUDDY_BLOCK constant defines the value of the type property of the event object for a buddyBlock event.
      static java.lang.String BUDDY_ERROR
      The SFSBuddyEvent.BUDDY_ERROR constant defines the value of the type property of the event object for a buddyError event.
      static java.lang.String BUDDY_LIST_INIT
      The SFSBuddyEvent.BUDDY_LIST_INIT constant defines the value of the type property of the event object for a buddyListInit event.
      static java.lang.String BUDDY_MESSAGE
      The SFSBuddyEvent.BUDDY_MESSAGE constant defines the value of the type property of the event object for a buddyMessage event.
      static java.lang.String BUDDY_ONLINE_STATE_UPDATE
      The SFSBuddyEvent.BUDDY_ONLINE_STATE_UPDATE constant defines the value of the type property of the event object for a buddyOnlineStateChange event.
      static java.lang.String BUDDY_REMOVE
      The SFSBuddyEvent.BUDDY_REMOVE constant defines the value of the type property of the event object for a buddyRemove event.
      static java.lang.String BUDDY_VARIABLES_UPDATE
      The SFSBuddyEvent.BUDDY_VARIABLES_UPDATE constant defines the value of the type property of the event object for a buddyVariablesUpdate event.
    • Constructor Summary

      Constructors 
      Constructor Description
      SFSBuddyEvent​(java.lang.String type)  
      SFSBuddyEvent​(java.lang.String type, java.util.Map<java.lang.String,​java.lang.Object> args)
      Creates a new SFSBuddyEvent instance.
    • Field Detail

      • BUDDY_LIST_INIT

        public static final java.lang.String BUDDY_LIST_INIT
        The SFSBuddyEvent.BUDDY_LIST_INIT constant defines the value of the type property of the event object for a buddyListInit event.

        Dispatched if the Buddy List system is successfully initialized. This event is fired in response to the InitBuddyListRequest request in case the operation is executed successfully.

        After the Buddy List system initialization, the user returns to his previous custom state (if any - see IBuddyManager.myState property). His online/offline state, his nickname and his persistent Buddy Variables are all loaded and broadcast in the system. In particular, the online state (see IBuddyManager.myOnlineState property) determines if the user will appear online or not to other users who have him in their buddies list.

        The properties of the arguments object contained in the event object have the following values:

        PropertyTypeDescription
        buddyListArrayA list of Buddy objects representing all the buddies in the current user's buddies list.
        myVariablesArrayA list of all the Buddy Variables associated with the current user.
        Example
        The following example initializes the Buddy List system:
         private void someMethod() {
             sfs.addEventListener(SFSBuddyEvent.BUDDY_LIST_INIT, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     System.out.println("Buddy List system initialized successfully");
                     
                     // Retrieve my buddies list
                     List buddies = (List) evt.getArguments().get("buddyList");
                     
                     // Display the online buddies in a list component in the application interface
                           ...
                 }
             });
             sfs.addEventListener(SFSBuddyEvent.BUDDY_ERROR, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     System.out.println("The following error occurred while executing a buddy-related request:" + evt.getArguments().get("errorMessage"));
                 }
             });
             
             // Initialize the Buddy List system
             sfs.send(new InitBuddyListRequest());
         }
         
        See Also:
        InitBuddyListRequest, IBuddyManager, Buddy, BuddyVariable, BUDDY_ERROR, Constant Field Values
      • BUDDY_ADD

        public static final java.lang.String BUDDY_ADD
        The SFSBuddyEvent.BUDDY_ADD constant defines the value of the type property of the event object for a buddyAdd event.

        Dispatched when a buddy is added successfully to the current user's buddies list. This event is fired in response to the AddBuddyRequest request in case the operation is executed successfully.

        The properties of the arguments object contained in the event object have the following values:

        PropertyTypeDescription
        buddyBuddyThe Buddy object corresponding to the buddy that was added.
        Example
        The following example handles the possible events caused by a request to add a buddy:
         private void someMethod() {
             sfs.addEventListener(SFSBuddyEvent.BUDDY_ADD, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     System.out.println("This buddy was added:" + ((Buddy) evt.getArguments().get("buddy")).getName();
                 }
             });
             sfs.addEventListener(SFSBuddyEvent.BUDDY_ERROR, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     System.out.println("The following error occurred during a buddy-related request:" + evt.getArguments().get("errorMessage"));
                 }
             });
             
             // Add user "Jack" as a new buddy to my buddies list
             sfs.send(new AddBuddyRequest("Jack"));
         }
         
        See Also:
        AddBuddyRequest, Buddy, BUDDY_REMOVE, BUDDY_ERROR, Constant Field Values
      • BUDDY_REMOVE

        public static final java.lang.String BUDDY_REMOVE
        The SFSBuddyEvent.BUDDY_REMOVE constant defines the value of the type property of the event object for a buddyRemove event.

        Dispatched when a buddy is removed successfully from the current user's buddies list. This event is fired in response to the RemoveBuddyRequest request in case the operation is executed successfully.

        The properties of the arguments object contained in the event object have the following values:

        PropertyTypeDescription
        buddyBuddyThe Buddy object corresponding to the buddy that was removed.
        Example
        The following example handles the possible events caused by a request to remove a buddy:
         private void someMethod() {
             sfs.addEventListener(SFSBuddyEvent.BUDDY_REMOVE, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     System.out.println("This buddy was removed:" + ((Buddy) evt.getArguments().get("buddy")).getName();
                 }
             });
             sfs.addEventListener(SFSBuddyEvent.BUDDY_ERROR, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     System.out.println("The following error occurred during a buddy-related request:" + evt.getArguments().get("errorMessage"));
                 }
             });
             
             // Remove Jack from my buddies list
             sfs.send(new RemoveBuddyRequest("Jack"));
         }
         
        See Also:
        RemoveBuddyRequest, Buddy, BUDDY_ADD, BUDDY_ERROR, Constant Field Values
      • BUDDY_BLOCK

        public static final java.lang.String BUDDY_BLOCK
        The SFSBuddyEvent.BUDDY_BLOCK constant defines the value of the type property of the event object for a buddyBlock event.

        Dispatched when a buddy is blocked or unblocked successfully by the current user. This event is fired in response to the BlockBuddyRequest request in case the operation is executed successfully.

        The properties of the arguments object contained in the event object have the following values:

        PropertyTypeDescription
        buddyBuddyThe Buddy object corresponding to the buddy that was blocked/unblocked.
        Example
        The following example handles the possible events caused by a request to block a buddy:
         private void someMethod() {
             sfs.addEventListener(SFSBuddyEvent.BUDDY_BLOCK, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     boolean isBlocked = ((Buddy)evt.getArguments().get("buddy")).isBlocked();
                     System.out.println("Buddy " + ((Buddy) evt.getArguments().get("buddy")).getName() + " is now " + (isBlocked ? "blocked" : "unblocked"));
                 }
             });
             sfs.addEventListener(SFSBuddyEvent.BUDDY_ERROR, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     System.out.println("The following error occurred during a buddy-related request:" + evt.getArguments().get("errorMessage"));
                 }
             });
             
             // Block user "Jack" in my buddies list
             sfs.send(new BlockBuddyRequest("Jack", true));
         }
         
        See Also:
        BlockBuddyRequest, Buddy, BUDDY_ERROR, Constant Field Values
      • BUDDY_ERROR

        public static final java.lang.String BUDDY_ERROR
        The SFSBuddyEvent.BUDDY_ERROR constant defines the value of the type property of the event object for a buddyError event.

        Dispatched if an error occurs while executing a request related to the Buddy List system. For example, this event is fired in response to the AddBuddyRequest request, the BlockBuddyRequest, etc.

        The properties of the arguments object contained in the event object have the following values:

        PropertyTypeDescription
        errorMessageStringThe message which describes the error.
        errorCodeshortThe error code.
        Example
        See the example provided in the BUDDY_ADD constant description.
        See Also:
        BUDDY_ADD, Constant Field Values
      • BUDDY_ONLINE_STATE_UPDATE

        public static final java.lang.String BUDDY_ONLINE_STATE_UPDATE
        The SFSBuddyEvent.BUDDY_ONLINE_STATE_UPDATE constant defines the value of the type property of the event object for a buddyOnlineStateChange event.

        Dispatched when a buddy in the current user's buddies list changes his online state in the Buddy List system. This event is fired in response to the GoOnlineRequest request.

        NOTE: this event is dispatched to those who have the user as a buddy, but also to the user himself. As in this case the value of the buddy parameter is null (because the user is not buddy to himself of course), the isItMe parameter should be used to check if the current user is the one who changed his own online state.

        The properties of the arguments object contained in the event object have the following values:

        PropertyTypeDescription
        buddyBuddyThe Buddy object representing the buddy who changed his own online state. If the isItMe parameter is true, the value of this parameter is null (because a user is not buddy to himself).
        isItMeBooleantrue if the online state was changed by the current user himself (in this case this event is a sort of state change confirmation).
        Example
        The following example changes the online state of the user in the Buddy List system:
         private void someMethod() {
             sfs.addEventListener(SFSBuddyEvent.BUDDY_ONLINE_STATE_UPDATE, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     // As the state change event is dispatched to me too,
                     // I have to check if I am the one who changed his state
                     
                     boolean isItMe = (Boolean)evt.getArguments().get("isItMe");
                     
                     if (isItMe)
                         System.out.println("I'm now" + (sfs.getBuddyManager().getMyOnlineState() ? "online" : "offline"));
                     else
                         System.out.println("My buddy " + ((Buddy) evt.getArguments().get("buddy")).getName() + " is now" + (((Buddy) evt.getArguments().get("buddy")).isOnline() ? "online" : "offline"));
                 }
             });
             
             // Put myself offline in the Buddy List system
             sfs.send(new GoOnlineRequest(false));
         }
         
        See Also:
        GoOnlineRequest, Buddy, Constant Field Values
      • BUDDY_VARIABLES_UPDATE

        public static final java.lang.String BUDDY_VARIABLES_UPDATE
        The SFSBuddyEvent.BUDDY_VARIABLES_UPDATE constant defines the value of the type property of the event object for a buddyVariablesUpdate event.

        Dispatched when a buddy in the current user's buddies list updates one or more Buddy Variables. This event is fired in response to the SetBuddyVariablesRequest request.

        NOTE: this event is dispatched to those who have the user as a buddy, but also to the user himself. As in this case the value of the buddy parameter is null (because the user is not buddy to himself of course), the isItMe parameter should be used to check if the current user is the one who updated his own Buddy Variables.

        The properties of the arguments object contained in the event object have the following values:

        PropertyTypeDescription
        buddyBuddyThe Buddy object representing the buddy who updated his own Buddy Variables. If the isItMe parameter is true, the value of this parameter is null (because a user is not buddy to himself).
        isItMeBooleantrue if the Buddy Variables were updated by the current user himself (in this case this event is a sort of update confirmation).
        changedVarsArrayThe list of names of the Buddy Variables that were changed (or created for the first time).
        Example
        The following example sets some Buddy Variables for the current user, one of which is persistent; the example also handles changes made by the user or by his buddies:
         private void someMethod() {
             sfs.addEventListener(SFSBuddyEvent.BUDDY_VARIABLES_UPDATE, new IEventListener() {
                 public void dispatch(BaseEvent evt) throws SFSException {
                     // As the update event is dispatched to me too,
                     // I have to check if I am the one who changed his Buddy Variables
                     
                     boolean isItMe = (Boolean)evt.getArguments().get("isItMe");
                     
                     if (isItMe) {
                         System.out.println("I've updated the following Buddy Variables:");
                         
                         for (String bVarName : (List)evt.getArguments().get("changedVars")) {
                             System.out.println(bVarName + "-->" + sfs.getBuddyManager().getMyVariable(bVarName).getValue());
                         }
                     }
                     else
                     {
                         String buddyName = ((Buddy)evt.getArguments().get("buddy")).getName();
                         
                         System.out.println("My buddy " + buddyName + " updated the following Buddy Variables:");
                         
                         for (String bVarName : (List)evt.getArguments().get("changedVars")) {
                             System.out.println(bVarName + "-->" + sfs.getBuddyManager().getBuddyByName(buddyName).getVariable(bVarName).getValue());
                         }
                     }
                 }
             });
             
             // Create two Buddy Variables containing the title and artist of the song I'm listening to
             BuddyVariable songTitle = new SFSBuddyVariable("songTitle", "Ascension");
             BuddyVariable songAuthor = new SFSBuddyVariable("songAuthor", "Mike Oldfield");
             
             // Create a persistent Buddy Variable containing my mood message
             BuddyVariable mood = new SFSBuddyVariable(SFSBuddyVariable.OFFLINE_PREFIX + "mood", "I Need SmartFoxServer 2X desperately!");
             
             // Set my Buddy Variables
             List vars = new ArrayList();
             vars.add(songTitle);
             vars.add(songAuthor);
             vars.add(mood);
             sfs.send(new SetBuddyVariablesRequest(vars));
         }
         
        See Also:
        SetBuddyVariablesRequest, BuddyVariable, Buddy, Constant Field Values
      • BUDDY_MESSAGE

        public static final java.lang.String BUDDY_MESSAGE
        The SFSBuddyEvent.BUDDY_MESSAGE constant defines the value of the type property of the event object for a buddyMessage event.

        Dispatched when a message from a buddy is received by the current user. This event is fired in response to the BuddyMessageRequest request.

        NOTE: the same event is fired by the sender's client too, so that the user is aware that the message was delivered successfully to the recipient, and it can be displayed in the chat area keeping the correct message ordering. As in this case the value of the buddy parameter is null (because, being the sender, the user is not buddy to himself of course), there is no default way to know who the message was originally sent to. As this information can be useful in scenarios where the sender is chatting with more than one buddy at the same time in separate windows or tabs (and we need to write his own message in the proper one), the data parameter can be used to store, for example, the id of the recipient buddy.

        The properties of the arguments object contained in the event object have the following values:

        PropertyTypeDescription
        buddyBuddyThe Buddy object representing the message sender. If the isItMe parameter is true, the value of this parameter is null (because a user is not buddy to himself).
        isItMeBooleantrue if the message sender is the current user himself (in this case this event is a sort of message delivery confirmation).
        messageStringThe message text.
        dataISFSObjectAn instance of SFSObject containing additional custom parameters (e.g. the message color, an emoticon id, etc).
        Example
        The following example sends a message to a buddy and handles the related event:
         private void someMethod() {
             sfs.addEventListener(SFSBuddyEvent.BUDDY_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
                     
                     boolean isItMe = (Boolean)evt.getArguments().get("isItMe");
                     Buddy sender = (Buddy)evt.getArguments().get("buddy");
                     
                     if (isItMe)
                         System.out.println("I said:" + evt.getArguments().get("message"));
                     else
                         System.out.println("My buddy " + sender.getName() + " said:" + evt.getArguments().get("message"));
                 }
             });
             
             // Get the recipient of the message, in this case my buddy Jack
             Buddy buddy = sfs.getBuddyManager().getBuddyByName("Jack");
             
             // Send a message to Jack
             sfs.send(new BuddyMessageRequest("Hello Jack!", buddy));
         }
         
        See Also:
        BuddyMessageRequest, Buddy, Constant Field Values
    • Constructor Detail

      • SFSBuddyEvent

        public SFSBuddyEvent​(java.lang.String type,
                             java.util.Map<java.lang.String,​java.lang.Object> args)
        Creates a new SFSBuddyEvent instance.
        Parameters:
        type - The type of event.
        args - An object containing the parameters of the event.
      • SFSBuddyEvent

        public SFSBuddyEvent​(java.lang.String type)