这是在Web上实现UDP/TCP API的草案,沿未形成标准。该标准的一大亮点就是使用内置Promise设计模式,替代了传统JavaScript中的事件触发回调。不过各大浏览器厂商会不会这样实现还要打一个问号,毕竟编写标准的学院派和实现标准的行业派很难达到完全统一。

       接口标准提供对原始UDP套接字(Socket),TCP客户端套接字和TCP服务器套接字API的定义。

简介

       这部分沿未形成规范。您可以使用该API来发送和接收数据,并使用TCP或UDP网络。


使用此API的部分用例:

  • 能够与SMTP, POP3 和 IMAP 服务器进行通信的邮件服务器。
  • 一个能与IRC服务器进行通信的IRC客户端 (注* IRC是一种通过网络的即时聊天方式。其主要用于群组聊天。)
  • 实现一个SSH应用程序
  • 与现有的消费硬件产品进行通信,如互联网电视
  • 游戏服务器
  • 端到端应用程序(注* P2P或对等网络应用)
  • 本地网络多播服务(multicast service)发掘,例如UPnP/ SSDP和mDNS

一个UDP的例子:

  1. // 
  2. // This example shows a simple implementation of UPnP-SSDP M-SEARCH
  3. // discovery using a multicast UDPSocket 
  4. //
  5.       
  6. var address = '239.255.255.250',
  7.     port = '1900',
  8.     serviceType = 'upnp:rootdevice',
  9.     rn = '\r\n',
  10.     search = '';

  11. //  Create a new UDP client socket
  12. var mySocket = new UDPSocket();

  13. // Build an SSDP M-SEARCH multicast message
  14. search += 'M-SEARCH * HTTP/1.1' + rn;
  15. search += 'ST: ' + serviceType + rn;
  16. search += 'MAN: "ssdp:discover"' + rn;
  17. search += 'HOST: ' + address + ':' + port + rn;
  18. search += 'MX: 10';


  19. // Receive and log SSDP M-SEARCH response messages
  20. function receiveMSearchResponses() {         

  21.   // While data in buffer, read and log UDP message
  22.   while (mySocket.readable.state === "readable") {            
  23.     var msg = mySocket.readable.read();
  24.     console.log ('Remote address: ' + msg.remoteAddress + 
  25.                  ' Remote port: ' + msg.remotePort + 
  26.                  'Message: ' + ab2str(msg.data)); 
  27.       // ArrayBuffer to string conversion could also be done by piping 
  28.       // through a transform stream. To be updated when the Streams API
  29.       // specification has been stabilized on this point. 
  30.   }  
  31.       
  32.   // Wait for SSDP M-SEARCH responses to arrive     
  33.   mySocket.readable.wait().then(
  34.     receiveMSearchResponses,          
  35.     e => console.error("Receiving error: ", e);
  36.   );     
  37. }

  38. // Join SSDP multicast group
  39. mySocket.joinMulticast(address);

  40. // Send SSDP M-SEARCH multicast message
  41. mySocket.writeable.write(
  42.   {data : str2ab(search),
  43.    remoteAddress : address,
  44.    remotePort : port
  45.   }).then(
  46.     () => {
  47.       // Data sent sucessfully, wait for response
  48.       console.log('M-SEARCH Sent');
  49.       receiveMSearchResponses();
  50.     },
  51.     e => console.error("Sending error: ", e);
  52. );

  53. // Log result of UDP socket setup. 
  54. mySocket.opened.then(
  55.   () => {
  56.     console.log("UDP socket created sucessfully");
  57.   },
  58.   e =>console.error("UDP socket setup failed due to error: ", e);
  59. );

  60. // Handle UDP socket closed, either as a result of the application 
  61. // calling mySocket.close() or an error causing the socket to be 
  62.    closed.
  63. mySocket.closed.then(
  64.   () => {
  65.      console.log("Socket has been cleanly closed");
  66.   },
  67.   e => console.error("Socket closed due to error: ", e);
  68. );
复制代码

相比UDP,TCP的示例代码显得简单一些

  1. // 
  2. // This example shows a simple TCP echo client. 
  3. // The client will send "Hello World" to the server on port **9 and log 
  4. // what has been received from the server.
  5. //   

  6. //  Create a new TCP client socket and connect to remote host     
  7. var mySocket = new TCPSocket("127.0.0.1", **9);

  8. // Send data to server
  9. mySocket.writeable.write("Hello World").then(
  10.     () => {
  11.         
  12.         // Data sent sucessfully, wait for response
  13.         console.log("Data has been sent to server");
  14.         mySocket.readable.wait().then(
  15.             () => {
  16.             
  17.                 // Data in buffer, read it
  18.                 console.log("Data received from server:" + mySocket.readable.read());

  19.                 // Close the TCP connection
  20.                 mySocket.close();
  21.             },
  22.              
  23.             e => console.error("Receiving error: ", e);
  24.         );
  25.     },
  26.     e => console.error("Sending error: ", e);
  27. );

  28. // Signal that we won't be writing any more and can close the write half of the connection.
  29. mySocket.halfClose();

  30. // Log result of TCP connection attempt. 
  31. mySocket.opened.then(
  32.   () => {
  33.     console.log("TCP connection established sucessfully");
  34.   },
  35.   e =>console.error("TCP connection setup failed due to error: ", e);
  36. );

  37. // Handle TCP connection closed, either as a result of the application 
  38. // calling mySocket.close() or the other side closed the TCP  
  39. // connection or an error causing the TCP connection to be closed.
  40. mySocket.closed.then(
  41.   () => {
  42.      console.log("TCP socket has been cleanly closed");
  43.   },
  44.   e => console.error("TCP socket closed due to error: ", e);
  45. );
复制代码

       有什么问题可在Github上面给他们开Issues:, 不过关注者廖廖(14个star目前):https://github.com/sysapps/tcp-udp-sockets/issues