-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
176 lines (174 loc) · 5.26 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
<!DOCTYPE html>
<html>
<head>
<title>PHP聊天室-基于swoole+redis+vue</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container" id="app">
<div class="left">
<ul>
<li class="user" v-for="user in user_list">
<img class="avatar" :src="avatar(user.avatar_id)">
<div class="user-des">
<p class="username">{{ user.name }}</p>
<p class="user-time">{{ user.time }}</p>
</div>
</li>
</ul>
</div>
<div class="right">
<div class="right-title">{{ title }}({{ user_count }})</div>
<div id="chat-container">
<ul>
<li class="chat" v-for="chat in chat_list">
<template v-if="chat.user_id !== user_id">
<img class="avatar-s" :src="avatar(chat.avatar_id)">
<div class="chat-content">
<div class="chat-time">{{ chat.time }}</div>
<p>{{ chat.txt }}</p>
</div>
</template>
<template v-else>
<img class="avatar-s" :src="avatar(chat.avatar_id)" style="float: right;">
<div class="chat-content" style="float: right;margin-right: 10px;">
<div class="chat-time" style="text-align: right;">{{ chat.time }}</div>
<p>{{ chat.txt }}</p>
</div>
</template>
</li>
</ul>
</div>
<div class="text">
<div class="chat-div">
<ul class="express">
<li><img src="images/express.png"></li>
<li><img src="images/message.png"></li>
</ul>
<textarea class="txt" v-model="text" @keyup.13="send()"></textarea>
<div class="button">
<button class="send-btn" @click="send()">发送(enter)</button>
</div>
</div>
<div class="name-div" v-bind:class="{ named: isNamed }">
<p>取个网名先,头像随机分配噢~</p>
<div class="make-div">
<input type="text" v-model="username">
<button class="name-btn" @click="makeName()">确定</button>
</div>
</div>
</div>
</div>
<div class="dialog" style="display: none">{{alert}}</div>
</div>
<script src="https://cdn.phpbest.cn/assets/js/jquery-3.3.1.min.js"></script>
<script src="https://cdn.phpbest.cn/assets/js/vue.min.js"></script>
<script type="text/javascript">
var websocket = new WebSocket("ws:127.0.0.1:9501");
websocket.onopen = function(){
console.log('connected!')
}
websocket.onclose = function(){
console.log('server closed!')
}
websocket.onmessage = function(e){
var data = JSON.parse(e.data)
app.receive(data)
}
websocket.onerror = function(){
alert('连接失败')
}
function playSound(){
var borswer = window.navigator.userAgent.toLowerCase();
if ( borswer.indexOf( "ie" ) >= 0 ){
//IE内核浏览器
var strEmbed = '<embed name="embedPlay" src="ding.wav" autostart="true" hidden="true" loop="false"></embed>';
if ( $("body").find("embed").length <= 0 )
$("body").append( strEmbed );
var embed = document.embedPlay;
//浏览器不支持 audion,则使用 embed 播放
embed.volume = 100;
//embed.play();
} else {
//非IE内核浏览器
var strAudio = "<audio id='audioPlay' src='ding.wav' hidden='true'>";
if ( $( "body" ).find( "audio" ).length <= 0 )
$( "body" ).append( strAudio );
var audio = document.getElementById( "audioPlay" );
//浏览器支持 audion
audio.play();
}
}
</script>
<script>
var app = new Vue({
el: '#app',
data: {
ws: websocket,
isNamed: false,
username: '',
user_id: 1,
avatar_id: 1,
title: 'PHP聊天室',
user_count: 0,
text: '',
user_list: [],
chat_list: [],
alert: '你有新短消息'
},
methods: {
send() {
this.request('chat', { avatar_id: this.avatar_id, txt: this.text, user_id: this.user_id })
this.text = ''
},
makeName() {
if (this.username == '' || this.username.length < 2 || this.username.length > 10) {
this.showMsg('请输入2-10位长度的名字')
return false
}
this.request('makeName', {name: this.username})
},
request(route, data) {
this.ws.send(JSON.stringify({"route":route, "params":data}))
},
avatar(fd) {
return 'images/avatars/'+fd+'.jpg'
},
showMsg(msg) {
this.alert = '新用户'+msg+'已上线'
$('.dialog').css('display', 'block')
setTimeout(function(){
$('.dialog').css('display', 'none')
}, 2500)
},
receive(data) {
if(data.type == 'users_list'){
// 更新用户列表
this.user_list = data.data
}
if(data.type == 'new_user' || data.type == 'myself'){
// 更新用户列表
this.user_list.push(data.data)
this.user_count = this.user_list.length
this.showMsg(data.data.name)
}
if(data.type == 'myself'){
// 设定自己的user_id
this.user_id = data.data.fd
this.avatar_id = data.data.avatar_id
this.isNamed = true
}
if(data.type == 'chat') {
this.chat_list.push(data.data)
if(this.user_id !== data.data.user_id) playSound()
setTimeout(function() {
document.getElementById('chat-container').scrollTop = document.getElementById('chat-container').scrollHeight
}, 50)
}
}
}
})
</script>
</body>
</html>