Managing Reconnections and Handling: User Already Logged In

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

Moderators: Lapo, Bax

Post Reply
ciaoamigos
Posts: 70
Joined: 05 Sep 2021, 16:57

Managing Reconnections and Handling: User Already Logged In

Post by ciaoamigos »

I'm managing automatic reconnections in case of interruptions. When I restart the server or lose the connection, I try to re-establish the connection through a loop. When the connection comes back online, the 'SFS2X.SFSEvent.LOGIN_ERROR' event displays the following message:

Code: Select all

ErrorCode: 6,
ErrorMessage: 'User XXXXXX is already logged in the BasicExamples zone'



However, the user is no longer connected. Any suggestions on how to avoid/manage this error?"
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Managing Reconnections and Handling: User Already Logged In

Post by Lapo »

Hi,
if your client uses TCP sockets you can use the auto-reconnect feature without any manual intervention.
http://docs2x.smartfoxserver.com/Gettin ... n-hrc-plus

However, the user is no longer connected. Any suggestions on how to avoid/manage this error?"

It looks like the connection was closed only from one side (i.e. the client) but not on the server side. In these cases the server still thinks the client is connected, hence the error.

It can happen because the process of disconnection requires an exchange between client and server. If this fails you end up with a so-called "half disconnection". However, eventually the server should recognize that the client is idle (depending on your config) and disconnect the User from the server side.
Lapo
--
gotoAndPlay()
...addicted to flash games
ciaoamigos
Posts: 70
Joined: 05 Sep 2021, 16:57

Re: Managing Reconnections and Handling: User Already Logged In

Post by ciaoamigos »

Thank you for the response. I am using your JavaScript SDK. So, I believe it uses a connection through WebSocket. In this case, is the reconnection always handled automatically?
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Managing Reconnections and Handling: User Already Logged In

Post by Lapo »

Ok, so it is Websocket, which does not support automatic reconnections.
However the second part of my previous response still stands, it looks like the disconnection was not acknowledged by the server side.

Only an internal timeout will complete the disconnection. This can be the server's own "idle timeout" or the underlying TCP connection timeout.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
ciaoamigos
Posts: 70
Joined: 05 Sep 2021, 16:57

Re: Managing Reconnections and Handling: User Already Logged In

Post by ciaoamigos »

It happens when I restart the server. When the server is restarted, does it not disconnect all users?"
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Managing Reconnections and Handling: User Already Logged In

Post by Lapo »

Can you give us a step-by-step description of how it can be reproduced?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
ciaoamigos
Posts: 70
Joined: 05 Sep 2021, 16:57

Re: Managing Reconnections and Handling: User Already Logged In

Post by ciaoamigos »

Hello everyone,

I'm sharing my code and need help resolving an issue. Here is the code:

Code: Select all

setThreadSafe(false);
addEventHandler(SFSEventType.USER_JOIN_ZONE, onUserEnteZoone);
addEventHandler(SFSEventType.USER_LOGIN, OnUserConnected);

function OnUserConnected(event) {
  trace('OnUserConnected');
  var user = event.getParameter(SFSEventParam.LOGIN_NAME);

  // ...

  var loginData = event.getParameter(SFSEventParam.LOGIN_IN_DATA);
  var outData = event.getParameter(SFSEventParam.LOGIN_OUT_DATA);

  // ...

  var result = JSON.parse(con.data);
  result = result.data.result;
  if (result && result.action == 'LOGIN') {
    // var user_Session = getApi().getUserBySession(session);
    outData.putUtfString(SFSConstants.NEW_LOGIN_NAME, result.username);
    ....
    session.setProperty("token", token);
    session.setProperty("initial", initial);
    session.setProperty("$permission", DefaultPermissionProfile.STANDARD);
  }
}

function onUserEnteZoone(event) {
  trace("onUserEnteZoone");
  var user = event.getParameter(SFSEventParam.USER);
  var session = user.getSession();

  var age = new SFSUserVariable("age", session.getProperty('age'), VariableType.STRING);
  // ...
  getApi().setUserVariables(user, [age, gender, token_stream, uniqueId, avatar], true);
}

On the client side, I click the "Reconnect" button and receive this message in the server log:

Login error: Another user is already logged with the same name: XXXXXX. Requested by: { Id: 1, Type: WEBSOCKET, Logged: Yes, IP: XXXXXXX:49617 }

However, I just restarted the server, so there shouldn't be any active users.

I wonder if someone can help me understand what is causing this error and how I can resolve it. Thanks in advance for your assistance!
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Managing Reconnections and Handling: User Already Logged In

Post by Lapo »

I recommend double checking that there is no User connected before testing.
You can do that via the AdminTool > Zone Monitor and selecting the Zone you're using. There you can find the Users already logged in.

Next you can test again with your client: if the same error is triggered, check again with the AdminTool.
I suspect there's something wrong with your connection/reconnection logic.

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
ciaoamigos
Posts: 70
Joined: 05 Sep 2021, 16:57

Re: Managing Reconnections and Handling: User Already Logged In

Post by ciaoamigos »

I am using the isConnecting method, and if possible, I kindly ask you to consider adding it in future versions.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Managing Reconnections and Handling: User Already Logged In

Post by Lapo »

ciaoamigos wrote:I am using the isConnecting method, and if possible, I kindly ask you to consider adding it in future versions.

I am not sure what you mean by that. Can you elaborate?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
ciaoamigos
Posts: 70
Joined: 05 Sep 2021, 16:57

Re: Managing Reconnections and Handling: User Already Logged In

Post by ciaoamigos »

Code: Select all

   get isConnecting()
   {
      if (this._socketEngine != null)
      {
         return this._socketEngine.isConnecting;
      } else{
          return false;
               }
   }
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Managing Reconnections and Handling: User Already Logged In

Post by Lapo »

Unfortunately it's still not clear what you're trying to do.
I get that you'd like to check whether or not a client is in the middle of a connection, but this should be obvious in your code without the need for extra API calls.

A connection attempt starts when you invoke the connect() method and it completes when you receive the SFSEvent.CONNECTION event.
I am unable to follow why you would need to query an isConnecting() method at any time.

Also I am not sure why this is relevant in the issue we're discussing, i.e. duplicate login error.

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
Post Reply