-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathchat.html
More file actions
187 lines (175 loc) · 5.31 KB
/
chat.html
File metadata and controls
187 lines (175 loc) · 5.31 KB
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
182
183
184
185
186
187
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<link href="css/mui.min.css" rel="stylesheet" />
<script src = "https://cdn.wilddog.com/js/client/current/wilddog.js"></script>
<style>
html,
body {
height: 100%;
margin: 0px;
padding: 0px;
overflow: hidden;
-webkit-touch-callout: none;
-webkit-user-select: none;
}
footer {
position: fixed;
width: 100%;
height: 50px;
min-height: 50px;
border-top: solid 1px #bbb;
left: 0px;
bottom: 0px;
overflow: hidden;
padding: 0px 65px 0 10px;
background-color: #fafafa;
}
.footer-right {
position: absolute;
width: 60px;
height: 50px;
right: 0px;
bottom: 0px;
text-align: center;
vertical-align: middle;
line-height: 100%;
padding: 8px 15px 0 0;
display: inline-block;
}
.footer-center {
height: 100%;
padding: 5px 0px;
}
.footer-center [class*=input] {
width: 100%;
height: 100%;
border-radius: 5px;
}
.footer-center .input-text {
background: #fff;
border: solid 1px #ddd;
padding: 10px !important;
font-size: 16px !important;
line-height: 15px !important;
font-family: verdana !important;
overflow: hidden;
}
.msg-box{
height: 15%;
overflow: auto;
-webkit-overflow-scrolling: touch;
margin: 10px 10px;
border: 1px solid #8d8d8d;
padding: 6px 6px 6px 15px;
border-radius: 2px;
background: #FFE4B5;
}
.msg-list{
height: 85%;
overflow: auto;
-webkit-overflow-scrolling: touch;
margin-left: 12px;
width: 95%;
margin: 10px 0 0px 12px;
border-radius: 6px;
background: #D8D8D8;
}
.name{
font-size: 16px;
color: blue;
width: 100%;
padding-left: 4px;
background: #D8D8D8;
}
.time{
font-size: 12px;
margin-left: 5px;
}
.content{
font-size: 16px;
color: #000;
margin-left: 10px;
}
</style>
</head>
<body>
<div class="mui-content">
<div class="msg-box">
<p><h5 id="nickname_label">欢迎信息</h5></p>
<input type="hidden" name="nickname" id="nickname" value="" />
<p><h5 id="roomid_label">房间号</h5></p>
<input type="hidden" name="roomid" id="roomid" value="" />
</div>
<div class="msg-list" id='msg-list'>
</div>
</div>
<footer>
<div class="footer-center">
<textarea id='msg-text' type="text" class='input-text' placeholder="说些什么吧"></textarea>
</div>
<label for="" class="footer-right">
<button type="button" class="mui-btn mui-btn-blue" id="send">发送</button>
</label>
</footer>
<script src="js/mui.min.js"></script>
<script>
mui.init();
mui.plusReady(function(){
//从本地存储中获取房间号和昵称,并且在页面上显示
var nickname_label = document.getElementById("nickname_label");
var nickname = document.getElementById("nickname");
var roomid_label =document.getElementById("roomid_label");
var roomid = document.getElementById("roomid");
nickname.value = localStorage.nickname;
roomid.value = localStorage.roomid;
nickname_label.innerHTML = "你好,"+localStorage.nickname+"!欢迎访问!";
roomid_label.innerHTML = "房间号码为:"+localStorage.roomid;
//获取控件ID
var msg_text = document.getElementById("msg-text");
var msg_list = document.getElementById("msg-list");
var send = document.getElementById("send");
//实例化野狗SDK,并且创建InfoMess根节点
var ref = new Wilddog("https://secretmui.wilddogio.com/InfoMess");
//实例化并定位到 房间ID
var url = "https://secretmui.wilddogio.com/InfoMess/" + localStorage.roomid;
var room = new Wilddog(url);
//获取显示消息页面DOM结构
var msgList = document.getElementById("msg-list");
//发送消息 按钮的单击事件
send.addEventListener('tap',function(){
//获取系统时间
var myDate = new Date();
var mytime = myDate.getTime();
//获取房间号,昵称,输入内容
var roomid_val = roomid.value;
var nickname_val = nickname.value;
var msg_text_val = msg_text.value;
//向云端服务器写入json数据(昵称,输入内容,时间)
ref.child(roomid_val).child(mytime).set({"name":nickname_val,"content":msg_text_val,"time":mytime});
})
//DOM操作向msg-list结构中写入数据
function getInsertHtml(username,time,content){
var msgList = document.getElementById("msg-list");
msgList.style.border="1px dotted #8d8d8d";
var string = "<p class='name'>" + username + ":<span class='time'>" + getCurrentTime(time) + "</span></p><p class='content'>" + content + "</p>";
return string;
}
//绑定on单击事件
room.on('child_added',function(snapshot){
console.log(JSON.stringify(snapshot.val()));
msgList.innerHTML = msgList.innerHTML + getInsertHtml(snapshot.val().name,snapshot.val().time,snapshot.val().content);
msg_text.value = "";
})
//解析转换时间戳
function getCurrentTime(timestr){
var time = new Date(parseInt(timestr)*1000);
return time.toLocaleString().substr(0,30)+ time.toLocaleTimeString();
}
})
</script>
</body>
</html>