-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
139 lines (120 loc) · 3.34 KB
/
Copy pathindex.html
File metadata and controls
139 lines (120 loc) · 3.34 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}
a {
color: #00B7FF;
}
.chats {
padding: 10px 5px;
height: 200px;
width: 500px;
background: #eee;
overflow-y: scroll;
}
.chats .chat {
display: flex;
margin: 5px 0;
}
.chats .chat .group {
display: inherit;
word-wrap: normal;
}
.chats .chat .group .name {
padding: 0 10px;
font-weight: bold;
}
.chats .chat .time {
padding-left: 10px;
font-size: 10px;
line-height: 18px;
color: #aaa;
}
.message {
margin-top: 10px;
}
.message input {
padding: 0 5px;
height: 25px;
width: 300px;
}
.message input:first-child {
width: 80px;
}
.message button {
height: 32px;
width: 100px;
}
</style>
<body>
<div class="chats">
<div class="chat">
<div class="group">
<div class="name">Robby:</div>
<div class="msg">Hi~</div>
</div>
<div class="time">11分鐘前</div>
</div>
<div class="chat">
<div class="group">
<div class="name">Robby:</div>
<div class="msg">Hola~</div>
</div>
<div class="time">10分鐘前</div>
</div>
</div>
<div class="message">
<input id="name" type="text" placeholder="your name" />
<input id="msg" type="text" placeholder="input the message" />
<button type="button">送出</button>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/locale/zh-tw.js'></script>
<script>
function Send() {
let name = document.querySelector('#name').value;
let msg = document.querySelector('#msg').value;
if (!msg && !name) {
alert('請輸入大名和訊息');
return;
}
let data = {
name: name,
msg: msg,
};
socket.emit('message', data);
document.querySelector('#msg').value = '';
}
document.querySelector('button').addEventListener('click', () => {
Send();
});
function appendData(obj) {
let el = document.querySelector('.chats');
let html = el.innerHTML;
obj.forEach(element => {
html +=
`
<div class="chat">
<div class="group">
<div class="name">${element.name}:</div>
<div class="msg">${element.msg}</div>
</div>
<div class="time">${moment(element.time).fromNow()}</div>
</div>
`;
});
el.innerHTML = html.trim();
}
</script>
<script src='./socket.js'></script>
</body>
</html>