cordova-plugin-chrome-apps-sockets-udp已经安装但在离子框架中使用时仍会出现错误(在笔记本电脑上测试 - 离子服务)
我创建了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);
});
});
});
};
})
});
我已经看到这是一个很大的问题,但在没有任何平台我找到了一个有效的解决方案。任何帮助或提示将非常感激。 谢谢!
答案 0 :(得分:0)
Cordova插件无法在浏览器中运行。您需要在模拟器或设备中进行测试。
将chrome.sockets.udp.create....
包裹在检查是否在cordova上的块中。类似的东西:
if (chrome.sockets.udp) {.......}