Page 1 of 1

Auto reconnection mechanism problem

Posted: 28 Jul 2011, 05:10
by COB
I'm trying to implement reconnection mechanism and in theory it looks to be very simple. I set User Reconnection Timeframe in admin panel to 5s. In my code I have:

Code: Select all

smartFoxClient.AddEventListener(SFSEvent.CONNECTION_RETRY, OnConnectionRetry);
      smartFoxClient.AddEventListener(SFSEvent.CONNECTION_RESUME, OnConnectionResume);


Code: Select all

        void OnConnectionRetry(BaseEvent evt) {
        Debug.Log("Connection retry");
            // Freeze your GUI and provide some feedback to the Player

        void OnConnectionResume(BaseEvent evt) {
        Debug.Log("Connection resume");
            // Unfreeze the GUI and let the player continue with the game...

Then I execute smartFoxClient.KillConnection();. After that I immediately see "Connection retry" and "Connection resume" in console. However, there are also many errors concerning sending data to SFS (saying that I'm trying to use smartFoxClient set to null) after these events. In fact, in admin panel I see that user disappears, so connection was not really resumed properly. My question is why? What am I doing wrong? Do I have to login manually when connection resume is fired? I will appreciate your quick response because it's very important for me to implement.

Posted: 28 Jul 2011, 18:32
by appels
Nope, you don't have to do anything, it should reconnect automaticly. It works fine on my box. Not sure if you set the SFS variable to Null is a good test. I tested it between 2 servers by unplugging the network cable briefly and that worked fine.

Posted: 28 Jul 2011, 18:52
by COB
I test reconnection with smartFoxClient.KillConnection(); method. I don't set anything to null. When I kill the connection, I see connection retry and resume event. However, after that I see errors in console which say that smartFoxClient is set to null, so in fact connection was not reestablished properly. Also in admin panel user disappears.
I also tried to make such mechanism manually. In this scenario I periodically try to connect to server. When it is not available I receive connect event with "failed" status and this is ok, but when I eventually manage to connect, connect event is not fired at all...

Posted: 29 Jul 2011, 04:45
by COB
I have for example such method:

Code: Select all

      // Send transform to all other users
      public void DoSend() {
         SmartFox client = NetworkController.GetClient();
         ISFSObject data = new SFSObject();
         if (!isRobot) data.PutUtfString("_cmd", "t");  //We put _cmd = "t" here to know that this object contains player transform sync data.
         else {
            data.PutUtfString("_cmd", "rt"); //We put _cmd = "rt" here to know that this object contains robot transform sync data.
            data.PutInt("robot_id", robotId);
         data.PutFloat("x", this.position.x);
         data.PutFloat("y", this.position.y);
         data.PutFloat("z", this.position.z);
         data.PutFloat("rx", this.rotation.x);
         data.PutFloat("ry", this.rotation.y);
         data.PutFloat("rz", this.rotation.z);
         data.PutFloat("w", this.rotation.w);
         // We send data using SendObject method here. To optimize this you can use SendXtMessage method with custum formatted method
         // Also an extension on the server side could decide which users really need to receive the transform
         client.Send(new ObjectMessageRequest(data));

In console I see:

For Debug.Log(client.IsConnected); line:
NetworkTransform:DoSend() (at Assets/Custom Scripts/network/NetworkTransform.cs:45)
NetworkTransformSender:SendTransform() (at Assets/Custom Scripts/network/NetworkTransformSender.cs:31)
NetworkTransformSender:FixedUpdate() (at Assets/Custom Scripts/network/NetworkTransformSender.cs:24)

and then for client.Send(new ObjectMessageRequest(data)); line:
NullReferenceException: Object reference not set to an instance of an object
Sfs2X.Requests.GenericMessageRequest.ExecuteObjectMessage (Sfs2X.SmartFox sfs)
Sfs2X.Requests.GenericMessageRequest.Execute (Sfs2X.SmartFox sfs)
Sfs2X.SmartFox.Send (IRequest request)
NetworkTransform.DoSend () (at Assets/Custom Scripts/network/NetworkTransform.cs:63)
NetworkTransformSender.SendTransform () (at Assets/Custom Scripts/network/NetworkTransformSender.cs:31)
NetworkTransformSender.FixedUpdate () (at Assets/Custom Scripts/network/NetworkTransformSender.cs:24)

Method DoSend is executed in loop, so I see this log many times each time after the reconnection. I don't understand this, because client.IsConnected returns true.

Posted: 02 Aug 2011, 07:14
by COB
Here are some logs from my console.
As you can see I put some logs before I am sending any data to server. I also display SFS client property IsConnected each time. Then I use KillConnection() method, and after that you can see OnConnectionRetry and OnConnectionResume. For a while SFS is not connected, but then it is connected again. However, as you can see, I get this null exception when I am sending data to server after reconnection even when IsConnected says that connection is reestablished. Do you have any idea what is wrong?


Uploaded with

Posted: 05 Aug 2011, 07:05
by ThomasLund
KillConnection is not meant for public usage, and does not properly return the API into a sane state. It simply kills the socket and thats it.

Code: Select all

      /// <summary>
      /// Simulate an abrupt disconnection
      /// For testing/simulations only!!!
      /// </summary>
      public void KillConnection()    {

Posted: 05 Aug 2011, 09:38
by COB
I use it only for tests. I thought that this function shall be used for the SFS reconnection mechanism tests. As you can see it kills the connection and it is even reestablished, but then API does not behave properly.
So, how should I test this mechanism? My second question is how many times SFS attempts to reconnect? Only once after connection is lost and then if reconnection is not successful I should implement standard connection/login mechanism to reeastablish it?

Posted: 05 Aug 2011, 12:28
by appels
COB wrote:So, how should I test this mechanism?

Unplug network cable or block with a firewall.

Posted: 08 Aug 2011, 06:19
by COB
Ok, I will try this way. However, what does exactly "User reconnection timeframe" mean in zone configuration? How this setting works?

I'm also trying to make manual reconnection mechanism and try to reestablish manually the connection, but after I'm disconnected, calling
smartFoxClient.Connect(IP, PORT);
does not make any change. I'm still not connected even when it is called after network connection is reestablished.

Posted: 08 Aug 2011, 10:00
by appels
reconnection timeframe = the time between the client connection lost on the server and the time the server should stop and try to reconnect the lost client
Just a wild guess but i don't think i'm far off.

Posted: 09 Oct 2011, 09:41
by ericheimburg
Okay so KillConnection() basically ruins the client instance forever and should never be called. Got it.

In that case, can you add to your TODO list to make KillConnection work? Ideally with a timeout so we can emulate our apps being disconnected for a fixed amount of time?

It can be very difficult to code the pause/unpause system in some multiplayer games. And it's friggin TORTURE to code it if the only way to test is to constantly get up, yank the wire from the wall, hurry back to see what the app is doing, and the run back to plug it in again. Over and over and over.

Posted: 09 Oct 2011, 09:53
by appels
use a firewall

Re: Auto reconnection mechanism problem

Posted: 28 Sep 2012, 04:51
by copet80
I have a question though, should we always freeze the GUI while reconnecting? Say, if whatever the client does during CONNECTION_RETRY is not detrimental to decision making (i.e. submitting chat messages that can pretty much be ignored without any effect to the gameplay), is it okay to just let the user think that nothing happens while the reconnecting is going on in the background?

Also just one quick question:
- When CONNECTION_RETRY is fired, is CONNECTION_LOST also fired?

Re: Auto reconnection mechanism problem

Posted: 28 Sep 2012, 13:30
by rjgtav
Maybe you could do something like skype does. When you send a chat message while you were recoonecting, you would add a small icon telling that the message hasn't arrived yet. Then when you reconnected, you could send all the messages that were on the queue again, at a specified rate (not on a for loop).
- When CONNECTION_RETRY is fired, is CONNECTION_LOST also fired?

When you have the HRC system is activated and the client loses the connection to the server, it firstly fires a CONNECTION_RETRY event.
Then, if it managed ti successfully reconnect, you get a CONNECTION_RESUME event.
If the client wasn't able to reconnect, you get a CONNECTION_LOST event.

Re: Auto reconnection mechanism problem

Posted: 30 Sep 2012, 12:29
by ThomasLund
yeah - I think its down to your client and what specific part of it is trying to send to the server.

If its the game itself where you are running around (e.g. arena game) - I'd freeze the game and show a "reconnecting" message. For chat application, do like Skype. For a turn based game, can keep letting user add more commands into the queue.