becvert / cordova-plugin-zeroconf

Cordova ZeroConf Plugin
MIT License
81 stars 57 forks source link

ZeroConf freezing UI #3

Closed kevin-dp closed 8 years ago

kevin-dp commented 8 years ago

Hi,

I set up a quick test to discover users present on the network. When running in the iOS simulator (or on a real iPhone, Android phone) Xcode gives me following warning

ZeroConf: watch _http._tcp.local. 2016-02-21 16:23:28.528 HelloCordova[41819:3077447] THREAD WARNING: ['ZeroConf'] took '19.701172' ms. Plugin should use a background thread.

I get the same warning for other methods, e.g. "register".

I was not worrying about this warning until I added a button so that clicking the button calls the "stop" method. Clicking the button does not call my function which calls the "stop" method. Hence, I assume Xcode was right and ZeroConf is freezing the UI ?

This is the very basic code I have,

index.html :

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
        <meta name="format-detection" content="telephone=no">
        <meta name="msapplication-tap-highlight" content="no">
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
        <link rel="stylesheet" type="text/css" href="css/index.css">
        <title>ZeroConf Plugin Test</title>
        <script type="text/javascript">
            function stp() {
                document.getElementById("devices").innerHTML = "STOPPED";
                cordova.plugins.zeroconf.stop(); // Unregisters all published services
            }
        </script>
    </head>
    <body>
        <div class="app">
            <h1>Devices on network</h1>
            <button onclick="stp()">STOP</button>
            <div id="deviceready" class="blink">
                <p id="devices"></p>
            </div>
        </div>
        <script type="text/javascript" src="cordova.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
    </body>
</html>

And index.js :

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    // Bind any events that are required on startup.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    onDeviceReady: function() {
        var el = document.getElementById("devices");
        var zeroconf = cordova.plugins.zeroconf;

        if(!(cordova.plugins && (typeof zeroconf !== 'undefined'))) {
            el.innerHTML = "ZEROCONF NOT READY";
        }

        zeroconf.watch('_http._tcp.local.', function(result) {
            var action = result.action;
            var service = result.service;

            if(action == 'added') {
                el.innerHTML = el.innerHTML + "Found device : " + result.service['name'] + "<br/>";
            }
            else {
                el.innerHTML = el.innerHTML + "Device " + result.service['name'] + " disconnected<br />";
            }
        });

        zeroconf.register('_http._tcp.local.', 'Test Name', 80, {
            'foo' : 'bar'
        });
    },
};

app.initialize();

Running this test you'll notice the "stp" function is not called upon clicking the button.

Thanks in advance! :)

becvert commented 8 years ago

Hi, I think script-src: 'unsafe-inline' is missing in your content-security-policy. Please re-test

becvert commented 8 years ago

Regarding the thread warning, I don't think the plugin is blocking the UI, cordova is only prompt to display that kind of warning. I'd be happy if someone could double check this though.

kevin-dp commented 8 years ago

Hi,

You were right, adding script-src 'self' 'unsafe-inline'; did the trick! Thanks for this one :)

becvert commented 8 years ago

You're welcome. I'm closing the issue.