Network Connection

Is there any way to detect network connection for CPP output? it is quite important. I tried using haxe.Http, but it causes crashes on Windows and Android for several app launches (security errors?). Thanks!

What about using openfl.net.URLLoader? Does that crash?

Hi! I tried to create Network extension following this: Internet Connection?
lime -verbose
haxelib run openfl setup -alias
lime create extension E:\openflextensions\Network
But these errors are showed and there is no ndll folder created.

Called from ? line 1
Called from CommandLineTools.hx line 1895
Called from CommandLineTools.hx line 22
Called from a C function
Called from CommandLineTools.hx line 113
Called from CommandLineTools.hx line 694
Called from utils/CreateTemplate.hx line 41
Called from /usr/share/haxe/std/neko/_std/sys/FileSystem.hx line 38
Uncaught exception - std@sys_rename

actuate: [1.8.9]
box2d: [1.2.3]
extension-android-immersive: [1.1.0]
hxcpp: [4.0.64]
layout: [1.2.1]
lime-samples: [7.0.0]
lime: [7.6.3]
openfl-samples: [8.7.0]
openfl: [8.9.5]
starling: [2.5.1]

Wait, are you on Windows or a Unix-based system? Because I think one of those file paths is wrong…

Either way, I’d suggest running lime create extension Network without “E:\openflextensions\”. That will put the extension inside the current directory, but once it’s made you can move it.

1 Like

hey, thank you. i was digging into it. learning new things is always awesome! :slight_smile:

When compiling .apk file I had this error
> E:\app\bin\android\bin\app\src\main\java\org\haxe\lime\GameActivity.java:143: error: no suitable method found for add(Network)
> extensions.add (new org.haxe.extension.Network ());
> ^
> method Collection.add(Extension) is not applicable
> (argument mismatch; Network cannot be converted to Extension)
> method List.add(Extension) is not applicable
> (argument mismatch; Network cannot be converted to Extension)
> Note: Some input files use or override a deprecated API.
> Note: Recompile with -Xlint:deprecation for details.
> Note: E:\app\bin\android\bin\app\src\main\java\org\libsdl\app\SDL.java uses unchecked or unsafe operations.
> Note: Recompile with -Xlint:unchecked for details.
> Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
> 1 error
> :app:compileReleaseJavaWithJavac FAILED
//////////

Network.hx

package;
import lime.system.CFFI;
import lime.system.JNI;

class Network {
	
	#if android
		public static function isConnected():Bool {
			return network_is_connected_jni;
		}
		private static var network_is_connected_jni = 
              JNI.createStaticMethod("org.haxe.extension.Network", "isConnected", "()Z");
	#end
	
}

Network.java

package org.haxe.extension;


import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import org.haxe.extension.Extension;

public class Network {
	private static NetworkInfo getActiveNetworkInfo() {
	
				ConnectivityManager cm =(ConnectivityManager)Extension.mainContext.getSystemService(Context.CONNECTIVITY_SERVICE);	
		        return cm.getActiveNetworkInfo();
		
		
		
	}
	
	public static boolean isConnected() {
		
		
				NetworkInfo activeNetwork = getActiveNetworkInfo();
		        return activeNetwork != null && activeNetwork.isConnected();
		
		
		
	}
}

include.xml
<?xml version="1.0" encoding="utf-8"?>

	<ndll name="network" unless="android"/>
	
	<!-- Use the following for an Android Java extension, not needed otherwise -->
	
	<dependency name="network" path="dependencies/android" if="android" />
	<android extension="org.haxe.extension.Network" />
	
</project>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.haxe.extension.network" >
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
	
	
</manifest>

Build.xml

<xml>
	
	<include name="${HXCPP}/build-tool/BuildCommon.xml"/>
	
	<files id="common">
		
		<compilerflag value="-Iinclude"/>
		
		<file name="common/ExternalInterface.cpp"/>
		<file name="common/Network.cpp"/>
		
	</files>
	
	<set name="SLIBEXT" value=".lib" if="windows"/>
	<set name="SLIBEXT" value=".a" unless="windows"/>
	<set name="SLIBEXT" value=".so" if="webos"/>
	
	<set name="DEBUGEXTRA" value="-debug" if="fulldebug" />
	
	<target id="NDLL" output="${LIBPREFIX}network${MSVC_LIB_VERSION}${DEBUGEXTRA}${LIBEXTRA}" tool="linker" toolid="${STD_MODULE_LINK}">
		
		<outdir name="../ndll/${BINDIR}"/>
		<ext value=".ndll" if="windows || mac || linux"/>
		<files id="common"/>
		
	</target>
	
	<target id="default">
		
		<target id="NDLL"/>
		
	</target>
	
</xml>

Any idea! Thanks for all your helps!

I think Network has to extend Extension

1 Like