HTML5的TCP和UDP Web Socket API草案定稿
目前存在一个在Web上实现UDP/TCP API的草案,但该草案尚未形成标准。此标准的一个显著亮点是采用了内置Promise设计模式,取代了传统JavaScript中的事件触发回调机制。不过,各大浏览器厂商是否会按照该草案进行实现仍不确定,毕竟制定标准的学术派和负责实现标准的行业派在理念和实践上难以完全达成一致。
接口标准
该接口标准对原始UDP套接字(Socket)、TCP客户端套接字和TCP服务器套接字的API进行了定义。
简介
这部分内容尚未形成规范。开发者可以使用该API通过TCP或UDP网络进行数据的发送和接收操作。
使用此API的部分用例
- 邮件服务器:能够与SMTP、POP3和IMAP服务器进行通信。
- IRC客户端:可以与IRC服务器进行通信(注:IRC是一种基于网络的即时聊天方式,主要用于群组聊天)。
- SSH应用程序:实现一个SSH应用程序。
- 消费硬件通信:与现有的消费硬件产品(如互联网电视)进行通信。
- 游戏服务器:构建游戏服务器。
- 端到端应用程序:开发P2P或对等网络应用。
- 本地网络多播服务发掘:例如UPnP/SSDP和mDNS。
UDP示例代码
以下是一个使用UDP进行UPnP - SSDP M - SEARCH发现的简单示例:
// 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.writable.write({
data: str2ab(search),
remoteAddress: address,
remotePort: port
}).then(
() => {
// Data sent successfully, 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 successfully");
},
(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)
);
TCP示例代码
相比UDP,TCP的示例代码相对简单,以下是一个简单的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.writable.write("Hello World").then(
() => {
// Data sent successfully, 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 successfully");
},
(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:https://github.com/sysapps/tcp - udp - sockets/issues ,不过目前关注者较少(仅有14个star)。