gpt4 book ai didi

javascript - ionic 框架类型错误: Cannot read property 'udp' of undefined

转载 作者:行者123 更新时间:2023-11-28 05:40:58 25 4
gpt4 key购买 nike

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

我创建了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);
});
});
});
};
})
});

我已经看到这是一个很多问题,但在没有平台上我找到了有效的解决方案。任何帮助或提示将非常感激。谢谢!

最佳答案

Cordova 插件在浏览器中不起作用。您需要在模拟器或设备中进行测试。

将您的 chrome.sockets.udp.create.... 包装在一个 block 中,检查是否在 cordova 上。像这样的东西:

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

关于javascript - ionic 框架类型错误: Cannot read property 'udp' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38926491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com