【问题标题】:Chrome packaged app UDP sockets not workingChrome 打包的应用程序 UDP 套接字不起作用
【发布时间】:2012-12-03 20:27:22
【问题描述】:

我正在尝试让 UDP 套接字适用于使用 Chrome Canary(当前版本 25)的打包应用程序。我对UDP example herereference 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', {}, ...

现在它创建好了,但是下面调用connect...

chrome.socket.connect(socketId, function(result) ...

...失败了:

未捕获的错误:调用表单 socket.connect(integer, function) 与定义不匹配 socket.connect(integer socketId, string 主机名、整数端口、函数回调)

...这并不奇怪,因为现在我的代码在任何地方都没有提到主机或端口,所以我想它需要在connect 中。所以我把它改成形式:

chrome.socket.connect(socketId, address, port, function (result) ...

我终于可以连接并写入套接字了。但这不包括阅读。

  • 谁能给我看一个可以发送和接收的基于 UDP 的工作示例,以便我可以使用它?
  • 由于示例的 onEvent 处理程序不起作用,我如何接收数据?如何确保我按需接收任何数据,而不会阻塞?

【问题讨论】:

    标签: javascript sockets google-chrome google-chrome-extension google-chrome-app


    【解决方案1】:

    网络通信文档不是最新的。请参阅最新的 API 文档:https://developer.chrome.com/trunk/apps/socket.html。但是文档并没有清楚地说明一切。 我查看了 Chromium 源代码,发现了一些有用的 cmets: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.bindchrome.socket.recvFrom/chrome.socket.sendTo 与客户端交互。对于客户端UDP套接字,调用chrome.socket.connectchrome.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);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多