-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.html
More file actions
61 lines (55 loc) · 1.9 KB
/
client.html
File metadata and controls
61 lines (55 loc) · 1.9 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
<!DOCTYPE html>
<html>
<head>
<title>WebSocket demo</title>
</head>
<body>
<input type="text" name="Name" id="cliName">
<button type="submit" onclick="setName();">Set Name</button>
<input type="text" name="Content" id="message">
<button type="submit" onclick="sendMsg();">Send Message</button>
<div>
<h1>Message</h1>
<ul id="list"></ul>
</div>
<script>
message = document.getElementById('message');
cliName = document.getElementById('cliName');
websocket = new WebSocket("ws://140.115.30.179:5555/");
id = '';
websocket.onmessage = function (event) {
data = JSON.parse(event.data);
if (data.header == "Reg Response"){
id = data.data;
}else if (data.header == "GET MSG"){
addAnother(data["data"]);
}
}
function setName(){
console.log(cliName.value);
websocket.send(JSON.stringify({
"header": "SET NAME",
"id": id,
"data": cliName.value
}));
}
function sendMsg(){
websocket.send(JSON.stringify({
"header": "SEND MSG",
"id": id,
"data": message.value
}))
}
function addAnother(msg) {
console.log(msg);
var ul = document.getElementById("list");
var li = document.createElement("li");
li.style = `color: ${msg['color']}`
li.appendChild(document.createTextNode(`
${msg['name']}: ${msg['msg']['data']}
`));
ul.prepend(li);
}
</script>
</body>
</html>