-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathclient.html
56 lines (54 loc) · 1.38 KB
/
client.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<div style="width: 600px;height: 500px;margin: 30px auto;text-align: center">
<h1>websocket聊天系统</h1>
<textarea id="msg" rows="6" cols="50"></textarea><br>
<input type="button" value="发送" onclick="send()">
<div id="list" style="width: 500px;border: 1px solid gray; height: 300px;margin: 10px auto;overflow: auto;">
</div>
</div>
</body>
</html>
<script type="text/javascript">
if (window.WebSocket){
console.log("This browser supports WebSocket!");
} else {
console.log("This browser does not support WebSocket.");
}
var ws = new WebSocket("ws://127.0.0.1:8000");
ws.onopen = function(){
console.log('连接成功');
var data="系统消息:建立连接成功";
list(data);
ws.send('11');
}
ws.onmessage = function(e){
var obj=eval("("+e.data+")");
var data=obj.name+"消息:" + obj.mess;
list(data);
}
ws.onerror = function(){
var data="出错了,请退出重试";
list(data);
}
function send()
{
var msg=document.getElementById("msg").value;
ws.send(msg);
// var data="客户端消息:"+msg;
// list(data);
// document.getElementById("msg").value='';
}
function list(data)
{
var p=document.createElement("p");
p.innerHTML=data;
var box=document.getElementById("list");
box.appendChild(p);
}
</script>