How to check network connectivity from game?

hi,
I want to know if in cocos creator there is a way I can check the network connectivity of my mobile device(both android and iOS)?
thanks in advance!

reference:Networking · Cocos Creator


hi @Big_Bear,
I need to check network connectivity, and not the validity of a WebSocket connection. I could use web sockets, but then I would need to ping to some reliable server, like google or something to see whether there is internet or not. isn’t there some direct way of checking network connectivity of the device? also, if there is no direct solution, could you please tell me of any server that I can connect to as a reliable network presence and what messages I need to send/receive?
thanks!

hi,
found the solution…

if (sys.getNetworkType() == sys.NetworkType.LAN || sys.getNetworkType() == sys.NetworkType.WWAN) {
console.log(“connected”);
}
else if (sys.getNetworkType() == sys.NetworkType.NONE) {
console.log(“not connected”);
}

LAN is for lan/wifi
WWAN is for mobile data network
NONE is for no internet.

hope this helps!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.