Page 1 of 1

Managing Reconnections and Handling: User Already Logged In

Posted: 24 Feb 2024, 07:38
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?"

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 26 Feb 2024, 08:59
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.

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 28 Feb 2024, 07:48
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?

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 28 Feb 2024, 10:01
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

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 28 Feb 2024, 15:01
by ciaoamigos
It happens when I restart the server. When the server is restarted, does it not disconnect all users?"

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 28 Feb 2024, 15:37
by Lapo
Can you give us a step-by-step description of how it can be reproduced?

Thanks

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 28 Feb 2024, 16:20
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!

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 28 Feb 2024, 17:26
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

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 29 Feb 2024, 08:55
by ciaoamigos
I am using the isConnecting method, and if possible, I kindly ask you to consider adding it in future versions.

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 29 Feb 2024, 09:13
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

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 29 Feb 2024, 09:18
by ciaoamigos

Code: Select all

   get isConnecting()
   {
      if (this._socketEngine != null)
      {
         return this._socketEngine.isConnecting;
      } else{
          return false;
               }
   }

Re: Managing Reconnections and Handling: User Already Logged In

Posted: 01 Mar 2024, 09:03
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