一.概要网页可以WebSockets协议与远程主机进行双向交互,这彻底改变了目前客户端只能被动接受服务器影响的模式。使得浏览器成为了真正意义上的支持各种应用协议的网络客户端;对于开发者而言可以用JS和DOM开发网络应用程序。目前该规范已被W3C接受,正在开发中,请见:http://dev.w3.org/html5/websockets/
二.接口
WebSocket {
readonly attribute DOMString URL; // ready state
const unsigned short CONNECTING = 0;
const unsigned short OPEN = 1;
const unsigned short CLOSED = 2;
readonly attribute unsigned short readyState;
readonly attribute unsigned long bufferedAmount; // networking
attribute Function onopen;
attribute Function onmessage;
attribute Function onclose;
boolean send(in DOMString data);
void close();
};
三.使用
var conn = new WebSocket("foo://www.foo.com/foo");
conn.onopen = function(evt) {
alert("Conn opened");
}
conn.onread = function(evt) {
alert("Read: " + evt.data);
}
conn.onclose = function(evt) {
alert("Conn closed");
}
conn.send("Hello World");