PhoneGap plugin Loading and use

Source: Internet
Author: User

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

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.