Observe network connection status

Observe network connection status
0.0 0


Hi @slackmoehrle @pandamicro

How can I observe network status so I can alert user to check their device connection when required?


@yinjimmy @nite can anyone help with this??

  1. ping with your owner server
  2. https://github.com/dustturtle/RealReachability (iOS)
    https://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html (Android)
    or check network connection with timer
    // Check network connection
    private boolean isNetworkConnected(){
    ConnectivityManager connectivityManager 
            = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();    
  3. use websocket https://www.websocket.org/echo.html ws://echo.websocket.org


Hi @yinjimmy
I am using cocos creator, js version, I donno if I can use websockets in js? (Can you let me know procedure if we can, or may be a sample code link somewhere would be helpful, Thanks :))

So We are using xhr GET request to ping httpbin server as it was there in one of cocos test case code.
In browser it works fine but on Android device for the following case Its not working fine.

  1. Connect to wifi, Open app: Shows Connected to internet after GET request completes.
  2. The above GET call is called every 10 seconds using scheduler.
  3. So now when we off Wifi on device, The status is still Connected to Internet even after call completes.
  4. But on browser once when new GET call fails after switching off wifi, it changes back to "“Not connected”.

My guess is it is caching the first call when conected to internet? I have been using random number attached to the GET url, so caching wont happen, but is it the case that this doesn’t work on Device?

Let me know if there is any work around for this.
Thanks :slight_smile:


I’m back after Chinese NEW YEAR.

whats the xhr GET result when device is in flight mode.


There was this error on android, JavaConnection Error something, so xhr readyChangeState is never getting compelted which was my basis on changing connectionStatus variable(xhr getting completed).
So I put the code I want to execute when not connected to internet in xhr.onError event. Everything works fine now.