<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />
	<title>聊天室示例</title>
</head>
<body>
<textarea id="sendText"></textarea>
<button onclick="sendMessage()">发送</button>
<script>
	var connect;
	window.onload=function unit(){
		if ("WebSocket" in window){
			// 打开一个 web socket
			connect = new WebSocket("ws://172.16.1.199:8080/webSocketServer");
			connect.onopen = function(){
				// Web Socket 已连接上
				alert("已连接");
			};
			connect.onmessage = function (evt){
				var r_msg = evt.data;
				document.body.innerHTML+="<br/>接收的信息为："+r_msg;
			};//接收时自己也会接收，可以自己设置过滤的if语句

			connect.onclose = function(){
				// 关闭 websocket
				alert("连接关闭");
			};
		}
		else{
			// 浏览器不支持 WebSocket
			alert("您的浏览器不支持 WebSocket!");
		}
	};
	function sendMessage(){
		connect.send(document.getElementById("sendText").value);
		document.getElementById("sendText").innerHTML="";
	}
</script>
</body>
</html>
