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的部分用例:
一个UDP的例子:
相比UDP,TCP的示例代码显得简单一些
有什么问题可在Github上面给他们开Issues:, 不过关注者廖廖(14个star目前): https://github.com/sysapps/tcp-udp-sockets/issues
本文转载自:http://ourjs.com/detail/54810f3e0dad0fbb6d000012
以下内容来自: 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 **9 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", **9);
- // 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
本文转载自:http://ourjs.com/detail/54810f3e0dad0fbb6d000012
【HTML5的TCP和UDP Web Socket API草案定稿】相关文章
1. HTML5的TCP和UDP Web Socket API草案定稿
2. HTML5-WebSocket实现对服务器CPU实时监控
7. CSS3教程:background-clip和background-origin
8. 三星确认将Bada整合进基于HTML5的Tizen操作系统
9. Adobe为Web设计人员推出开源文本编辑器Brackets
10. Web前端开发工具,Adobe Brackets 1.1 正式版发布
本文来源:https://www.51html5.com/a3793.html
﹝HTML5的TCP和UDP Web Socket API草案定稿﹞相关内容
- HTML5 WebApp part4:使用 Web Workers 来加速您的移动 Web 应用程序(下) ...
- HTML5 WebApp part4:使用 Web Workers 来加速您的移动 Web 应用程序(上) ...
- FiveMusicPlayer:Web版音乐播放器
- Mozilla宣布火狐Beta将支持Social API架构
- Air Hockey Plus
- 通过Web App和Native App之争 看如何做技术选型
- Web App和Native App不是生死之争,而是可以和平共处!
- Metro UI For Block
- 历时三年W3C宣布HTML5规范正式定稿
- HTML5定稿了,为什么原生App世界将被颠覆