A friend asked can be in CANTK and Appbuilder development of the app to send UDP data, HTML5 can only use Https/http/websocket several communication methods, to use the UDP need to be packaged into an APK and other specific platform installation package. For this I wrote a UDP example, but the runtime encountered the problem, so it took some time to study the PHONEGAP load UDP plug-in process.
1. Add the required plugins
The list of plugins referenced by the app is stored in cordova_plugins.js and can be added with PhoneGap plugin add, such as:
add org.chromium.sockets.tcp
2. Load Cordova_plugins.js
Exports.load = function(callback) { varPathprefix = Findcordovapath ();if(Pathprefix = = =NULL) {Console.log (' Could not find cordova.js script tag. Plugin loading may fail. '); Pathprefix ="'; } injectifnecessary (' Cordova/plugin_list ', Pathprefix +' Cordova_plugins.js ', function() { varModulelist =require("Cordova/plugin_list"); Handlepluginsobject (Pathprefix, Modulelist, callback); }, callback);}; function handlepluginsobject(Path, modulelist, finishpluginloading) { //Now inject the scripts. varScriptcounter = Modulelist.length;if(!scriptcounter) {finishpluginloading ();return; } function scriptloadedcallback() { if(!--Scriptcounter) {Onscriptloadingcomplete (modulelist, finishpluginloading); } } for(vari =0; i < modulelist.length; i++) {injectifnecessary (modulelist[i].id, Path + modulelist[i].file, scriptloadedcallback); }}
3. Plug-in JS file called Cordova.define to register themselves in the PhoneGap module list
cordova.define("in.girish.datagram.datagram"function(requireexportsmodule) {
4. The caller does not need to refer directly to the plugin's JS, but instead calls Cordova.require to find it from the plug-in list and then uses the
var dgram = cordova.require(‘in.girish.datagram.datagram‘); var client = dgram.createSocket("udp4"); client.send("hello cantk""192.168.1.168"41234function(err) { JSON"success"); client.close(); });
Plug-in users only need to focus on steps 1th and 4th. The 2nd step is implemented by PhoneGap, and the 3rd step is implemented by the plug-in provider.
But the 3rd step is to note that the old version requires the plugin to call Cordova.define, the new edition will automatically add this definition. Therefore, the new version of the PHONEGAP using the old version of the plugin will be problematic, resulting in a duplicate definition and can not be used, you need to manually delete this definition.
PhoneGap plugin Loading and use