This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.html
156 lines (141 loc) · 3.74 KB
/
client.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
</head>
<style media="screen">
:root {
--foreground: white;
--background: #263238;
--highlight: #FFA000;
--highlight-deep: #FF6F00;
}
body {
display: flex;
flex-flow: row nowrap;
color: var(--foreground);
background-color: var(--background);
font-family: monospace;
}
div {
flex: 10;
display: flex;
flex-flow: column;
border: 2px solid white;
border-radius: 10px;
padding: 1rem;
margin: 0.5rem;
}
div li {
margin: 0 0.5rem;
}
textarea {
height: calc(100vh - 20rem);
}
.inputContainer {
flex: 0;
}
.text{
background-color: #cccccc;
font-family: monospace;
font-size: 1rem;
color: #333333;
border-radius: 5px;
}
.button {
height: 2.5rem;
background-color: var(--highlight-deep);
cursor: pointer;
border: 0;
font-family: monospace;
font-weight: bold;
font-size: 1.5rem;
color: var(--foreground);
border-radius: 5px;
margin-top: 0.5rem;
}
#next {
background-color: var(--highlight);
}
</style>
<body>
<div class="inputContainer">
<input id="event" class="text" placeholder="event" /> <br>
<textarea id="payload" class="text" placeholder="payload"></textarea> <br>
<button class="button" onclick="send()">Send</button>
<button id="next" class="button" onclick="nextCommand()">Next</button>
</div>
<div>
<div id="sentContainer">
<h1>Sent</h1>
<ol id="sent"></ol>
</div>
<div id="receivedContainer">
<h1>Received</h1>
<ol id="received"></ol>
</div>
</div>
<div class="inputContainer">
<p>User ID</p>
<input id="userid" class="text" value="2" placeholder="userid" />
<p>Item</p>
<input id="item" class="text" value="1" placeholder="item" /> <br>
<button id="connect" class="button" onclick="connect()">Connect</button>
</div>
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.1.1/socket.io.js"></script>
<script>
let socket;
function connect() {
const userId = document.querySelector("#userid").value;
const item = document.querySelector("#item").value;
socket = io("http://localhost:1337",{
query: `force-user=${userId}&item=${item}`,
transports: ['websocket'],
});
const onevent = socket.onevent;
socket.onevent = function(packet) {
onevent.call(this, packet);
console.log("Packet received:", packet);
addToReceived(packet.data[0], packet.data[1]);
};
}
function send() {
const event = document.querySelector("#event").value;
const payload = document.querySelector("#payload").value;
console.log("Message sent:", `event: ${event}\npayload:${payload}`);
addToSent(event, payload);
socket.emit(event, payload);
}
function nextCommand() {
socket.emit("next_command", "");
}
const addToSent = (e, p) => addTo("#sentContainer", e, p);
const addToReceived = (e, p) => addTo("#receivedContainer", e, p);
let lastPayloadId = 0;
function addTo(containerId, event, payload) {
lastPayloadId++;
const messages = document.querySelector(containerId);
let li = document.createElement("li");
li.innerHTML = `event: ${event}
<a
style="cursor: pointer; background-color: deeppink"
onclick="togglePayload('payload${lastPayloadId}')"
>
DETAIL
</a>
<textarea
readonly
id="payload${lastPayloadId}"
style="display: none; width: 50rem; background-color: black; color: white"
>${JSON.stringify(payload, null, " ")}</textarea>
`;
messages.append(li);
window.scrollTo(0, document.body.scrollHeight);
}
function togglePayload(payloadId) {
const li = document.querySelector(`#${payloadId}`);
li.style.display = li.style.display == "none" ? "inherit" : "none";
}
</script>
</html>