Handling leaveroom metrics when client is closed.

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

Moderators: Lapo, Bax

User avatar
Aet
Posts: 34
Joined: 15 Aug 2019, 23:10

Handling leaveroom metrics when client is closed.

Postby Aet » 16 Aug 2019, 16:38

I have a number of metrics I log when a user leaves a room about their activity in that room, as well as some room level metrics that record activity when the last user has left a room.

These work fine for most cases, but when the room is set to delete on empty, and the last user to leave exits by closing their client.. the room gets deleted when the session is closed before the logout handler is called. The logout handler then calls my handleLeaveRoom method on all joined non-null rooms.

Is there a way I can make it so that my handleLeaveRoom method is called before the room is deleted from lack of users?

I would ideally like to keep the delete on empty functionality, but might need to implement my own version which handles this case.

Thanks for the help.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Handling leaveroom metrics when client is closed.

Postby Lapo » 17 Aug 2019, 15:25

To catch the event you should add some code in your Extension's destroy() method. This is invoked before the Room is deleted and should do the trick.
It should be done like this:

Code: Select all

@Override
public void destroy()
{
   super.destroy()
   // my code...
}


Hope it helps
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Aet
Posts: 34
Joined: 15 Aug 2019, 23:10

Re: Handling leaveroom metrics when client is closed.

Postby Aet » 19 Aug 2019, 16:38

Thanks, but I'm a little unclear on a few details. Its been a bit since I've programmed seriously in Java, so I may just have some simple misunderstanding on my part.

The SFSExtension's destroy() method is only called once when the server is closed though correct?

I'm looking for recording the data when the user closes their client while in a dynamic room. So the server is still running and other users are online in other rooms. So what I really would want to override is SFSRoom's destroy(), but I don't (and can't without significant effort?) extend that class.

Specifically my metrics recording are inside my handler for USER_LEAVE_ROOM so I can log to a Splunk server user metrics and activities, and each user also has a list of joined rooms so on USER_LOGOUT and USER_DISCONNECT I can call the leaveroom handler on each joined room. The issue being that the room is deleted for being empty on a USER_DISCONNECT before the handler is called and can retrieve the room properties or even the room name.

Again, apologies if I'm missing your point here.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Handling leaveroom metrics when client is closed.

Postby Lapo » 20 Aug 2019, 09:23

Aet wrote:Thanks, but I'm a little unclear on a few details. Its been a bit since I've programmed seriously in Java, so I may just have some simple misunderstanding on my part.

The SFSExtension's destroy() method is only called once when the server is closed though correct?

Depends on what it is attached to. When an Extension is attached to a Room the destroy() method is called when the Room is removed.
Are your game Rooms running an Extension?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Aet
Posts: 34
Joined: 15 Aug 2019, 23:10

Re: Handling leaveroom metrics when client is closed.

Postby Aet » 20 Aug 2019, 18:02

Alright, thanks for the help, it was something simple I missed, since someone else wrote the codebase I'm working with so I wasn't aware of RoomExtensions as separate from ZoneExtensions which I thought you were referring to.

I've added a simple RoomExtension method with some code to handle my metrics before it gets deleted, and I need to attach it to every dynamic room the client-side creates, so I imagine that should solve my issues once I get it working.

But just to be sure, there is not a better way to do this correct? We don't need a Room Extension (at least currently) for anything other than catching some metrics logging. And reading this: https://smartfoxserver.com/blog/perform ... xtensions/

I'd rather not worry about scaling issues in the future if there is a different solution. Though I imagine I can put only the RoomExtension and some logging in a separate extension jar to counteract this to some degree.

Regardless if you have any suggestion on how to better handle this I'd welcome it, we have a a couple hundred dynamic rooms being created and removed relatively frequently, so its ideal to keep that process rapid as the number increases.

Edit: I'm thinking of instead of a CreateRoomRequest from the client I have my own UserCreateRoom handler which creates a "CustomRoom extends SFSRoom" and overrides delete() in there.
User avatar
Lapo
Site Admin
Posts: 23027
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Handling leaveroom metrics when client is closed.

Postby Lapo » 21 Aug 2019, 07:00

Hi,
Aet wrote:I've added a simple RoomExtension method with some code to handle my metrics before it gets deleted, and I need to attach it to every dynamic room the client-side creates, so I imagine that should solve my issues once I get it working.

If you need to add an Extension to every Room only to obtain the stats then I don't think it's the best approach.
In my previous message I was only asking if you were using Room Extensions. If so, I would have recommended using the destroy() method. But if you do not run any Room Extension it is a different story.

In this case you should be able to handle all the necessary events from the Zone Extension instead.
You get an event for every user joining or leaving a Room.
When a User gets disconnected or quits the game you also get an event with a reference to all Rooms he was joined previously.
This should be enough to handle the metrics you have mentioned. If not please give us more details.

Edit: I'm thinking of instead of a CreateRoomRequest from the client I have my own UserCreateRoom handler which creates a "CustomRoom extends SFSRoom" and overrides delete() in there.

I don't think it's the right way to do it. I'd say server side events is the proper approach here.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
Aet
Posts: 34
Joined: 15 Aug 2019, 23:10

Re: Handling leaveroom metrics when client is closed.

Postby Aet » 21 Aug 2019, 19:18

Thanks!

It works great. We had our own implementation of JOINED_ROOMS as a user property with a list of strings so we can use it elsewhere easily, so just using that for joined rooms in the logout handler was failing because zone.getRoomByName() was returning null for the dynamic room.

But using the event parameter falls very neatly as a replacement for that.

I'm surprised but very happy that the server lists the room as being deleted before entering the logout function and retrieving a still living reference to the room.

Thanks for the help!

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 142 guests