Sign In Register

How can we help you today?

Start a new topic
Answered

Internet Connectivity vs GS.GameSparksAvailable

When our game starts we need to fetch data from GameSparks server as soon has GS is ready. So I'm adding a listener to GS.GameSparksAvailable that starts downloading levels from GS server. But when there's no internet connectivity my GameSparksAvailable listener is not called.


So what is the recommended way to test if internet connection is available when using GS? Use a GS listener or event or parameter that I don't about or use something like UnityEngine.Network.TestConnection()

  

public class GameBootstrapper : MonoBehaviour
    {
        void Start ()
        {
            GS.GameSparksAvailable += HandleGameSparksAvailable;
        }

        //not call if no internet connectivity
        private void HandleGameSparksAvailable (bool value)
        {
            //what is meaning of the bool parameter ???
            DownloadLevelFromGS ();
            GS.GameSparksAvailable -= HandleGameSparksAvailable;
        }

        private void DownloadLevelFromGS ()
        {
            ...
        }
        
        //I don't know when to call this
        private void InitDefaultLevel ()
        {
            ...
        }
    }

  


Best Answer

The bool is to let you know if GameSparks is available. If you put   

void Start ()
        {
            GS.GameSparksAvailable += HandleGameSparksAvailable;
        }
 
        //not call if no internet connectivity
        private void HandleGameSparksAvailable (bool value)
        {
            //what is meaning of the bool parameter ???
            Debug.Log(value);
        }

  You'll see that it's equal to true if you're connected to GameSparks and false if not. It's not required for anything but available if you need it. At the moment you have to check if GameSparks is not available and code around it yourself, but an Offline mode is in the plans in the next 4 weeks or so.


1 person has this question

Unity has some built in functionality in this regard with it's Application.internetReachability and NetworkReachability


  

void OnGUI()
	{
		if (Application.internetReachability != NetworkReachability.NotReachable)
		{
			// do some stuff 
		}
		else
		{
			// No connection
		}
	}

  

Hi,

Somewhat related to this, I'm fairly sure we're seeing cases in our project where GameSparks doesn't become 'available' on iOS after a suspend/resume, despite having good internet connectivity (and being available and authenticated before the suspend)

Is there anything we could do or should be doing do in these cases to retry/recheck? (When this happens, another suspend/resume seems to make it become available again)

 


1 person likes this

 You could possibly try using OnApplicationPause  

void OnApplicationPause (bool pause)
{
   if(pause)
   {
       // we are in background
   }
   else
   {
       // we are in foreground again.
	if (!GS.Available)
	{
		GS.Reconnect();
	}
   }
}

  I'll have to test this myself, if I find a different solution I'll post back here 

Application.internetReachability indicates if the device has wifi ON or not. The Wifi can be ON and still have no internet access. Network.TestConnection() can be use to detect internet access. But since GS tries to open a websocket at startup, it should notify the application when it fails connect to make the game starts in "LOCAL" mode. BTW I still don't know the meaning of bool parameter in GameSparksAvailable listener.

Answer

The bool is to let you know if GameSparks is available. If you put   

void Start ()
        {
            GS.GameSparksAvailable += HandleGameSparksAvailable;
        }
 
        //not call if no internet connectivity
        private void HandleGameSparksAvailable (bool value)
        {
            //what is meaning of the bool parameter ???
            Debug.Log(value);
        }

  You'll see that it's equal to true if you're connected to GameSparks and false if not. It's not required for anything but available if you need it. At the moment you have to check if GameSparks is not available and code around it yourself, but an Offline mode is in the plans in the next 4 weeks or so.


1 person likes this

Does this mean, if the connection is lost, the GS.GameSparksAvailable callback should be called? This doesn't seem to work now. This is not necessarily needed, but it would be nice to have

Hi What happens to this?? if the connection is lost, the GS.GameSparksAvailable callback should be called?  Thanks this topics is very helpfull

Hi Luis,


Actually since this topic was created we have added a new function to the Unity SDK called SetDurable, this will allow you to queue up requests if a connection is lost.

Once the connection is re-established, the relevant requests will be sent. 


Hope this helps out.

Thanks,


Oisin

Hi Oisin,



I am testing SetDurable on a LogEventRequest with a Send completion handler that writes some Debug.Log text depending on the response.HasErrors.

 

new LogEventRequest ()
	.SetEventKey ("TakeTurn")
	.SetDurable(true)
	.Send (response => {
                if (response.HasErrors) {
			Debug.Log ("Event Request ERROR!!!");
		} else {
			Debug.Log ("Event Request OK");
		}	
	});						

 

With internet connection, that LogEventRequest will generate on the console the "GS: SEND:" info followed by the "GS: RECV:" info and my "Event Request OK" text.


With no internet connection, the console returns the "GS: SEND:" info followed by my "Event Request ERROR!!!" message, 


and when the connection comes back, the request is sent as a second attempt (SetDurable), the console returns the "GS: SEND:" info, followed by the "GS: RECV:" info but none of my texts "Event Request OK" nor "Event Request ERROR".


Can you please confirm whether the Send completion handler is supposed to be executed on a SetDurable request when triggered by a re-established connection?


Thanks,



Stephane




Hey Stephane,

This is the correct behavior, but you can attach your own callback to any of this event handlers if you want to control what happens when the game reconnects.

-Sean

 

void Start () { GS.GameSparksAvailable += HandleGameSparksAvailable; } //not call if no internet connectivity private void HandleGameSparksAvailable (bool value) { //what is meaning of the bool parameter ??? Debug.Log(value); }


This does not work

When the application is launched with no internet, HandleGameSparksAvailable is never called.


1 person likes this

Dan, GS.GameSparksAvailable is a callback that only triggers when GameSparks becomes available. If you have no internet, GameSparks won't be available, and therefore the callback (HandleGameSparksAvailable) won't be triggered. If you need something to run whether connected to GameSparks or not, you need to isolate it and call it manually on the even that GameSparks is unable to connect. In my case, I ran a Coroutine that after 5 seconds, considered itself timed out (and subsequently removed the DefaultPlatform Component to stop it from constantly sending a connecting request every second)..

IMHO the API is a bit confusing. If GameSparksAvailable does not trigger when the connection is lost why does the method signature contain a bool that holds "isAvailable" information. Shouldn't the method signature be: 


Action GameSparksOnAvailable


instead of


Action<bool> GameSparksAvailable


Because when you finally connect to GameSparks, the callback is triggered, and then when you subsequently get disconnected, it will pass false rather than true. I was previously talking about the case where you never connect to GS in the first place.

 

Login to post a comment