复制代码代码如下: interface WebSocket{ readonly attribute DOMString URL; //就绪状态 const unsigned short CONNECTING = 0; const unsigned short OPEN = 1; const unsigned short CLOSED = 2; readonly attribute unsigned short readyState; readonly attribute unsigned short bufferedAmount; //网络 attribute Function onopen; attribute Function onmessage; attribute Function onclose; boolean send(in DOMSString data); void close(); }; WebSocket implements EventTarget;
注意:ws://和wss://前缀分别表示WebSocket连接和安全的WebSocket连接。二、HTML5 WebSockets API本节讨论HTML5 WebSockets的使用方法1、检测浏览器是否支持通过window.WebSocket来判断浏览器是否支持。2、API的基本用法a. WebSocket对象的创建以及与WebSocket服务器的连接 复制代码代码如下: url = "ws://localhost:8080/echo"; ws = new WebSocket(url); b. 添加事件监听器WebSocket遵循异步编程模型,打开socket后,只需等待事件发生,而不需主动向服务器轮询,因此需要添加回调函数来监听事件。WebSocket对象有三个事件:open、close和message。当连接建立时触发open事件,当收到消息时触发message事件,当WebSocket连接关闭时触发close事件。 复制代码代码如下: ws.onopen = function(){ log("open"); } ws.onmessage = function(){ log(e.data); } ws.onclose = function(){ log("closed"); } c. 发送消息当socket处于打开状态(即调用onopen监听程序之后,调用onclose监听程序之前),可以使用send方法发送消息。ws.send( Hello World 三、HTML5 WebSockets 应用示例本节将结合前面讲述的Geolocation接口来创建一个直接在Web页面中计算距离的应用。1、编写HTML文件 复制代码代码如下: !DOCTYPE html /p p html /p p head /p p meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /p p title HTML5 WebSocket / Geolocation 追踪器 /title /p p link rel="stylesheet" href="styles.css" /p p /head /p p body onload="loadDemo()" /p p h1 HTML5 WebSocket / Geolocation 追踪器 /h1 /p p div strong Geolocation /strong : p id="geoStatus" 你的浏览器不支持HTML5 Geolocation /p /div /p p div strong WebSocket /strong : p id="socketStatus" 你的浏览器不支持HTML5 Web Sockets /p /div /p p /body /p p /html 2、添加WebSocket代码 复制代码代码如下: function loadDemo(){ //确保浏览器支持WebSocket if(window.WebSocket){ url = "ws://localhost:8080";//broadcast WebSocket服务器位置 ws = new WebSocket(url); ws.onopen = function(){ updateSocketStatus("连接已建立"); } ws.onmessage = function(e){ updateSocketeStatus("更新位置数据:" + dataReturned(e.data)); } } } 3、添加Geolocation代码 复制代码代码如下: var geo; if(navigator.geolocation){ geo = navigator.geolocation; updateGeolocationStatus("浏览器支持HTML5 Geolocation"); } /p p geo.watchPosition(updateLocation,handleLocationError,{maximumAge:20000});//每20s更新一次 /p p function updateLocation(position){ var latitude = position.coords.latitude; var longitude = position.coords.longitude; var timestamp = position.timestamp; updateGeolocationStatus("位置更新时间:" + timestamp); var toSend = JSON.stringify([myId,latitude,longitude]); sendMyLocation(toSend); } 4、合并所有内容 复制代码代码如下: !DOCTYPE html /p p html /p p head /p p meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /p p title HTML5 WebSocket / Geolocation 追踪器 /title /p p link rel="stylesheet" href="styles.css" /p p /head /p p body onload="loadDemo()" /p p h1 HTML5 WebSocket / Geolocation 追踪器 /h1 /p p div strong Geolocation /strong : p id="geoStatus" 你的浏览器不支持HTML5 Geolocation /p /div /p p div strong WebSocket /strong : p id="socketStatus" 你的浏览器不支持HTML5 Web Sockets /p /div /p p /body /p p script /p p //WebSocket的引用 /p p var ws; /p p //为该会话生成的唯一随机的ID /p p var myId = Math.floor(100000*Math.random()); /p p //当前显示的行数 /p p var rowCount; /p p function updateSocketStatus(message){ /p p document.getElementById("socketStatus").innerHTML = message; /p p } /p p function updateGeolocationStatus(message){ /p p document.getElementById("geoStatus").innerHTML = message; /p p } /p p function loadDemo(){ /p p //确保浏览器支持WebSocket /p p if(window.WebSocket){ /p p url = "ws://localhost:8080";//broadcast WebSocket服务器位置 /p p ws = new WebSocket(url); /p p ws.onopen = function(){ /p p updateSocketStatus("连接已建立"); /p p } /p p ws.onmessage = function(e){ /p p updateSocketStatus("更新位置数据:" + dataReturned(e.data)); /p p } /p p } /p p var geo; /p p if(navigator.geolocation){ /p p geo = navigator.geolocation; /p p updateGeolocationStatus("浏览器支持HTML5 Geolocation"); /p p } /p p geo.watchPosition(updateLocation,handleLocationError,{maximumAge:20000});//每20s更新一次 /p p function updateLocation(position){ /p p var latitude = position.coords.latitude; /p p var longitude = position.coords.longitude; /p p var timestamp = position.timestamp; /p p updateGeolocationStatus("位置更新时间:" + timestamp); /p p var toSend = JSON.stringify([myId,latitude,longitude]); /p p sendMyLocation(toSend); /p p } /p p function sendMyLocation(newLocation){ /p p if(ws){ /p p ws.send(newLocation) /p p } /p p } /p p function dataReturned(locationData){ /p p var allData = JSON.parse(locationData); /p p var incomingId = allData[1]; /p p var incomingLat = allData[2]; /p p var incomingLong = allData[3]; /p p var incomingRow = document.getElementById(incomingId); /p p if(!incomingRow){ /p p incomingRow = document.getElementById("div"); /p p incomingRow.setAttribute("id",incomingId); /p p incomingRow.userText = (incomingId == myId)?"Me":'User' + rowCount; /p p rowCount++; /p p document.body.appendChild(incomingRow); /p p } /p p incomingRow.innerHTML = incomingRow.userText + " // Lat: " + /p p incomingLat + " // Lon: " + /p p incomingLong; /p p return incomingRow.userText; /p p } /p p function handleLocationError(error){ /p p switch(error.code){ /p p case 0: /p p updateGeolocationStatus("检索位置信息出错: " + error.message); /p p break; /p p case 1: /p p updateGeolocationStatus("用户阻止获取位置信息。"); /p p break; /p p case 2: /p p updateGeolocationStatus("浏览器不能检测你的位置信息: " + error.message); /p p break; /p p case 3: /p p updateGeolocationStatus("浏览器检索位置信息超时。"); /p p break; /p p } } /p p /script /p p /html html教程