我正在使用bonjour服务
在我的服务器上运行

var test= bonjour.publish({ name: "abcd", type: 'http', port: settings.server.port });
test.start();


在客户端上运行:

function onDeviceReady() {

  console.log('in deviceready')
  var zeroconf = cordova.plugins.zeroconf;
  console.log(zeroconf)

  zeroconf.watch('abcd._http._tcp.local.', function (result) {
    console.log('in zeroconf')
    var action = result.action;
    var service = result.service;
    if (action == 'added') {
      console.log('service added', service);
    } else {
      console.log('service removed', service);
    }
  });
  console.log("test")
}


但是我的zeroconf.watch方法没有任何响应/成功。我添加了所需的插件,但无济于事...

最佳答案

我为我工作:

<script type="text/javascript">
    function onDeviceReady() {
        ZeroConf.watch('_http._tcp.local.', function(resp){
            document.getElementById("zeroconf2").innerHTML = resp;
        });
    }
    function onLoad() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
    onLoad();
</script>


并将此行放入config.xml

<gap:plugin name="cordova-zeroconf-plugin" source="npm" />

09-25 22:15