标签:style blog http io ar color os 使用 sp
这是在 Web 上实现 UDP/TCP API 的草案,沿未形成标准。该标准的一大亮点就是使用内置 Promise 设计模式,替代了传统 JavaScript 中的事件触发回调。不过各大浏览器厂商会不会这样实现还要打一个问号,毕竟编写标准的学院派和实现标准的行业派很难达到完全统一。
以下内容来自: http://www.w3.org/TR/2014/WD-tcp-udp-sockets-20141202/
接口标准提供对原始 UDP 套接字(Socket),TCP 客户端套接字和 TCP 服务器套接字 API 的定义。
简介
这部分沿未形成规范。您可以使用该 API 来发送和接收数据,并使用 TCP 或 UDP 网络。
使用此 API 的部分用例:
一个 UDP 的例子:
// // This example shows a simple implementation of UPnP-SSDP M-SEARCH // discovery using a multicast UDPSocket // var address = ‘‘‘‘239.255.255.250‘‘‘‘, port = ‘‘‘‘1900‘‘‘‘, serviceType = ‘‘‘‘upnp:rootdevice‘‘‘‘, rn = ‘‘‘‘\r\n‘‘‘‘, search = ‘‘‘‘‘‘‘‘; // Create a new UDP client socket var mySocket = new UDPSocket (); // Build an SSDP M-SEARCH multicast message search += ‘‘‘‘M-SEARCH * HTTP/1.1‘‘‘‘ + rn; search += ‘‘‘‘ST: ‘‘‘‘ + serviceType + rn; search += ‘‘‘‘MAN: "ssdp:discover"‘‘‘‘ + rn; search += ‘‘‘‘HOST: ‘‘‘‘ + address + ‘‘‘‘:‘‘‘‘ + port + rn; search += ‘‘‘‘MX: 10‘‘‘‘; // Receive and log SSDP M-SEARCH response messages function receiveMSearchResponses () { // While data in buffer, read and log UDP message while (mySocket.readable.state === "readable") { var msg = mySocket.readable.read (); console.log (‘‘‘‘Remote address: ‘‘‘‘ + msg.remoteAddress + ‘‘‘‘ Remote port: ‘‘‘‘ + msg.remotePort + ‘‘‘‘Message: ‘‘‘‘ + ab2str (msg.data)); // ArrayBuffer to string conversion could also be done by piping // through a transform stream. To be updated when the Streams API // specification has been stabilized on this point. } // Wait for SSDP M-SEARCH responses to arrive mySocket.readable.wait () .then ( receiveMSearchResponses, e => console.error ("Receiving error: ", e); ); } // Join SSDP multicast group mySocket.joinMulticast (address); // Send SSDP M-SEARCH multicast message mySocket.writeable.write ( {data : str2ab (search), remoteAddress : address, remotePort : port }) .then ( () => { // Data sent sucessfully, wait for response console.log (‘‘‘‘M-SEARCH Sent‘‘‘‘); receiveMSearchResponses (); }, e => console.error ("Sending error: ", e); ); // Log result of UDP socket setup. mySocket.opened.then ( () => { console.log ("UDP socket created sucessfully"); }, e =>console.error ("UDP socket setup failed due to error: ", e); ); // Handle UDP socket closed, either as a result of the application // calling mySocket.close () or an error causing the socket to be closed. mySocket.closed.then ( () => { console.log ("Socket has been cleanly closed"); }, e => console.error ("Socket closed due to error: ", e); );
html5的tcp和udp web socket api草案定稿
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/jgr8224/p/4147113.html