-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
134 lines (127 loc) · 4 KB
/
index.html
File metadata and controls
134 lines (127 loc) · 4 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
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>websocket</title>
<style>
body {
display: flex;
align-items: center;
justify-content: center;
}
#main {
display: flex;
flex-direction: column;
background-color: bisque;
}
#game {
display: flex;
}
button {
width: 100px;
height: 100px;
border: 1px solid gray;
outline: none;
background: aquamarine;
}
button:not(:last-child) {
margin-right: 50px;
}
#close {
width: 400px;
}
p {
width: 400px;
margin: 100px 0;
text-align: center;
font-size: 2rem;
}
</style>
</head>
<body>
<div id="main">
<div id="game">
<button id="stone">stone</button>
<button id="scissors">scissors</button>
<button id="cloth">cloth</button>
</div>
<p id="result">
Please choose!
</p>
<button id="close" onclick="close">close</button>
</div>
</body>
<script type="text/javascript">
const ws = new WebSocket("ws://127.0.0.1:8080");
const stone = document.getElementById('stone');
const scissors = document.getElementById('scissors');
const cloth = document.getElementById('cloth');
const p = document.getElementById('result');
let tmpOthers = '';
let tmpMine = '';
ws.onopen = () => {
console.log('Connection open ...');
};
ws.onclose = () => {
console.log("Connection closed.");
};
const close = () => {
ws.close();
};
const result = () => {
let result = '';
if (tmpMine === 'stone') {
if (tmpOthers === 'stone') {
result = 'equal';
} else if (tmpMine === 'scissors') {
result = 'you win';
} else if (tmpMine === 'cloth') {
result = 'you lose';
}
} else if (tmpMine === 'scissors') {
if (tmpOthers === 'stone') {
result = 'you lose';
} else if (tmpMine === 'scissors') {
result = 'equal';
} else if (tmpMine === 'cloth') {
result = 'you win';
}
}else if (tmpMine === 'cloth') {
if (tmpOthers === 'stone') {
result = 'you wim';
} else if (tmpMine === 'scissors') {
result = 'you lose';
} else if (tmpMine === 'cloth') {
result = 'equal';
}
}
setTimeout(() => {
tmpOthers = '';
tmpMine = '';
result = 'Please choose!';
}, 3000);
};
ws.onmessage = (event) => {
const others = event.data;
if (tmpOthers === '') {
tmpOthers = others;
if (tmpMine !== '') {
result();
}
}
};
const send = (mine) => {
ws.send(mine);
if (tmpMine === '') {
tmpMine = mine;
if(tmpOthers !== '') {
result();
}
}
};
stone.addEventListener('click', () => send('stone'));
scissors.addEventListener('click', () => send('scissors'));
cloth.addEventListener('click', () => send('cloth'));
</script>
</html>