HTML5链接tcpUDP,HTML5的TCP和UDP Web Socket 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);

);

相比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);

);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值