用于离子 chrom.sockets.udp, 'sockets' of undefined
Used in the ionic chrom.sockets.udp, 'sockets' of undefined
我安装了 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);
});
});
但无法读取未定义的 属性 'sockets'。
我打印 chrome 对象没有找到套接字
是什么原因?插件安装不正确?
According to the official sample:...
呃不,这听起来不对。我想你在那里错过了几行代码。 (你能为那个样本提供一个 link 吗?)对于套接字,你将不得不 create()
、bind()
、send()
查看 tests.js here, navigated from您提供的link。我认为这更有意义:
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 的使用(而不是基于离子的东西...)试试这个代码,看看它是否适合你。注意:onReceive(data){..}
是我编写的自定义函数,用于解析和使用 JavaScript "class" 中的 return 数据。它不是标准的、预先存在的函数。
编辑:为回应您的评论 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
// 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 插件: 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);
});
});
但无法读取未定义的 属性 'sockets'。
我打印 chrome 对象没有找到套接字
是什么原因?插件安装不正确?
According to the official sample:...
呃不,这听起来不对。我想你在那里错过了几行代码。 (你能为那个样本提供一个 link 吗?)对于套接字,你将不得不 create()
、bind()
、send()
查看 tests.js here, navigated from您提供的link。我认为这更有意义:
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 的使用(而不是基于离子的东西...)试试这个代码,看看它是否适合你。注意:onReceive(data){..}
是我编写的自定义函数,用于解析和使用 JavaScript "class" 中的 return 数据。它不是标准的、预先存在的函数。
编辑:为回应您的评论 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
// 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);
}