我正在尝试使用Chrome Canary(目前的版本25)为一个打包的应用程序运行UDP套接字。我对UDP example here与reference documentation here冲突的事实感到很困惑。
官方示例使用此行:
chrome.socket.create('udp', '127.0.0.1', 1337, { onEvent: handleDataEvent }, ...
在Canary中使用此行会导致错误:
未捕获错误:调用表单socket.create(string,string, 整数,对象,函数)与定义不匹配 socket.create(字符串类型,可选对象选项,函数回调)
这并不奇怪,因为它符合函数的记录形式。 (我猜这个例子已经过时了?)好的,我试试这个......
chrome.socket.create('udp', { onEvent: handleDataEvent }, ...
金丝雀抱怨:
未捕获错误:参数2的值无效。属性'onEvent': 意外的财产。
现在我很困惑,特别是因为这个参数是undocumented in the reference。所以我就这样说:
chrome.socket.create('udp', {}, ...
现在它创建了OK,但是以下调用connect
...
chrome.socket.connect(socketId, function(result) ...
...失败了:
未捕获错误:调用表单socket.connect(整数,函数) 不匹配定义socket.connect(整数socketId,string 主机名,整数端口,函数回调)
...这并不奇怪,因为现在我的代码在任何地方都没有提到主机或端口,所以我想它需要在connect
。所以我将其更改为以下形式:
chrome.socket.connect(socketId, address, port, function (result) ...
最后我可以连接并写入套接字OK。但这不包括阅读。
答案 0 :(得分:8)
网络通讯文档不是最新的。请参阅最新的API文档:https://developer.chrome.com/trunk/apps/socket.html。但是医生并没有明确说明一切。 我查看了Chromium源代码,并在此处找到了一些有用的注释:https://code.google.com/searchframe#OAMlx_jo-ck/src/net/udp/udp_socket.h&q=file:(%5E%7C/)net/udp/udp_socket%5C.h$&exact_package=chromium
// Client form:
// In this case, we're connecting to a specific server, so the client will
// usually use:
// Connect(address) // Connect to a UDP server
// Read/Write // Reads/Writes all go to a single destination
//
// Server form:
// In this case, we want to read/write to many clients which are connecting
// to this server. First the server 'binds' to an addres, then we read from
// clients and write responses to them.
// Example:
// Bind(address/port) // Binds to port for reading from clients
// RecvFrom/SendTo // Each read can come from a different client
// // Writes need to be directed to a specific
// // address.
对于服务器UDP套接字,请调用chrome.socket.bind
和chrome.socket.recvFrom
/ chrome.socket.sendTo
与客户端进行交互。对于客户端UDP套接字,请调用chrome.socket.connect
和chrome.socket.read
/ chrome.socket.write
以与服务器进行交互。
以下是一个例子:
var serverSocket;
var clientSocket;
// From https://developer.chrome.com/trunk/apps/app_hardware.html
var str2ab=function(str) {
var buf=new ArrayBuffer(str.length);
var bufView=new Uint8Array(buf);
for (var i=0; i<str.length; i++) {
bufView[i]=str.charCodeAt(i);
}
return buf;
}
// From https://developer.chrome.com/trunk/apps/app_hardware.html
var ab2str=function(buf) {
return String.fromCharCode.apply(null, new Uint8Array(buf));
};
// Server
chrome.socket.create('udp', null, function(createInfo){
serverSocket = createInfo.socketId;
chrome.socket.bind(serverSocket, '127.0.0.1', 1345, function(result){
console.log('chrome.socket.bind: result = ' + result.toString());
});
function read()
{
chrome.socket.recvFrom(serverSocket, 1024, function(recvFromInfo){
console.log('Server: recvFromInfo: ', recvFromInfo, 'Message: ',
ab2str(recvFromInfo.data));
if(recvFromInfo.resultCode >= 0)
{
chrome.socket.sendTo(serverSocket,
str2ab('Received message from client ' + recvFromInfo.address +
':' + recvFromInfo.port.toString() + ': ' +
ab2str(recvFromInfo.data)),
recvFromInfo.address, recvFromInfo.port, function(){});
read();
}
else
console.error('Server read error!');
});
}
read();
});
// A client
chrome.socket.create('udp', null, function(createInfo){
clientSocket = createInfo.socketId;
chrome.socket.connect(clientSocket, '127.0.0.1', 1345, function(result){
console.log('chrome.socket.connect: result = ' + result.toString());
});
chrome.socket.write(clientSocket, str2ab('Hello server!'), function(writeInfo){
console.log('writeInfo: ' + writeInfo.bytesWritten +
'byte(s) written.');
});
chrome.socket.read(clientSocket, 1024, function(readInfo){
console.log('Client: received response: ' + ab2str(readInfo.data), readInfo);
});
});