Loading and using the phonegap plug-in and the phonegap plug-in

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.