Check ping of multiple servers

Post here your questions about the Flash / Flex / Air API for SFS2X

Moderators: Lapo, Bax

Progamer
Posts: 13
Joined: 22 Mar 2017, 15:09

Check ping of multiple servers

Postby Progamer » 04 Apr 2017, 07:31

Hi,

I am developing a multiplayer game using smartfox. I have to collect data(like ping etc.) from multiple servers. So, I am connecting to them simultaneously by creating Smartfox object for each server. After connecting to one server, I am destroying objects of every server. But problem, I am facing is that my game hangs randomly. So am I doing something wrong ? Thanks in advance.
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Check ping of multiple servers

Postby Lapo » 04 Apr 2017, 07:54

Hi,
if your needs to measure the ping of a server you can call SmartFox.enableLagMonitor(). This will generate a PING_PONG event that keeps track of the lag time (ping).

You can learn more by consulting the client side documentation for the platform in use:
http://docs2x.smartfoxserver.com/

(See API Documentation in the left side menu)

Cheers
Lapo
--
gotoAndPlay()
...addicted to flash games
Progamer
Posts: 13
Joined: 22 Mar 2017, 15:09

Re: Check ping of multiple servers

Postby Progamer » 04 Apr 2017, 08:03

That is exactly what I am doing, but my game hangs randomly after I started to check for pings. What I wanted to know is whether creating five different Smartfox objects that are pinging five different locations simultaneously, the correct approach?
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Check ping of multiple servers

Postby Lapo » 04 Apr 2017, 08:12

There is no problem opening multiple connections in a single client. In fact that's the way you can do stress testing by running hundreds of connections (or even thousands) in a single application.

What does it mean that the game hangs exactly? What happens? Do you have a specific error?

Also I am not sure I get how this works exactly. While the game is running and connected to a specific server you keep pinging 4 more servers? Why is that, if I may ask?

Thanks
Lapo

--

gotoAndPlay()

...addicted to flash games
Progamer
Posts: 13
Joined: 22 Mar 2017, 15:09

Re: Check ping of multiple servers

Postby Progamer » 04 Apr 2017, 08:26

When the user is playing the game we do not check for other server pings. We are doing this because we have to show user the ping for every server and on that basis he/she can select the appropriate server and then play our game.

Also there is no specific error windows just says application not responding.
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Check ping of multiple servers

Postby Lapo » 04 Apr 2017, 09:05

Sorry, but I still don't understand what it means "not responding". Can you be less vague?
It doesn't respond to what? Does it freeze entirely?

I am pretty sure if you debug the client you should be able to find out more about this "not responding" issue.
Lapo

--

gotoAndPlay()

...addicted to flash games
Progamer
Posts: 13
Joined: 22 Mar 2017, 15:09

Re: Check ping of multiple servers

Postby Progamer » 04 Apr 2017, 10:09

It freezes entirely. And when i try to pause my application from flashdevelop IDE at the time of freeze, so It says no actionscript is running.
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Check ping of multiple servers

Postby Lapo » 04 Apr 2017, 10:29

So how do you know the freeze is caused by the SFS API and not something else in your code?
Lapo

--

gotoAndPlay()

...addicted to flash games
Progamer
Posts: 13
Joined: 22 Mar 2017, 15:09

Re: Check ping of multiple servers

Postby Progamer » 04 Apr 2017, 13:50

Because when I comment the ping checking code the freeze does not happen anymore.
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Check ping of multiple servers

Postby Lapo » 04 Apr 2017, 13:55

Can you show us the ping checking code?
Lapo

--

gotoAndPlay()

...addicted to flash games
Progamer
Posts: 13
Joined: 22 Mar 2017, 15:09

Re: Check ping of multiple servers

Postby Progamer » 04 Apr 2017, 14:03

Here is the class which I am using to calculate the ping of a server. In order to connect to every server this class instance is made.

Code: Select all

package com.apargames.sapc.managers
{
   import com.sapc.core.Root;
   import com.sapc.data.Constants;
   import com.sapc.data.GameScreens;
   import com.sapc.utils.Utils;
   import com.smartfoxserver.v2.bitswarm.AirUDPManager;
   import com.smartfoxserver.v2.core.SFSEvent;
   import com.smartfoxserver.v2.entities.data.ISFSObject;
   import com.smartfoxserver.v2.entities.data.SFSObject;
   import com.smartfoxserver.v2.requests.ExtensionRequest;
   import com.smartfoxserver.v2.requests.LoginRequest;
   import com.smartfoxserver.v2.requests.LogoutRequest;
   import com.smartfoxserver.v2.SmartFox;
   import flash.events.HTTPStatusEvent;
   import flash.filesystem.File;
   import flash.net.URLLoader;
   import starling.events.EventDispatcher;
   
   /**
    * ...
    * @author Ayush
    */
   public class NetworkInfoManager extends EventDispatcher
   {
      private var m_sfs:SmartFox = null;
      private var _isDebug:Boolean = false;
      private var _isConnectionInProgress:Boolean = false;
      
      private var _udpManager:AirUDPManager = null;
      private var m_myPing:int;
      
      private var m_isResponseRecieved:Boolean = true;
      private var m_sendingPingInterval:Number = 0;
      
      private var m_udpTestOffsetValue:int = 100;
      
      private var m_eachPacketHopCount:int = 1;
      private var m_udpHopCountMaxValue:int = 11;
      
      private var m_udpTestPacketCount:int = 1;
      private var m_udpTestPacketMaxCount:int = 11;
      
      private var m_playerCount:int = 0;
      
      private var m_serverNum:int = -1;
      
      private var m_userCountExtRequest:ExtensionRequest = null;
      
      private var ldr:URLLoader = null;
      private var m_startTime:Number = 0;
      private var m_stopTime:Number = 0;
      
      private var m_showPlayerCount:String = "false";
      
      private var m_isEnable:Boolean = true;
      
      public function NetworkInfoManager()
      {
         super();
         try
         {
            m_sfs = new SmartFox(_isDebug);
            _udpManager = new AirUDPManager();
            Utils.writeLogToFile("smartfox initialized: " + "\n");
         }
         catch (e:Error)
         {
            //to do in catch
            Utils.writeLogToFile("error constructor: " + e.message + "\n");
            AnalyticsManager.getInstance().submitErrorLog(e, "error constructor: " + e.message);
            Root.root.setDebugText("error constructor: " + e.message);
            trace("inside catch: " + e.message);
         }
      }
      
      public function initializeSmartfox():void
      {
         Root.root.setDebugText("initializing network manager...");
         
         m_sfs.addEventListener(SFSEvent.CONNECTION, onConnection);
         m_sfs.addEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost);
         m_sfs.addEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, onConfigLoadSuccess);
         m_sfs.addEventListener(SFSEvent.CONFIG_LOAD_FAILURE, onConfigLoadFailure);
         m_sfs.addEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);
         m_sfs.addEventListener(SFSEvent.UDP_INIT, onUdpInit);
         m_sfs.addEventListener(SFSEvent.PING_PONG, onCalculatePing);
         
         //ldr = new URLLoader();
         //ldr.addEventListener(HTTPStatusEvent.HTTP_STATUS, ldrStatus);
      }
      
      /*private function ldrStatus(e:HTTPStatusEvent):void
      {
         if (e.status == 200)
         {
            m_stopTime =
         }
      }*/
      
      public function removeListeners():void
      {
         removeLoginListener();
         m_sfs.removeEventListener(SFSEvent.CONNECTION, onConnection);
         m_sfs.removeEventListener(SFSEvent.CONNECTION_LOST, onConnectionLost);
         m_sfs.removeEventListener(SFSEvent.CONFIG_LOAD_SUCCESS, onConfigLoadSuccess);
         m_sfs.removeEventListener(SFSEvent.CONFIG_LOAD_FAILURE, onConfigLoadFailure);
         //m_sfs.removeEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);
         m_sfs.removeEventListener(SFSEvent.UDP_INIT, onUdpInit);
         //m_sfs.removeEventListener(SFSEvent.PING_PONG, onCalculatePing);
      }
      
      public function connectToServer():void
      {
         if (Utils.isConnectedToInternet())
         {   
            //log(this, "Connecting Server ");
            try
            {
               var l_file:File = File.documentsDirectory.resolvePath("sfs-config.xml");
               m_sfs.loadConfig(l_file.nativePath);
            }
            catch (e:Error)
            {
               Utils.writeLogToFile("error connectToServer: " + e.message + "\n");
               AnalyticsManager.getInstance().submitErrorLog(e, "error connectToServer: " + e.message);
               Root.root.setDebugText("error connectToServer: " + e.message);
            }
         }
         else
         {
            Utils.writeLogToFile("Not connected to internet." + "\n");
            AnalyticsManager.getInstance().submitInfoLog("Not connected to internet.");
            Root.root.setDebugText("Not connected to internet...");
         }
      }
      
      public function disConnectFromServer():void
      {
         /*var l_logout:LogoutRequest = new LogoutRequest();
         m_sfs.addEventListener(SFSEvent.LOGOUT, onLogout);
         m_sfs.send(l_logout);*/
         
         //removeAllListeners();
         removeListeners();
         //m_sfs.disconnect();
         m_isEnable = false;
         //m_sfs.enableLagMonitor(false, 2);
         
         trace("server disconnected.=====================================");
         
         //m_sfs = null;
      }
      
      private function onLogout(e:SFSEvent):void
      {
         //m_sfs.disconnect();
         //removeAllListeners();
      }
      
      private function removeAllListeners():void
      {
         removeListeners();
         m_sfs.removeEventListener(SFSEvent.EXTENSION_RESPONSE, onExtensionResponse);
         m_sfs.removeEventListener(SFSEvent.PING_PONG, onCalculatePing);
      }
      
      private function onUdpInit(e:SFSEvent):void
      {
         if (e.params.success)
         {
            log(this, "UDP initialised..");
            //TweenMax.delayedCall(2, removeUdpPopup);
            
            /*m_sfs.send(m_udpTestCmd);
            m_sendingPingInterval = Number(m_myPing + (m_udpTestOffsetValue * m_eachPacketHopCount));
            m_isResponseRecieved = false;*/
            
            Root.root.setDebugText("UDP initialised...");
            
            Utils.writeLogToFile("UDP initialised.." + "\n");
            
            AnalyticsManager.getInstance().submitInfoLog("UDP Initialised.");
            
            /*if (m_userCountExtRequest == null)
            {
               m_userCountExtRequest = new ExtensionRequest(Constants.CMD_PLAYER_COUNT);
            }
            
            m_sfs.send(m_userCountExtRequest);*/
         }
         else
         {
            log(this, "Error during UDP initialisation..!!", e.params.errorMessage);
            Utils.writeLogToFile("Error during UDP initialisation..!!" + e.params.errorMessage + "\n");
            AnalyticsManager.getInstance().submitInfoLog("Error during UDP initialisation..");
            
            Root.root.setDebugText("Error during UDP initialisation..!!");
            
            if (!m_sfs.udpInited)
            {
               m_sfs.initUDP(_udpManager);
            }   
         }
      }
      
      private function onExtensionResponse(e:SFSEvent):void
      {
         var l_cmd:String = e.params.cmd as String;
         
         switch(l_cmd)
         {
            case Constants.EXT_RES_PLAYER_COUNT:
               trace("playercount is: " + SFSObject(e.params.params).getInt("player_count"));
               m_playerCount = SFSObject(e.params.params).getInt("player_count") - 1;
               if (m_myPing != 0)
               {
                  Root.root.setDetailsForServer(m_playerCount, m_myPing, m_serverNum);
               }
               break;
         }
      }
      
      private function onConfigLoadFailure(e:SFSEvent):void
      {
         trace("onConfigLoadFailure================================");
      }
      
      private function onConfigLoadSuccess(e:SFSEvent):void
      {
         trace("onConfigLoadSuccess================================");
      }
      
      private function onConnectionLost(e:SFSEvent):void
      {
         trace("onConnectionLost================================");
      }
      
      private function onConnection(e:SFSEvent):void
      {
         //trace("onConnection================================");
         
         if (e.params.success)
         {
            var loginRequest:LoginRequest = null;
            addLoginListener();
            
            if (Root.root.steamManager.isAvailable)
            {
               var params:ISFSObject = SFSObject.newInstance();
               params.putUtfString("suserId", Root.root.steamManager.userId);
               params.putInt("sappid", Root.root.steamManager.mySteamworks.getAppID());
               loginRequest = new LoginRequest(Root.root.steamManager.userName, "", "SAPhysicsZone", params);
            }
            else
            {
               loginRequest = new LoginRequest();
            }
            m_sfs.send(loginRequest);
         }
         else
         {
            log(this, "Connection failure due to server is down");
            Utils.writeLogToFile("Connection failure due to server is down" + "\n");
            AnalyticsManager.getInstance().submitErrorLog(e, "Connection failure due to server is down.");
            //Root.root.uiManager.showPopUpUI(GameScreens.POP_UP_NOT_CONNECTED_TO_SERVER, null, "Connection failure due to server is down");
            
            Root.root.setDebugText("Connection failure due to server is down...");
            
            //Root.root.uiManager.showNotificationPopUp(GameScreens.POP_UP_NOT_CONNECTED_TO_SERVER, "Connection failure due to server is down. Please restart game.", null);
         }
      }
      
      private function addLoginListener():void
      {
         m_sfs.addEventListener(SFSEvent.LOGIN, onLogin);
         m_sfs.addEventListener(SFSEvent.LOGIN_ERROR, onLoginError);
      }
      
      private function removeLoginListener():void
      {
         m_sfs.removeEventListener(SFSEvent.LOGIN, onLogin);
         m_sfs.removeEventListener(SFSEvent.LOGIN_ERROR, onLoginError);
      }
      
      private function onLoginError(e:SFSEvent):void
      {
         
      }
      
      private function onLogin(e:SFSEvent):void
      {
         //removeLoginListener();
         
         m_sfs.mySelf.properties.steamid = Root.root.steamManager.userId;
         
         m_sfs.enableLagMonitor(true, 2);
         
         //trace("on Login success=======================================");
         
         Utils.writeLogToFile("on Login success.." + "\n");
         AnalyticsManager.getInstance().submitInfoLog("on login success.");
         
         m_sfs.initUDP(_udpManager);
         //Root.root.uiManager.showPopUpUI(GameScreens.POP_UP_UDP);
      }
      
      private function onCalculatePing(e:SFSEvent):void
      {
         trace("ping: " + e.params.lagValue);
         
         m_myPing = int(e.params.lagValue);
         
         if (!m_isEnable)
         {
            m_sfs.enableLagMonitor(false, 2);
            m_sfs.disconnect();
            m_sfs = null;
            
            trace("server is really disconnected.===================================================");
            
            return;
         }
         
         if (m_showPlayerCount == "true")
         {
            if (m_userCountExtRequest == null)
            {
               m_userCountExtRequest = new ExtensionRequest(Constants.CMD_PLAYER_COUNT);
            }
            m_sfs.send(m_userCountExtRequest);
            
            Utils.writeLogToFile("calculating ping for: " + m_sfs.currentIp + "\n");
            
            //Root.root.setDetailsForServer(m_playerCount, m_myPing, m_serverNum);
            //removeListeners();
         }
         else
         {
            Root.root.setDetailsForServer(m_playerCount, m_myPing, m_serverNum);
         }
      }
      
      public function get serverNum():int
      {
         return m_serverNum;
      }
      
      public function set serverNum(value:int):void
      {
         m_serverNum = value;
      }
      
      public function get showPlayerCount():String
      {
         return m_showPlayerCount;
      }
      
      public function set showPlayerCount(value:String):void
      {
         m_showPlayerCount = value;
      }
      
      public function get isEnable():Boolean
      {
         return m_isEnable;
      }
      
      public function set isEnable(value:Boolean):void
      {
         m_isEnable = value;
      }
   }
}
User avatar
Lapo
Site Admin
Posts: 23007
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Check ping of multiple servers

Postby Lapo » 04 Apr 2017, 15:14

Quite a bit of code. By looking at it I can't find anything particularly outstanding...
My suggestion would be to run the code in a debugger or with the debug Flash player and see exactly what is going on.

Let us know
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X ActionScript 3 API”

Who is online

Users browsing this forum: No registered users and 15 guests