HTML5的TCP和UDP Web Socket API草案定稿
這是在 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 的部分用例:
- 能夠與 SMTP, POP3 和 IMAP 服務器進行通信的郵件服務器。
- 一個能與 IRC 服務器進行通信的 IRC 客戶端 (注* IRC 是一種通過網絡的即時聊天方式。其主要用于群組聊天。)
- 實現一個 SSH 應用程序
- 與現有的消費硬件產品進行通信,如互聯網電視
- 游戲服務器
- 端到端應用程序(注* P2P 或對等網絡應用)
- 本地網絡多播服務(multicast service)發掘,例如 UPnP/ SSDP 和 mDNS
一個 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);
);
相比 UDP,TCP 的示例代碼顯得簡單一些
// // This example shows a simple TCP echo client.
// The client will send "Hello World" to the server on port 6789 and log
// what has been received from the server.
// // Create a new TCP client socket and connect to remote host var mySocket = new TCPSocket ("127.0.0.1", 6789);
// Send data to server mySocket.writeable.write ("Hello World") .then (
() => {
// Data sent sucessfully, wait for response console.log ("Data has been sent to server");
mySocket.readable.wait () .then (
() => {
// Data in buffer, read it console.log ("Data received from server:" + mySocket.readable.read ());
// Close the TCP connection mySocket.close ();
},
e => console.error ("Receiving error: ", e);
);
},
e => console.error ("Sending error: ", e);
);
// Signal that we won't be writing any more and can close the write half of the connection. mySocket.halfClose ();
// Log result of TCP connection attempt. mySocket.opened.then (
() => {
console.log ("TCP connection established sucessfully");
},
e =>console.error ("TCP connection setup failed due to error: ", e);
);
// Handle TCP connection closed, either as a result of the application
// calling mySocket.close () or the other side closed the TCP
// connection or an error causing the TCP connection to be closed. mySocket.closed.then (
() => {
console.log ("TCP socket has been cleanly closed");
},
e => console.error ("TCP socket closed due to error: ", e);
);有什么問題可在 Github 上面給他們開 Issues:, 不過關注者廖廖(14 個 star 目前): https://github.com/sysapps/tcp-udp-sockets/issues
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!