-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket2.html
69 lines (44 loc) · 1.25 KB
/
websocket2.html
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
<!DOCTYPE html>
<title>WebSocket Test Page</title>
<script>
var log = function(s) {
console.log(s);
if (document.readyState !== "complete") {
log.buffer.push(s);
} else {
document.getElementById("output").innerHTML += (s + "\n")
}
}
log.buffer = [];
url = "ws://192.168.0.10:8080";
w = new WebSocket(url);
w.onopen = function() {
log("open");
w.send("thank you for accepting this Web Socket request");
}
w.onmessage = function(e) {
console.log(e.data);
log(e.data);
}
w.onclose = function(e) {
log("closed");
}
window.onload = function() {
log(log.buffer.join("\n"));
document.getElementById("sendButton").onclick = function() {
console.log(document.getElementById("inputMessage").value);
w.send(document.getElementById("inputMessage").value);
}
// 간지나게 엔터키 누르면 메시지 날림
document.getElementById("inputMessage").onkeypress = function() {
if (event.keyCode == '13') {
value = document.getElementById("inputMessage").value
w.send(value);
document.getElementById("inputMessage").value = "";
}
}
}
</script>
<input type="text" id="inputMessage">
<button id="sendButton">Send</button>
<pre id="output"></pre>