1

Chrome Canary (현재 버전 25)를 사용하여 패키지 앱에서 UDP 소켓을 작동 시키려고합니다. 나는 UDP example herereference documentation here과 충돌한다는 사실에 상당히 혼란 스럽습니다. 그 함수의 문서화 된 형태와 일치하기 때문에Chrome 패키지 앱 UDP 소켓 작동하지 않음

Uncaught Error: Invocation of form socket.create(string, string, integer, object, function) doesn't match definition socket.create(string type, optional object options, function callback)

의외 : 오류이 선 결과를 사용하여 카나리아에

chrome.socket.create('udp', '127.0.0.1', 1337, { onEvent: handleDataEvent }, ... 

:

공식 예는이 줄을 사용합니다. OK (? 내가 예 오래된 것 같다), 그래서 나는 이것을 시도 ...

chrome.socket.create('udp', { onEvent: handleDataEvent }, ... 

카나리아는 불평 :

Uncaught Error: Invalid value for argument 2. Property 'onEvent': Unexpected property.

이 지금은이 매개 변수가 undocumented in the reference 특히 때문에, 혼란 스러워요. 그래서 난 그냥이 갈 :

chrome.socket.create('udp', {}, ... 

을 이제 확인을 생성하지만 connect에 다음 호출 ...

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

는 ...이 실패 :

Uncaught Error: Invocation of form socket.connect(integer, function) doesn't match definition socket.connect(integer socketId, string hostname, integer port, function callback)

. .. 놀랍지는 않습니다. 지금부터는 내 코드가 호스트 나 포트를 언급하지 않기 때문에, connect에 있어야합니다. 그래서 다음과 같은 형식으로 변경합니다 :

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

마지막으로 소켓에 연결하고 쓸 수 있습니다. 그러나 이것은 독서를 포함하지 않습니다.

  • 누군가가 저에게 &을 보낼 수있는 UDP 기반의 작동 예제를 표시 할 수 있습니까?
  • 예제의 onEvent 핸들러가 작동하지 않아서 데이터를 수신하는 방법은 무엇입니까? 요청하지 않고 도착하자마자 주문형 데이터를 수신하도록하려면 어떻게해야합니까?

답변

8

네트워크 통신 문서가 최신 버전이 아닙니다. 최신 API doc : https://developer.chrome.com/trunk/apps/socket.html을 참조하십시오. 그러나 의사는 모든 것을 명확하게 진술하지는 않습니다. 고객과의 상호 작용 chrome.socket.bindchrome.socket.recvFrom/chrome.socket.sendTo 전화, 서버 UDP 소켓의 경우 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.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); 
    }); 
}); 
: 여기

은 예입니다