vstirbu / ZeroConf

ZeroConf plugin for Cordova/Phonegap 3.0
MIT License
38 stars 21 forks source link

Examples #3

Closed drasko closed 10 years ago

drasko commented 10 years ago

Can you please provide a few examples and/or links to the code that uses this plugin? This will make easier to understand the documentation and function usage.

vstirbu commented 10 years ago

The following is a skeleton of an app (e.g. the www/js/index.js, if created using cordova cli) that searches for a service using mDNS:

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');

        ZeroConf.watch('_http._tcp.local.', function(result) {
           // do something with the result
        });
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

Basically, the plugin functionality is exposed via the ZeroConf object. Be sure to access it after the deviceready event is fired.

drasko commented 10 years ago

Should something (i.e. ZeroConf object) be included somewhere, since I am getting the error: Uncaught ReferenceError: ZeroConf is not defined

vstirbu commented 10 years ago

You must handle the plugin using the cordova cli workflow.

This way the ZeroConf will be included automatically.