在Google Chrome中使用Web Sockets | 3秒 (3seconds.cn)
- 从Google Chrome的developer channel释放的4.0.249.0版本开始,Web Sockets已经可用并默认开启了。Web Sockets是“网络的TCP”,它是Web Applications 1.0中下一代网络应用程序双向通信技术标准的一部分
- 与XMLHttpRequest截然不同的的是,Web Sockets在你的浏览器里面提供一个真正的双向沟通渠道,一旦得到Web Socket连接,你就能通过直接调用send()方法从浏览器发送数据到服务器,并且使用onmessage的这个事件处理句柄接收数据
- if ("WebSocket" in window) {
var ws = new WebSocket("ws://example.com/service");
ws.onopen = function() {
// Web Socket is connected. You can send data by send() method.
ws.send("message to send"); ....
};
ws.onmessage = function (evt) { var received_msg = evt.data; ... };
ws.onclose = function() { // websocket is closed. };
} else {
// the browser doesn't support WebSocket.
}
Posted from Diigo. The rest of my favorite links are here.
没有评论:
发表评论