forked from EastHuang/php-websocket-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.html
82 lines (82 loc) · 2.17 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<!DOCTYPE html>
<html>
<meta http-equiv="Content-type" content="text/html;charset=utf-8">
<head>
<title>客户端</title>
<link rel="stylesheet" type="text/css" href="public/css/style.css">
<script src="public/js/jquery-2.2.1.min.js"></script>
<script src="public/js/client.js"></script>
<script>
var ws = new WebSocket('ws://127.0.0.1:8080/');
ws.onopen = function(){
console.log("握手成功,打开socket连接了。。。");
};
ws.onmessage = function(e){
var template = $("#other_template").html();
var temp = $(template);
temp.find(".info").children("span").text(e.data);
$("#main").append(temp);
};
ws.onclose = function(){
console.log("断开socket连接了。。。");
};
ws.onerror = function(e){
console.log("ERROR:" + e.data);
};
sendMessage = function(){
//向服务端发送消息
var data = $("#msg").val();
ws.send(data);
$("#msg").val("");
//根据模板生成自己消息视图
var template = $("#self_template").html();
var temp =$(template);
temp.find(".info").children("span").text(data);
$("#main").append(temp);
}
</script>
<script id="other_template" type="text/template">
<div class="show_box">
<div class="user box-left">
<img src="public/image/huaji.jpg">
</div>
<div class="info info_other box-left">
<span></span>
<div class="angle-left info_other"></div>
</div>
</div>
</script>
<script id="self_template" type="text/template">
<div class="show_box">
<div class="user box-right">
<img src="public/image/huaji.jpg">
</div>
<div class="info info_self box-right">
<span></span>
<div class="angle-right info_self"></div>
</div>
</div>
</script>
</head>
<body>
<div id="client">
<div id="head"></div>
<div id="main"></div>
<div id="msg_box">
<input id="msg" type="text" placeholder="随便说两句吧...">
<button id="send">发送</button>
</div>
</div>
<script>
$("#client").ui_init();
$("#send").click(function(){
sendMessage();
});
$("#msg").on("keypress",function(e){
if(e.keyCode == 13){
sendMessage();
}
});
</script>
</body>
</html>