html5的websocket怎么建立udp连接

html5的websocket怎么建立udp连接,第1张

目前还是不支持udp。想用udp,可以使用flash.

UDP目前还未正式发布。

一个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 = 'rn',

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)

)

对webSocket到不是很熟悉,但是应该可以用其他方式解决,比如用vc开发一个ocx,放到客户端浏览器调用,缺点是需要提前注册还必须用ie或360浏览器,也可以用flash开发一个socket客户端 ,让浏览器加载进去,flash任何浏览器都支持。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7260421.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存