日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > HTML >内容正文

HTML

html5支持udp协议吗,HTML5的TCP和UDP Web Socket API草案定稿

發(fā)布時(shí)間:2023/12/18 HTML 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 html5支持udp协议吗,HTML5的TCP和UDP Web Socket API草案定稿 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

一個(gè)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的示例代碼顯得簡(jiǎn)單一些

//

// 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);

);

總結(jié)

以上是生活随笔為你收集整理的html5支持udp协议吗,HTML5的TCP和UDP Web Socket API草案定稿的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。