gpt4 book ai didi

cordova - 用于未定义的ionic chrom.sockets.udp, 'sockets'

转载 作者:行者123 更新时间:2023-12-03 11:51:48 25 4
gpt4 key购买 nike

我安装了cordova插件:
https://github.com/MobileChromeApps/cordova-plugin-chrome-apps-sockets-udp

根据官方样本:

 chrome.sockets.udp.create({}, function (socketInfo) {
// The socket is created, now we can send some data
var socketId = socketInfo.socketId;
chrome.sockets.udp.send(socketId, arrayBuffer,
'255.255.255.255', 9999, function (sendInfo) {
console.log("sent " + sendInfo.bytesSent);
});
});

但无法读取未定义的属性“套接字”。

我在打印chrome对象时发现没有套接字

是什么原因插件安装不正确?

最佳答案

According to the official sample:...不好,那听起来不对。我想您在那里错过了几行代码。 (您可以提供该示例的链接吗?)对于套接字,您将不得不create()bind()send()查看从您提供的链接导航的tests.js here,。我认为这更有意义:

self = this;  // obviously this is based on how you have the socket "class" defined in JS
chrome.sockets.udp.create({}, function (socketInfo) {
// The socket is created...
var socketId = socketInfo.socketId;
// Setup a listener event handler
chrome.sockets.udp.onReceive.addListener(self.onReceive);
// Bind the socket
chrome.sockets.udp.bind(socketId, "0.0.0.0", 0, function(result) {
if (result < 0) {
console.log("Error binding socket.");
return;
}
// send out a message
chrome.sockets.udp.send(socketId, arrayBuffer,
'255.255.255.255', 9999, function (sendInfo) {
console.log("sent " + sendInfo.bytesSent);
});
// reminder: 255.255.255.255:9999 is the message destination address
});

请注意:如果基于我在其他应用程序中使用chrome.sockets.udp的响应(而不是基于 ionic 的东西...),我的响应请尝试以下代码,看看它是否适合您。注意: onReceive(data){..}是我编写的自定义函数,用于解析和使用JavaScript“class”中的返回数据。它不是标准的,预先存在的功能。

编辑:响应您的评论 I already can create, but in when chrome.sockets.udp.send arrayBuffer need what format?,这是我的buffer_converter.js文件
// ref:  https://www.safaribooksonline.com/library/view/programming-chrome-apps/9781491905272/ch04.html
// See also http://stackoverflow.com/questions/29574916/how-to-send-string-over-udp-using-javascript-on-chrome-app

// translate text string to Arrayed buffer
function text2ArrayBuffer(str /* String */ ) {
var encoder = new TextEncoder('utf-8');
return encoder.encode(str).buffer;
}

// translate Arrayed buffer to text string
function arrayBuffer2Text(buffer /* ArrayBuffer */ ) {
var dataView = new DataView(buffer);
var decoder = new TextDecoder('utf-8');
return decoder.decode(dataView);
}

关于cordova - 用于未定义的ionic chrom.sockets.udp, 'sockets',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38048560/

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