Prevent user from overwriting their data?

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
moccha
Posts: 112
Joined: 13 Feb 2014, 16:09

Prevent user from overwriting their data?

Postby moccha » 01 Mar 2018, 01:00

Hello,

I have a server that needs to interact with an external database. It writes data to the database via PHP scripts accessed from the Java server via URLConnection.

When the user logs in, their data is grabbed by SmartFox from the remote server and saved into a UserVar. When they quit, all of their changed data is then sent to the remote server to be saved in the database.

There is an issue with this: What if the user logs out and then logs back into the server very fast, before the data has been sent to the remote server? The server grabs the remote data before the old data is saved, and effectively rewrites the old data back into memory. The user lost all the progress of the previous session. This problem also happens if a connection error happens with a socket and the "onExit" event is never fired by SmartFox. They lose their data in this case as well.

I think the solution is the block the user from logging in until the URLConnection event has completed. Is this possible?

If SmartFox cannot contact the remote server for whatever reason (site went down?), is there a way to repeatedly try to send the user data until the server finally accepts it, in a timed interval? Say attempting to send data every minute until it is accepted by the server.

I can explain extra if needed
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Prevent user from overwriting their data?

Postby Lapo » 01 Mar 2018, 08:45

moccha wrote:Hello,
I think the solution is the block the user from logging in until the URLConnection event has completed. Is this possible?

No, unfortunately it isn't because all those operations run in parallel and asynchronously.

Part of the problem is the multiple steps you have in your SFS->PHP->DB setup. If possible I would cut the middleman (Apache/PHP) and let SFS2X talk to the DB directly. Also, it would be highly recommended to run both SFS2X and the DB in the same private network to reduce lag between the two and improve scalability.

If SmartFox cannot contact the remote server for whatever reason (site went down?), is there a way to repeatedly try to send the user data until the server finally accepts it, in a timed interval? Say attempting to send data every minute until it is accepted by the server.

Yes, you can handle the request error and schedule a new attempt via the main Scheduler (or your own if you have create one for other purposes)

Make sense?
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
moccha
Posts: 112
Joined: 13 Feb 2014, 16:09

Re: Prevent user from overwriting their data?

Postby moccha » 01 Mar 2018, 13:32

Another team works on a separate network for the database, so it's difficult to try and set something up within the same network. I also want to support my database provider as well, so I have some natural inefficiency.

As for the PHP middleman issue, even hundred http requests tend to complete very quickly and the PHP script execution time is also very small, and the calls are not frequently made per session. I haven't quite run into the problem yet, it's mostly theoretical but was inspired by the issue of user's being disconnected, because ghost accounts have caused this issue before.

Is there a way to check if a user is already logged in when a user is trying to connect, deny the login connection and fire a "logout" event in SmartFox?

As for what to do with a user rapidly trying to logout and back in, I was thinking about storing a public list of users who recently "logged out", and SFS only erases the name from the list once the data has been saved. Users logging in could be checked against the list.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Prevent user from overwriting their data?

Postby Lapo » 01 Mar 2018, 13:48

moccha wrote:Is there a way to check if a user is already logged in when a user is trying to connect, deny the login connection and fire a "logout" event in SmartFox?

What do you mean by "logged in"? Logged in SFS2X?
SFS2X doesn't allow a User to login twice. If he's already logged in then a 2nd login will be denied (unless you activate the "force logout" option)

As for what to do with a user rapidly trying to logout and back in, I was thinking about storing a public list of users who recently "logged out", and SFS only erases the name from the list once the data has been saved. Users logging in could be checked against the list.

It could work.
In your Extension login code you could deny a user's login if he just left a few seconds ago.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
moccha
Posts: 112
Joined: 13 Feb 2014, 16:09

Re: Prevent user from overwriting their data?

Postby moccha » 01 Mar 2018, 20:47

This sounds like a good plan!

But for the denying a connection, I'm not sure if there's a setting within SmartFox, but when I login to SmartFox on one computer and then login from another computer, it actually kicks the first computer off and lets the 2nd computer login. Is there something that would cause the default behavior to differ from what you explained?
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Prevent user from overwriting their data?

Postby Lapo » 02 Mar 2018, 08:33

moccha wrote:This sounds like a good plan!

But for the denying a connection, I'm not sure if there's a setting within SmartFox,

I am not sure why you would deny a connection.
When a connection is made to the server we have no idea who the User is. Only after the login request has been sent you can start making decision as to whether or not let the user in.

but when I login to SmartFox on one computer and then login from another computer, it actually kicks the first computer off and lets the 2nd computer login.

This is because you have set the "Force logout" option in your Zone config. If you turn it off again, you will see that once the 1st client is connected the 2nd one will be refused (when logging in with the same name)

Hope it helps
Lapo

--

gotoAndPlay()

...addicted to flash games
User avatar
moccha
Posts: 112
Joined: 13 Feb 2014, 16:09

Re: Prevent user from overwriting their data?

Postby moccha » 06 Mar 2018, 14:27

Lapo wrote:I am not sure why you would deny a connection.


Sorry, i think I didn't explain it well enough. the "force logout" option was on as you said, which was all I needed to toggle to not allow connection (deny, as I wrote).

Thanks for your help and have a great day.
User avatar
Lapo
Site Admin
Posts: 22999
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Prevent user from overwriting their data?

Postby Lapo » 06 Mar 2018, 15:43

Perfect :)
Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 41 guests