-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
181 lines (178 loc) · 6.59 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
/* form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } */
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
/* #messages li:nth-child(odd) { background: #eee; } */
#messages { margin-bottom: 40px }
#messages .tip{ font-size: 10px;text-align: center;}
#messages .enter{ color: #1ec100; }
#messages .leave{ color: #f00; }
#loginFail{ color: red; }
#usersList::-webkit-scrollbar {display: none;}
#messages::-webkit-scrollbar {display: none;}
[contenteditable]:focus{outline: none;}
</style>
</head>
<body style="background-color: #ececec;">
<div class="container">
<div class="row">
<div class="col-sm-3 border p-0" style="height: 100%;">
<h4 class="text-center">用户列表(<span id="userCount">0</span>)</h4>
<ul class="list-group" id="usersList" style="overflow-y:scroll;">
<!-- 用户列表 -->
</ul>
</div>
<div class="col-sm-9" style="height: 100vh;">
<div class="row border border-dark " style="height: 80vh;">
<div class="col-sm-12 p-0">
<ul id="messages" style="overflow-y:scroll;">
<!-- 消息区域 -->
</ul>
</div>
</div>
<div class="row" style="height: 20vh;">
<div class="col-sm-12 bg-white">
<div class="col-sm-12" style="height: 100%;position: relative;">
<div contenteditable class="border-0 pt-2 pb-2" style="width:100%;height:100%;" id="msg"></div>
<button class="btn btn-primary btn-sm" style="position: absolute;right:0;bottom: 5px;" id="btnSend">发送</button>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
<!-- 模态框 -->
<div class="modal fade" tabindex="-1" role="dialog" id="tipLinkSuc" data-backdrop="static" keyboard="false">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">连接提示</h5>
</div>
<div class="modal-body">
<p>连接服务器成功,输入用户名就可以聊天了!</p>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="basic-addon1">用户名</span>
</div>
<input type="text" class="form-control" placeholder="Username" aria-label="Username"
aria-describedby="basic-addon1" id="username">
</div>
<p id="loginFail"></p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" id="getUserName">确定</button>
</div>
</div>
</div>
</div>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
<script>
$(function () {
window.onresize = function () {
$('#usersList').height($(document).height()-40)
$('#messages').height($(document).height()*0.8)
}
var TYPE_ENTER = 0;
var TYPE_LEAVE = 1;
var TYPE_MSG = 2;
var username;
var socket = io();
socket.on('connect',function(){
$("#tipLinkSuc").modal('show')
})
$("#getUserName").click(function () {
if ($("#username").val() != "") {
$("#tipLinkSuc").modal('hide')
socket.emit('login',{
username:$("#username").val(),
type:TYPE_ENTER,
time:new Date().toLocaleTimeString()
})
username = $("#username").val()
} else {
$("#username").css("border", "1px solid red")
}
})
socket.on('loginSuccess',data=>{
console.log(data)
$('#userCount').text(data.usersList.length)
$("#messages").append(`<li class="tip enter">${data.data.username}进入聊天室---${data.data.time}</li>`)
$('#usersList').html(`<li class="list-group-item active">${username}</li>`)
data.usersList.forEach(item => {
$('#usersList').append(`<li class="list-group-item">${item.username}</li>`)
});
})
socket.on('loginFail',data=>{
$("#tipLinkSuc").modal('show')
$("#loginFail").text(data.msg)
})
//发消息
// $("#msg").bind("keydown", function (event) {
// if (event.keyCode == "13") {
// if($("#msg").text()!=""){
// socket.emit('getMsg',{
// username:username,
// msg:$("#msg").html(),
// type:TYPE_MSG,
// })
// $("#msg").css("border", "1px solid #ced4da")
// $("#msg").html("")
// }else{
// $("#msg").css("border", "1px solid red")
// }
// }
// })
socket.on('sendMsg',data=>{
console.log(data)
if(data.username === username){
//自己的消息
$("#messages").append(`
<li>
<h4 class="text-right pr-2"><span class="bg-success" style="border-radius: 5px 5px 5px 5px;padding:2px;">${data.msg}</span>:${data.username}</h4>
</li>
`)
}else{
//别人的消息
$("#messages").append(`
<li>
<h4 class="pl-2">${data.username}:<span class="bg-white rounded" style="padding:2px;">${data.msg}</span></h4>
</li>
`)
}
$('#messages li:last')[0].scrollIntoView(false)
})
$('#btnSend').on('click',function () {
if($("#msg").text()!=""){
socket.emit('getMsg',{
username:username,
msg:$("#msg").html(),
type:TYPE_MSG,
})
$("#msg").html("")
}else{
alert('知道为啥弹框不?')
}
})
socket.on('userLeave',data=>{
$('#userCount').text(data.usersList.length)
$("#messages").append(`<li class="tip leave">${data.data.username}离开聊天室---${data.data.time}</li>`)
$('#usersList').html(`<li class="list-group-item active">${username}</li>`)
data.usersList.forEach(item => {
$('#usersList').append(`<li class="list-group-item">${item.username}</li>`)
});
})
});
</script>