How do I check internet connection on mobile?

0 favourites
  • 15 posts
From the Asset Store
A simple fun and stylish endless scroller game ready to be customized and published.
  • Hi guys,

    how can i check the internet connection on mobile? I used the Browser plugin, with the "is online" function but the problem is, when i export through phonegap with cli-8.0.0 it will not work (with the cli-6.5.0 it works). I need the cli-8.0.0 for my other plugins to work..

    Do somebody of you made it to work somehow? Or any other ideas/plugins available?

    Thanks in advance!

  • I've written software in other languages that needed internet connections and what I've discovered is that many functions like .isOnline are querying some driver or some device and often don't really know if the device is connected or not.

    One of the easiest ways in Construct and one pretty much guaranteed to pass or fail, is to do an Ajax call to a website you know is going to be up and one that accepts Ajax calls from anyone. Or, create your own php file on a webserver and do an Ajax call to it.

    Google has a bunch of resources that accept open Ajax calls. One is for jquery, both current and past versions:

    ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js

    + System: On start of layout
    -> AJAX: Request "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" (tag "check")
    
    + AJAX: On "check" completed
    -> Text: Set text to "Passed"
    
    + AJAX: On any error
    -> Text: Set text to "Failed"
    

    That simple test will pass.

    So, in theory, if you set the url to the current jquery version, it should, I say should, still work years later. Version 1.2.6 came out in 2007 but if you put this url in that check:

    "https://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"

    It still passes.

    The thing you have to keep in mind is that Google has a HUGE history of changing their mind about things so having it check your own url is, most of the time, a safer bet. You'll still have to worry about your host servers crashing, internet outages and such but at least the domain is yours and you can take it wherever you wish.

  • Don't want to/can't host a no-op endpoint then you could use example.com. Better yet, make a HEAD call rather than a GET call, which reduces the size from 1,270 bytes to 606 bytes.

    To be super fast though, just do the TCP connection, only three packets and no other overhead that way.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Don't want to/can't host a no-op endpoint then you could use example.com. Better yet, make a HEAD call rather than a get call, which reduces the size from 1,270 bytes to 606 bytes.

    To be super fast though, just do the TCP connection, only three packets and no other overhead that way.

    Hi Buddy,

    can you may explain this a little bit more in detail? :)

    This sounds good! Can you write down the each steps? :)

    Thanks!

  • He's saying if you don't want to host your own file/site, use example.com as the url. And don't use the ajax post or get, use the head call, which I'd agree with.

    The problem I have with any site or domain other than your own, you can't guarantee that they'll always be there.

    As far as the tcp, I assume he means websockets. If c3 or c2 has some way of sending tcp/udp packets without writing JS, I'd love to know about it.

  • He's saying if you don't want to host your own file/site, use example.com as the url. And don't use the ajax post or get, use the head call, which I'd agree with.

    The problem I have with any site or domain other than your own, you can't guarantee that they'll always be there.

    As far as the tcp, I assume he means websockets. If c3 or c2 has some way of sending tcp/udp packets without writing JS, I'd love to know about it.

    Thank you too for answer! But can you show me how you mean it? Your first tip works for me :)

    Can you make write down again each step? thanks buddy

  • I personally couldn't get example.com to work. But to do a "HEAD" call which only returns the header of the site you're contacting and not all of the content (which is smaller memory wise and a fraction of a second faster), change the first line in my example to this:

    -> AJAX: Send "" to URL "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" (method "HEAD", tag "check")
    
  • I personally couldn't get example.com to work. But to do a "HEAD" call which only returns the header of the site you're contacting and not all of the content (which is smaller memory wise and a fraction of a second faster), change the first line in my example to this:

    > -> AJAX: Send "" to URL "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" (method "HEAD", tag "check")
    

    i did that but nothing happens.. text will not change

  • And I run this:

    + System: On start of layout
    -> AJAX: Send "" to URL "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" (method "HEAD", tag "example")
    
    + AJAX: On "example" completed
    -> Text: Set text to "passed"
    
    + AJAX: On "example" error
    -> Text: Set text to "failed"
    

    And it comes back passed which looks exactly like what you have.

  • Fengist explained what I meant. No idea if you can make a TCP handshake happen in Construct/JavaScript but if you could then that would be good.

    My point about using example.com is that it is one of four special domains that are specifically reserved for when you need a real domain, that works, but doesn't actually do anything.

    • example.com
    • example.net
    • example.org
    • example.edu

    You could use a domain that's probably always there, but this is better. I've never known it return anything other than a 200.

    You could use an endpoint you host, which means you can have control and responsibility of keeping it available.

    The acid test for choosing whether to use you own endpoint or to use example.* is this: Do you want to test whether the internet is accessible, or do you want to test if your specific resource is accessible?

  • Fengist explained what I meant. No idea if you can make a TCP handshake happen in Construct/JavaScript but if you could then that would be good.

    My point about using example.com is that it is one of four special domains that are specifically reserved for when you need a real domain, that works, but doesn't actually do anything.

    • example.com
    • example.net
    • example.org
    • example.edu

    You could use a domain that's probably always there, but this is better. I've never known it return anything other than a 200.

    You could use an endpoint you host, which means you can have control and responsibility of keeping it available.

    The acid test for choosing whether to use you own endpoint or to use example.* is this: Do you want to test whether the internet is accessible, or do you want to test if your specific resource is accessible?

    For some reason your example doesn't work for me...

    Everytime it says "fail". What am i doing wrong here? :(

  • I don't think you're doing anything wrong. Ajax needs a special 'header' on the other end to allow it to interact with the website. The google url has that header. I don't think example.com does.

    Just do the head post to the google url or one of your own. It's fast and there are no ajax results actually returned so it doesn't consume any memory.

    Here, this definitely works on my system:

    https://www.twistedvoid.com/c3examples/headcheck.c3p

  • I don't think you're doing anything wrong. Ajax needs a special 'header' on the other end to allow it to interact with the website. The google url has that header. I don't think example.com does.

    Just do the head post to the google url or one of your own. It's fast and there are no ajax results actually returned so it doesn't consume any memory.

    Here, this definitely works on my system:

    https://www.twistedvoid.com/c3examples/headcheck.c3p

    Thank you dude!

  • Is the problem CORS?

  • None of these methods are working for me on device. I have 'always connected' regardless if I am actually connected or not and I'm doing an AJAX check to

    "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"

    every 2 seconds. (I tried various other websites too)

    Like everyone said the browser event doesn't work on a device and nor does the PlatformInfo.ConnectionRTT method work (always returns 0 on device).

    Regarding CORS if I add a header "Access-Control-Allow-Origin" "*" then I just get Not COnnected all the time.

    I also tried various plugins of which all had other compatibility issues.

    I would have thought this was a very commonly used feature in most mobile games, so I'm surprised there isn't an obvious solution.

    Where to turn next?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)