User Disconnection Cant Get Room and Room Variables

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

Moderators: Lapo, Bax

balasourav
Posts: 22
Joined: 25 Jul 2017, 07:44

User Disconnection Cant Get Room and Room Variables

Postby balasourav » 22 Jul 2020, 23:14

This is my server side code in java :-

Code: Select all

if(event.getType() == SFSEventType.USER_DISCONNECT)
{
         trace("User Disconnect EventCapture for User 1:"+event.getParameter(SFSEventParam.USER));
         User u;
         Room r;
         u = (User) event.getParameter(SFSEventParam.USER);
         trace("Connection Lost User Name 1:"+u.getName());
         trace(u);      
                  
         r = (Room) event.getParameter(SFSEventParam.ROOM);
         trace(r); //------------------------------------Line No - 34
 }
                 


My client is javascript. When i refresh the html5 page user is disconnected. At the i can't get the ROOM and my room variables.
When a last user is disconnected at time the room is empty. In my scenario Room removed event is triggered 1st then user disconnect event is triggered that's why i cant get the room details.
My Error is :-

Code: Select all

04:32:04,763 WARN  [SFSWorker:Ext:1] managers.SFSExtensionManager     - java.lang.NullPointerException:
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Exception: java.lang.NullPointerException
Message: *** Null ***
Description: Error during event handling: java.lang.NullPointerException, Listener: { Ext: R_Room, Type: JAVA, Lev: ZONE, { Zone: R_Don }, {} }
+--- --- ---+
Stack Trace:
+--- --- ---+
com.smartfoxserver.v2.extensions.BaseSFSExtension.getTraceMessage(BaseSFSExtension.java:551)
com.smartfoxserver.v2.extensions.BaseSFSExtension.trace(BaseSFSExtension.java:529)
com.smartfoxserver.v2.extensions.BaseSFSExtension.trace(BaseSFSExtension.java:519)
com.smartfoxserver.v2.extensions.BaseServerEventHandler.trace(BaseServerEventHandler.java:98)
logic.RD_EventHandler.handleServerEvent(RD_EventHandler.java:34)
com.smartfoxserver.v2.extensions.SFSExtension.handleServerEvent(SFSExtension.java:259)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchEvent(SFSExtensionManager.java:768)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.dispatchZoneLevelEvent(SFSExtensionManager.java:689)
com.smartfoxserver.v2.entities.managers.SFSExtensionManager.handleServerEvent(SFSExtensionManager.java:921)
com.smartfoxserver.v2.core.SFSEventManager$SFSEventRunner.run(SFSEventManager.java:65)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
java.lang.Thread.run(Thread.java:745)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


What can i do for this type of pblm. At the time of user disconnection i want get room details and Room variables. Please reply soon.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User Disconnection Cant Get Room and Room Variables

Postby Lapo » 23 Jul 2020, 07:25

Hi,
is the Java code (that you have posted) part of a Room Extension?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
balasourav
Posts: 22
Joined: 25 Jul 2017, 07:44

Re: User Disconnection Cant Get Room and Room Variables

Postby balasourav » 23 Jul 2020, 07:28

Lapo wrote:Hi,
is the Java code (that you have posted) part of a Room Extension?

Thanks


Yes its a java code and That is zone Extension.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User Disconnection Cant Get Room and Room Variables

Postby Lapo » 23 Jul 2020, 08:00

Hi,
the problem is the parameter you're trying to read.

There is no SFSEventParam.ROOM passed by the USER_DISCONNECT event. Instead the event passes a list of Rooms in which the player was joined --> SFSEventParam.JOINED_ROOMS

In other words:

Code: Select all

List<Room> joinedRooms = event.getParameter(SFSEventParam.JOINED_ROOMS);


You can check the docs here for more details:
http://docs2x.smartfoxserver.com/api-do ... DISCONNECT

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
balasourav
Posts: 22
Joined: 25 Jul 2017, 07:44

Re: User Disconnection Cant Get Room and Room Variables

Postby balasourav » 23 Jul 2020, 08:19

Lapo wrote:

Code: Select all

List<Room> joinedRooms = event.getParameter(SFSEventParam.JOINED_ROOMS);



Thanks For You Reply Lapo. If i use the above code, can i get room variables?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User Disconnection Cant Get Room and Room Variables

Postby Lapo » 23 Jul 2020, 08:21

The code above will give you back the list of Rooms in which the User was joined.
From there you can extract the Room(s) and access their relative variables.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
balasourav
Posts: 22
Joined: 25 Jul 2017, 07:44

Re: User Disconnection Cant Get Room and Room Variables

Postby balasourav » 23 Jul 2020, 09:17

Lapo wrote:The code above will give you back the list of Rooms in which the User was joined.
From there you can extract the Room(s) and access their relative variables.

Cheers


Thank You So much Lapo.
1. Please check the below code is correct or not? Or any-other ways there to split List?

Code: Select all

@SuppressWarnings("unchecked")
List<Room> joinedRooms = (List<Room>) event.getParameter(SFSEventParam.JOINED_ROOMS);
joinedRooms.get(0).getName().equals("The Lobby")


2. The Below code only contains single room details or it contains all ROOMS joined by user?

Code: Select all

List<Room> joinedRooms = event.getParameter(SFSEventParam.JOINED_ROOMS);
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User Disconnection Cant Get Room and Room Variables

Postby Lapo » 23 Jul 2020, 11:20

balasourav wrote:1. Please check the below code is correct or not? Or any-other ways there to split List?

Code: Select all

@SuppressWarnings("unchecked")
List<Room> joinedRooms = (List<Room>) event.getParameter(SFSEventParam.JOINED_ROOMS);
joinedRooms.get(0).getName().equals("The Lobby")

Yes it's correct.

2. The Below code only contains single room details or it contains all ROOMS joined by user?

Code: Select all

List<Room> joinedRooms = event.getParameter(SFSEventParam.JOINED_ROOMS);

All of the Rooms joined by the User.
Lapo

--

gotoAndPlay()

...addicted to flash games
balasourav
Posts: 22
Joined: 25 Jul 2017, 07:44

Re: User Disconnection Cant Get Room and Room Variables

Postby balasourav » 23 Jul 2020, 18:10

Lapo wrote:
2. The Below code only contains single room details or it contains all ROOMS joined by user?

Code: Select all

List<Room> joinedRooms = event.getParameter(SFSEventParam.JOINED_ROOMS);

All of the Rooms joined by the User.


Thanks Lapo. But I need Last Joined Room Only And its Details. At the time of disconnection I need to store user details by LastJoined Room.
For Example A user in "ROOM_1" At the time disconnection occur, I need the "ROOM_1" Details. What can i do lapo?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User Disconnection Cant Get Room and Room Variables

Postby Lapo » 24 Jul 2020, 07:07

Does your application allow for Users to join multiple Rooms at the same time?
If not, you will always just find one Room in the list provided by the event, which is obviously the last Room it was joined.

If yes, you will probably need to keep track of the last joined Room manually. By setting a property in the User object itself

For example:

Code: Select all

user.setProperty("lastRoom", Room.getId());

which can be retrieved later with:

Code: Select all

int roomId = user.getProperty("lastRoom");


Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
balasourav
Posts: 22
Joined: 25 Jul 2017, 07:44

Re: User Disconnection Cant Get Room and Room Variables

Postby balasourav » 24 Jul 2020, 07:15

Lapo wrote:
For example:

Code: Select all

user.setProperty("lastRoom", Room.getId());

which can be retrieved later with:

Code: Select all

int roomId = user.getProperty("lastRoom");


Cheers


Thank you so much lapo. I need this Exactly. In my game a user can join multiple room. at the same time.
User avatar
Rob
Posts: 53
Joined: 01 Jul 2017, 07:33

Re: User Disconnection Cant Get Room and Room Variables

Postby Rob » 24 Jul 2020, 10:21

Lapo wrote:you will probably need to keep track of the last joined Room manually. By setting a property in the User object itself

What about user.getLastJoinedRoom()?
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: User Disconnection Cant Get Room and Room Variables

Postby Lapo » 24 Jul 2020, 10:51

I would not rely on this when there's a disconnection, because the references are getting removed.
I would use the suggestion I posted earlier, with the setProperty/getProperty

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
balasourav
Posts: 22
Joined: 25 Jul 2017, 07:44

Re: User Disconnection Cant Get Room and Room Variables

Postby balasourav » 03 Aug 2020, 05:56

Lapo wrote:I would not rely on this when there's a disconnection, because the references are getting removed.
I would use the suggestion I posted earlier, with the setProperty/getProperty

Cheers

Sorry For the inconvenience lapo. I use the above one. Am a beginner that's why i ask Like that...

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 60 guests