Problem visiting url without connection

leogallardo

  • *
  • Posts: 28
Hi guys!

Im running into some troubles (and truly frustrated). I'm building a game for android and iOS (not flash)

I'm almost done with my game, I just need to sync the highest score a player has to my server, and found out that visiturl without connection makes the game freeze for around 2 minutes and then starts once it realizes there is no connection.

To sum up, I don't need to sync, is not mandatory for the game to run, I just need that once the game is launched, it tries to upload the score, but if there is no internet connection just run anyways.

I built a simplified version of what I need, with a visit url that fetches a random number from here:
http://leonardogallardo.com/game/response.php
(it's just to test my point)







Is there a way to check the connection and then do the visiturl?

If I turn on internet, it works great. If i turn off internet, it freezes for like 2 minutes before switching to the 2nd scene.

Im attaching the test game, remember it's for mobile (I tried it on a Moto G running 4.4.4)

Thanks in advance

leogallardo

  • *
  • Posts: 28
Update: I disconnected internet on my pc, and tried the game on flash (after fixing the security sandbox issues) and it worked...

Still no luck on mobile

sdieters

  • Posts: 2068
I think the problem lies that it keeps trying to fetch the URL without having a connection. This will lead into an infinite loop which will cause problems 99% of the time. Here is a little mock-up system that first tries to fetch a small text file (to eliminate the download speed differences) to check if we have a solid connection. If that is true, we know we have a connection, so now you can look for your bigger file without any problem.

HOWEVER. I'm not sure if this is the best way of doing so. Since I don't know what the [site's response] block returns if there is no connection at all.

The only thing you need to do to set this up for your own is to create a small text file (in my case "ConnectionTest") and the only thing inside that text file is the word "ConnectionTest". Upload this file to any server, and refer to that file in the first visit URL block.

Let me know if this works for you, so I can consider making a better tutorial for it (this also allows me to take a deeper look into the haXe syntax to actually check for a connection).
My new profile is TheIndieStation.
When you see a recent post with this name, i'm probably using my phone. So dont mind any typo's =p

leogallardo

  • *
  • Posts: 28
@sdieters thanks for the reply, I tried it but no luck there, still getting a 2-5 mins freeze whenever there's no connection.

I tried it on a 2nd mobile (also android) and still no luck...

« Last Edit: October 06, 2014, 06:24:19 pm by leogallardo »

leogallardo

  • *
  • Posts: 28
MODS, I created another post in the bug section, I truly believe this is bug since I'm building the code exactly as the tutorial from Web Request says.

Let me know if I need to delete this post, or if its not necessary.

Thanks :)

David07

  • Posts: 9
Hola Leogallardo, How are you?
I see you speak Spanish but I am posting this in English so anyone can read it. I saw this post a couple of days ago and also see that no one has replied to the bug you've logged so far.
I was facing a similar issue using the Parse extension from  mdotedot where I wanted to check the user's internet connection to make sure I could use the parse extension first to connect to the database and the same happened to me that the game was freezing but in my case it happens in both Flash and Mobile.
I am not a programmer but I understand a bit and I can get around this so what I did is to check the Stencyl API here  https://github.com/Stencyl/stencyl-engine/blob/master/Source/com/stencyl/behavior/Script.hx and I found that the visitURL block works as follows:

public function visitURL(URL:String, fn:Event->Void = null)
   {
      if(fn == null)
      {
         fn = defaultURLHandler;
      }
      
      try
      {
         var request = new URLRequest(URL);
         request.method = URLRequestMethod.GET;
         
         var loader = new URLLoader(request);
         loader.addEventListener(Event.COMPLETE, fn);
         
         #if flash
         loader.addEventListener(IOErrorEvent.NETWORK_ERROR, defaultURLError);
         loader.addEventListener(IOErrorEvent.IO_ERROR, defaultURLError);
         #end
      }
      
      catch(error:String)
      {
         trace("Cannot open URL.");
      }
   }

You can see there that there is  an if stament for Flash where it adds event listeners for IO error events but it doesn't do the same or similar for mobile.
Digging a bit deeper and looking at the API documentation here: http://static.stencyl.com/api/31/  I saw that there is a close() method that it closes any load operation in progress is immediately.
I couldn't get around using the visitURL block from Stencyl and adding a short piece of code to use the close() method but what I did is to remove the visitURL block completely and I added some code directly into Stencyl.
Basically what I did is to try to load a simple file that returns just a txt file with a number 1 as a response and if after 0.5 seconds a variable that I set to = site's response IS NOT variable = 1 then I assume that the connection was not successful and I couldn't set my variable equals to "1" as per the site response should be, therefore I close the connection and the game continues (previously notifying the user to check his/her internet connection ).
The code I used in the custom code block is as follows:

// I grabbed this code from the Stencyl API in github
    function visitURL(URL:String, fn:Event->Void = null)
   {

      try
      {
         var request = new URLRequest(URL);
         request.method = URLRequestMethod.GET;
         
         var loader = new URLLoader(request);
         loader.addEventListener(Event.COMPLETE, fn);
         
         #if flash
         loader.addEventListener(IOErrorEvent.NETWORK_ERROR, defaultURLError);
         loader.addEventListener(IOErrorEvent.IO_ERROR, defaultURLError);
         #end

         //making sure the connection closes after 0.5 secs so the game doesn't freeze
      runLater(500 * 1, function(timeTask:TimedTask):Void
                {
                   loader.close();
                });
      }
      
      catch(error:String)
      {
         trace("Cannot open URL.");
      }
      
   }

   visitURL("https://YOUR_URL_THAT_HOLDS_THE_SMALL_TEXT_FILE_1.txt", function(event:Event):Void
                {
           // I set my Game atribute "Connected" to the site's response which should be "1" as per the littler txt file
                    setGameAttribute("Connected", ("" + cast(event.target, URLLoader).data));
                });


If I add a screenshot of my blocks in Stencyl it probably won't help and it could be confusing but if you've got any questions please let me know.
It worked for me calling that close() method after 0.5 seconds to interrupt the connection so the game doesn't freeze in mobile, only tested it in my mobile android 4.1.2.
Hope it helps.

Cheers,

David07

captaincomic

  • *
  • Posts: 6108
Thanks, David, good find!

I can commit this fix. I understand this bug appears only on Android, right?

« Last Edit: November 16, 2014, 03:54:32 am by captaincomic »

captaincomic

  • *
  • Posts: 6108
I commited the fix for Andoird. Please let me know if this solves it for you, or if the same workaround is also needed on iOS.

David07

  • Posts: 9
Thanks captaincomic, that seems to have fixed the issue, the game doesn't freeze any more on Android. I can't tell for iOS as I am not working that platform, yet.
Quick question, where can I see the last commits and the new methods and API that Stencyl uses? Sorry, I am still newbie in some stuff. I only found the "visitURL()" method in here https://github.com/Stencyl/stencyl-engine/blob/master/Source/com/stencyl/behavior/Script.hx which seems to be quiet old.

Thanks, captaincomic for the fix! If anyone else can confirm would be great.

David07

captaincomic

  • *
  • Posts: 6108
You can find the changelog here: http://community.stencyl.com/index.php/topic,13789.msg80899.html#msg80899

And you can find the engine source files in your installation dir under plaf/haxe/lib/stencyl/1,00/com/stencyl, so when you get the latest build you'll have the latest sources.

The GitHub repository isn't updated automatically, so it is not up to date, as you have noticed.

leogallardo

  • *
  • Posts: 28
Really appreciate your help guys, all of you :) this one was a blocker for my game.

I don't have an iphone or access to one either, can anyone confirm this fix for that platform as well? That'd be awesome :)

David07

  • Posts: 9
You can find the changelog here: http://community.stencyl.com/index.php/topic,13789.msg80899.html#msg80899

And you can find the engine source files in your installation dir under plaf/haxe/lib/stencyl/1,00/com/stencyl, so when you get the latest build you'll have the latest sources.

The GitHub repository isn't updated automatically, so it is not up to date, as you have noticed.

Thanks captaincomic!!

David