Loading and using the phonegap plug-in and the phonegap plug-in
Some friends asked if they could send UDP data in the apps developed by CanTK and AppBuilder. HTML5 can only use HTTPS/HTTP/WebSocket communication methods, to use UDP, You need to package it into an installation package for a specific platform such as APK through phonegap. For this reason, I wrote a UDP example, but it took some time to study the process of loading the udp plug-in from phonegap.
1. Add the required plug-in
In cordova_plugins.js, the list of plug-ins referenced by the APP is stored. You can use phonegap plugin add to add the plug-ins, for example:
phonegap plugin add org.chromium.sockets.tcp
2. Load cordova_plugins.js
exports.load = function(callback) { var pathPrefix = 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() { var moduleList = require("cordova/plugin_list"); handlePluginsObject(pathPrefix, moduleList, callback); }, callback);};function handlePluginsObject(path, moduleList, finishPluginLoading) { // Now inject the scripts. var scriptCounter = moduleList.length; if (!scriptCounter) { finishPluginLoading(); return; } function scriptLoadedCallback() { if (!--scriptCounter) { onScriptLoadingComplete(moduleList, finishPluginLoading); } } for (var i = 0; i < moduleList.length; i++) { injectIfNecessary(moduleList[i].id, path + moduleList[i].file, scriptLoadedCallback); }}
3. Call cordova. define in the JS file of the plug-in to register yourself to the module list of phonegap.
cordova.define("in.girish.datagram.datagram", function(require, exports, module) {
4. The caller does not need to directly reference the plug-in JS, but calls cordova. require To search for the plug-in list and then uses
var dgram = cordova.require('in.girish.datagram.datagram'); var client = dgram.createSocket("udp4"); client.send("hello cantk", "192.168.1.168", 41234, function(err) { console.log(err ? JSON.stringify(err) : "success"); client.close(); });
Plug-in users only need to pay attention to steps 1st and 4th. Step 2 is implemented by phonegap and Step 2 is implemented by the plug-in provider.
However, you must note that the old version requires the plug-in to call cordova. define by itself. This definition is automatically added in the new version. Therefore, the new version of phonegap may have problems when using the plug-in of the old version, which leads to repeated definitions and is unavailable. You need to manually delete this definition.