-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
93 lines (90 loc) · 2.54 KB
/
index.php
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WS Client</title>
<style>
body{
background-color: #fafbfc;
margin: 5em;
font-family: Arial, Helvetica, sans-serif;
}
h1{
text-align: center;
}
.cont{
width: 90%;
margin: 0 auto;
}
#msgboard{
border-radius: 15px 15px 0 0;
background-color: #fefefe;
box-sizing: border-box;
padding: 1em;
border: 1px solid #ccc;
height: 400px;
overflow-y: auto;
width: 100%;
}
#msg{
font-size: 14pt;
box-sizing: border-box;
padding: 1em;
border: 1px solid #ccc;
height: 120px;
overflow-y: auto;
width: 100%;
border-radius: 0 0 15px 15px;
resize: none;
}
.msg{
margin-right: auto;
width: 60%;
border: 1px solid #ccc;
background: #eee;
padding: 10px;
border-radius: 25px;
}
.msg.mine{
margin-right: 0;
margin-left: auto;
background: #eef;
}
</style>
</head>
<body>
<div class="cont">
<h1>Messenger</h1>
<div id="msgboard"></div>
<textarea id="msg" rows="6" placeholder="Enter to send"></textarea>
</div>
<script>
var conn = new WebSocket('ws://localhost:4000');
conn.onopen = function(e) {
msgboard.innerHTML += "<p class='msg'><strong>onOpen:</strong> Connection established!</p>";
console.log(e);
};
conn.onmessage = function(e) {
console.log(e);
var data = JSON.parse(e.data);
var mine = '';
if(data.mine) mine='mine';
msgboard.innerHTML += `<p class='msg ${mine}'><strong>${data.from}:</strong> ${data.msg}</p>`;
console.log(e.data);
};
conn.onclose = function(e) {
console.log(e);
msgboard.innerHTML = `<p class='msg'><strong>onClose:</strong> ${e.data}</p>`;
console.log(e.data);
};
msg.onkeyup = function(e){
if(e.keyCode == 13) {
conn.send(msg.value);
msg.value = '';
}
}
</script>
</body>
</html>