SFSEventType.BUDDY_ADD Event Handler error not thrown on client

Post here your questions about the HTML5 / JavaScript for SFS2X

Moderators: Lapo, Bax

rukukun
Posts: 1
Joined: 12 Jun 2022, 18:53

SFSEventType.BUDDY_ADD Event Handler error not thrown on client

Postby rukukun » 12 Jun 2022, 19:05

Hey guys,

I am trying to do some custom logic on the server when a "AddBuddyRequest" is sent. However, even though I throw an error in the server, the client still just adds the user. I tried updating to the latest Smartfox version (2.18.3) and also the latest client API version (1.7.17).

Server log:

Code: Select all

12 Jun 2022 | 18:56:03,673 | WARN  | SFSWorker:Ext:2 | entities.managers.SFSExtensionManager |     | com.smartfoxserver.v2.exceptions.SFSBuddyListException:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: com.smartfoxserver.v2.exceptions.SFSBuddyListException
Message: The requested player does not exist.
Description: Error during event handling: com.smartfoxserver.v2.exceptions.SFSBuddyListException: The requested player does not exist., Listener: { Ext: __lib__, Type: JAVA, Lev: ZONE, { Zone: GlobalLobby }, {} }
+--- --- ---+
Stack Trace:
+--- --- ---+
rukukun.tdg.BuddyEventHandler.handleServerEvent(BuddyEventHandler.java:32)
com.smartfoxserver.v2.extensions.SFSExtension.handleServerEvent(SFSExtension.java:259)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchEvent(SFSExtensionManager.java:773)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchZoneLevelEvent(SFSExtensionManager.java:694)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.handleServerEvent(SFSExtensionManager.java:994)
com.smartfoxserver.v2.core.SFSEventManager$SFSEventRunner.run(SFSEventManager.java:66)
java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
java.base/java.lang.Thread.run(Thread.java:829)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


Client log:

Code: Select all

Buddy added: [Buddy: bvb, id: -1]
UnityEngine.Debug:Log (object)


Please see my simplified code;

Client code

Code: Select all

void Start() {
   [...]
   sfs.AddEventListener(SFSBuddyEvent.BUDDY_LIST_INIT, OnBuddyListInit);
   sfs.AddEventListener(SFSBuddyEvent.BUDDY_ADD, OnBuddyAdded);
   sfs.AddEventListener(SFSBuddyEvent.BUDDY_ERROR, OnBuddyError);
}

public void SendFriendRequest(string friendName)
{
   sfs.Send(new Sfs2X.Requests.Buddylist.AddBuddyRequest(friendName));
}

private void OnBuddyListInit(BaseEvent evt)
{
   Debug.Log("Buddy list initiated");
}

private void OnBuddyAdded(BaseEvent evt)
{
   Debug.Log("Buddy added: " + (Buddy)evt.Params["buddy"]);
}

private void OnBuddyError(BaseEvent evt)
{
   Debug.Log("BuddyList error: " + (string)evt.Params["errorMessage"]);
}


CustomExtension.java

Code: Select all

@Override
public void init() {
   [...]
   addEventHandler(SFSEventType.BUDDY_ADD, BuddyEventHandler.class);
   [...]
}


BuddyEventHandler.java

Code: Select all

@Override
   public void handleServerEvent(ISFSEvent arg0) throws SFSException {
      SFSErrorData errdata = null;
      Buddy targetBuddy = (Buddy) arg0.getParameter(SFSBuddyEventParam.BUDDY);
      if(checkIfUserExists(targetBuddy.getName()))
      {
         
      } else {
         errdata = new SFSErrorData(SFSErrorCode.BUDDY_LIST_FULL);
         errdata.addParameter("The requested player does not exist");
         throw new SFSBuddyListException("The requested player does not exist.", errdata);
      }
      
   }
   
private boolean checkIfUserExists(String username)
{
   try {
      IDBManager dbm = getParentExtension().getParentZone().getDBManager();
      ISFSArray resultset = [...]
      if (resultset != null && resultset.size() > 0) {
         return true;
      }
   } catch (SQLException e) {
      e.printStackTrace();
   }
   return false;
}


Please let me know if you have any solutions for me here.

Thanks!
User avatar
Lapo
Site Admin
Posts: 22508
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: SFSEventType.BUDDY_ADD Event Handler error not thrown on client

Postby Lapo » 13 Jun 2022, 07:44

Hi,
the event on the server side is fired after the action has been executed by the server. That's why throwing an exception doesn't change the outcome.
If you want your Extension to verify the request before executing it you should implement it in a different way:

  • From client side send an ExtensionRequest with the name of the Player to be added as buddy
  • Handle the request and apply the custom logic
  • If the check is passed invoke the server-side BuddyList API to add the provided name as buddy.

To learn more about the server-side BuddyList API see here:
http://docs2x.smartfoxserver.com/Advanc ... y-list-api

Scroll down a bit until you reach the Buddy List Server API section.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games

Return to “SFS2X HTML5 / JavaScript API”

Who is online

Users browsing this forum: No registered users and 2 guests