-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_socket.html
49 lines (33 loc) · 1 KB
/
index_socket.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>socket</title>
</head>
<body>
<button id="sendBtn">发送消息</button>
<button id="leaveBtn">离开</button>
</body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
<script type="text/javascript">
var socket=io.connect('http://127.0.0.1:3001'),//与服务器进行连接
send=document.getElementById('sendBtn'),
leave=document.getElementById('leaveBtn');
send.onclick=function(){
socket.send('hello world!hello worldhello worldhello world');
socket.emit('hi', 'hello!');
}
leave.onclick=function(){
window.location.href="about:blank";
window.close()
socket.emit('leave', 'leave');
}
//接收来自服务端的信息事件c_hi
socket.on('c_hi',function(msg){
console.log(msg);
})
// socket.on('c_leave',function(msg){
// alert(msg)
// })
</script>
</html>