【问题标题】:ionic framework TypeError: Cannot read property 'udp' of undefined离子框架类型错误:无法读取未定义的属性“udp”
【发布时间】:2016-12-19 23:18:32
【问题描述】:

cordova-plugin-chrome-apps-sockets-udp 已安装,但当我在 ionic 框架中使用它时仍然出现错误(在笔记本电脑上测试 - ionic serve)

我创建了 2 个文件 index.html 和 app.js,见下文:

index.html 文件:

<!DOCTYPE html>
<html >
  <head>
    <meta charset="UTF-8">
    <!-- below line to allow access to outside website -->
   <meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' *; style-src  'self' 'unsafe-inline' *; img-src 'self' data: *">
   <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
   <meta HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
   <meta HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
    <title>udpTest</title>
    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">   
    <link rel="stylesheet" href="css/style.css" media="screen, projection">
    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
    <!-- Before cordova.js -->
    <script src="lib/ngCordova/dist/ng-cordova.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter" ng-controller="AppCtrl">>

    <ion-pane>
  <ion-header-bar class="bar-stable">
    <h1 class="title">press key</h1>
  </ion-header-bar>
  <ion-content>
    <button class="button button-block button-balanced" ng-click="connect()">
      lock
    </button>
    <button class="button button-block button-assertive" ng-click="connect()">
      unlock
    </button>
  </ion-content>
</ion-pane>
  </body>
</html>

和 app.js 文件:

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
angular.module('starter', ['ionic', 'ngCordova'])
//.factory('udpfact')
.controller('AppCtrl', function($scope, $ionicPlatform) {
  $ionicPlatform.ready(function() {
       $scope.connect = function() {
      var socketId;

      // Handle the "onReceive" event.
      var onReceive = function(info) {
        if (info.socketId !== socketId)
        return;
        console.log(info.data);
        };  

        // Create the Socket
chrome.sockets.udp.create({}, function(socketInfo) {
  socketId = socketInfo.socketId;
  // Setup event handler and bind socket.
  chrome.sockets.udp.onReceive.addListener(onReceive);
  console.log("Just checking.");
  chrome.sockets.udp.bind(socketId,
    '', 5000, function(result) {
      if (result < 0) {
        console.log("Error binding socket.");
        return;
      }
      var arrayBuffer = new ArrayBuffer(20);
      var dv = new DataView(arrayBuffer,0);
      dv.setUint16(0,0);
      dv.setUint16(1,12);
      dv.setUint16(2,0);
      dv.setUint16(3,0);
      dv.setUint32(4,0);
      dv.setUint32(5,55555);
      dv.setUint32(6,0);
      console.log("here");
      chrome.sockets.udp.send(socketId, arrayBuffer,
        '255.255.255.255', 5000, function(sendInfo) {
          console.log("sent " + sendInfo.bytesSent);
      });
  });
});
  };
})
});

我经常看到这是一个问题,但在任何平台上我都没有找到可行的解决方案。任何帮助或提示将不胜感激。 谢谢!

【问题讨论】:

    标签: javascript cordova ionic-framework udp


    【解决方案1】:

    Cordova 插件无法在浏览器中运行。您需要在模拟器或设备中进行测试。

    将您的chrome.sockets.udp.create.... 包装在一个块中,以检查是否在cordova 上。比如:

    if (chrome.sockets.udp) {.......}

    【讨论】:

    • 更好的做法:if (chrome &amp;&amp; chrome.sockets &amp;&amp; chrome.sockets.udp){...}。否则会出现异常。
    • 使用调试功能(Xcode & safari + iPhone)后,提示找不到chrome。我需要在某处参考谷歌吗?在其他地方,例如3djs 你需要指向 index.html 中的 js 文件。再次感谢。
    • 我不知道你想要实现什么,但我猜 chrome.sockets 不会在 Safari 中存在
    • 最终目标是一个 Ionic 应用程序,它正在与我的 Raspberry Pi 通信。由于 Raspberry 的本地主机名并不总是可以访问,我需要 Pi 的 IP 地址。所以这就是为什么我想让应用程序向网络(本地)发送 UDP 消息以获取 Pi 的 IP 地址。有了这个,我可以在 Pi 上运行我的 API,就像我过去使用我的本地名称 Pi 一样。我还结合使用了 Ionic 框架和一些 D3js 进行报告,然后我需要将这一行 用于引用该库。我现在不想错过吗?
    • 最后我发现使用npm的问题是没有正确安装,你需要sudo cordova plugin add cordova-plugin-chrome-apps-sockets-udp
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2017-12-26
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2018-04-29
    相关资源
    最近更新 更多