在浏览器端使用WebSocket之前,需要检测浏览器是否支持WebSocket,代码如下:
var socket=null;
window.WebSocket = window.WebSocket || window.MozWebSocket;
if (!window.WebSocket) { alert('Error: WebSocket is not supported .'); }
else{ socket = new WebSocket('ws://...');}
Websocket接口定义如下:
interface WebSocket : EventTarget {
readonly attribute DOMString url;
// ready state
const unsigned short CONNECTING = 0;
const unsigned short OPEN = 1;
const unsigned short CLOSING = 2;
const unsigned short CLOSED = 3;
readonly attribute unsigned short readyState;
readonly attribute unsigned long bufferedAmount;
// networking
attribute EventHandler onopen;
attribute EventHandler onerror;
attribute EventHandler onclose;
readonly attribute DOMString extensions;
readonly attribute DOMString protocol;
void close([Clamp] optional unsigned short code, optional DOMString reason);
// messaging
attribute EventHandler onmessage;
attribute DOMString binaryType;
void send(DOMString data);
void send(Blob data);
void send(ArrayBuffer data);
void send(ArrayBufferView data);
};
从上面定义中可以很清晰的看出:
通过send()发向服务器送数据; 通过close()关闭连接; 通过注册事件函数 onopen,onerror,onmessage,onclose 来处理服响应.在index.jsp中编写编写代码如下:
<!DOCTYPE HTML>
<html>
<head>
<title>WebSocket demo</title>
<style>
body {padding: 40px;}
#outputPanel {margin: 10px;padding:10px;background: #eee;border: 1px solid #000;min-height: 300px;}
</style>
</head>
<body>
<input type="text" id="messagebox" size="60" />
<input type="button" id="buttonSend" value="Send Message" />
<input type="button" id="buttonConnect" value="Connect to server" />
<input type="button" id="buttonClose" value="Disconnect" />
<br>
<% out.println("Session ID = " + session.getId()); %>
<div id="outputPanel"></div>
</body>
<script type="text/javascript">
var infoPanel = document.getElementById('outputPanel'); // 输出结果面板
var textBox = document.getElementById('messagebox'); // 消息输入框
var sendButton = document.getElementById('buttonSend'); // 发送消息按钮
var connButton = document.getElementById('buttonConnect');// 建立连接按钮
var discButton = document.getElementById('buttonClose');// 断开连接按钮
// 控制台输出对象
var console = {log : function(text) {infoPanel.innerHTML += text + "<br>";}};
// WebSocket演示对象
var demo = {
socket : null, // WebSocket连接对象
host : '', // WebSocket连接 url
connect : function() { // 连接服务器
window.WebSocket = window.WebSocket || window.MozWebSocket;
if (!window.WebSocket) { // 检测浏览器支持
console.log('Error: WebSocket is not supported .');
return;
}
this.socket = new WebSocket(this.host); // 创建连接并注册响应函数
this.socket.onopen = function(){console.log("websocket is opened .");};
this.socket.onmessage = function(message) {console.log(message.data);};
this.socket.onclose = function(){
console.log("websocket is closed .");
demo.socket = null; // 清理
};
},
send : function(message) { // 发送消息方法
if (this.socket) {
this.socket.send(message);
return true;
}
console.log('please connect to the server first !!!');
return false;
}
};
// 初始化WebSocket连接 url
demo.host=(window.location.protocol == 'http:') ? 'ws://' : 'wss://' ;
demo.host += window.location.host + '/Hello/websocket/say';
// 初始化按钮点击事件函数
sendButton.onclick = function() {
var message = textBox.value;
if (!message) return;
if (!demo.send(message)) return;
textBox.value = '';
};
connButton.onclick = function() {
if (!demo.socket) demo.connect();
else console.log('websocket already exists .');
};
discButton.onclick = function() {
if (demo.socket) demo.socket.close();
else console.log('websocket is not found .');
};
</script>
</html>









