expand client
This commit is contained in:
parent
7341a8802f
commit
b82a0f32c7
1 changed files with 34 additions and 33 deletions
|
@ -12,45 +12,46 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>Cards For Humanity Test Client</h1>
|
<h1>Cards For Humanity Test Client</h1>
|
||||||
<input id="username" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="username">
|
<div id="status">
|
||||||
<button id="join-chat" type="button">Join Chat</button>
|
<p><em>Disconnected...</em></p>
|
||||||
<textarea id="chat" style="display:block; width:600px; height:400px; box-sizing: border-box" cols="30" rows="10"></textarea>
|
</div>
|
||||||
<input id="input" style="display:block; width:600px; box-sizing: border-box" type="text" placeholder="chat">
|
<form id="login" onsubmit="loginSubmit();return false">
|
||||||
|
<input id="username" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="username">
|
||||||
|
<button id="join-chat" type="submit">Join Chat</button>
|
||||||
|
</form>
|
||||||
|
<form id="chat" onsubmit="chatSubmit();return false">
|
||||||
|
<textarea id="chat-history" style="display:block; width:600px; height:400px; box-sizing: border-box" cols="30" rows="10"></textarea>
|
||||||
|
<input id="chat-input" style="display:block; width:600px; box-sizing: border-box" type="text" placeholder="chat">
|
||||||
|
</form>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
const username = document.querySelector("#username");
|
websocket = new WebSocket("ws://localhost:3030/websocket");
|
||||||
const join_btn = document.querySelector("#join-chat");
|
|
||||||
const textarea = document.querySelector("#chat");
|
|
||||||
const input = document.querySelector("#input");
|
|
||||||
|
|
||||||
join_btn.addEventListener("click", function(e) {
|
function loginSubmit() {
|
||||||
this.disabled = true;
|
document.getElementById("join-chat").disabled = true;
|
||||||
|
websocket.send(username.value);
|
||||||
|
};
|
||||||
|
|
||||||
const websocket = new WebSocket("ws://localhost:3000/websocket");
|
websocket.onopen = function() {
|
||||||
|
console.log("connection opened");
|
||||||
|
document.getElementById("status").innerHTML = '<p><em>Connected!</em></p>';
|
||||||
|
}
|
||||||
|
|
||||||
websocket.onopen = function() {
|
websocket.onclose = function() {
|
||||||
console.log("connection opened");
|
console.log("connection closed");
|
||||||
websocket.send(username.value);
|
// document.getElementById("join-chat").disabled = false;
|
||||||
}
|
document.getElementById("status").innerHTML = '<p><em>Disconnected...</em></p>';
|
||||||
|
}
|
||||||
|
|
||||||
const btn = this;
|
websocket.onmessage = function(e) {
|
||||||
|
console.log("received message: "+e.data);
|
||||||
|
document.getElementById("chat-history").value += e.data+"\r\n";
|
||||||
|
}
|
||||||
|
|
||||||
websocket.onclose = function() {
|
function chatSubmit() {
|
||||||
console.log("connection closed");
|
let input = document.getElementById("chat-input");
|
||||||
btn.disabled = false;
|
websocket.send(input.value);
|
||||||
}
|
input.value = "";
|
||||||
|
};
|
||||||
websocket.onmessage = function(e) {
|
|
||||||
console.log("received message: "+e.data);
|
|
||||||
textarea.value += e.data+"\r\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
input.onkeydown = function(e) {
|
|
||||||
if (e.key == "Enter") {
|
|
||||||
websocket.send(input.value);
|
|
||||||
input.value = "";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Add table
Reference in a new issue