started edition of chat in node
+ handled cors + added css fo chat scroll and newline
This commit is contained in:
55
tests_hugo/chat_node/chat_client/chat.html
Normal file
55
tests_hugo/chat_node/chat_client/chat.html
Normal file
@@ -0,0 +1,55 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" dir="ltr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="./style/chat.css" type="text/css" rel="stylesheet">
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
<input type="checkbox" id="chat_input" style="display: none;"/>
|
||||
|
||||
<div class="chat_box">
|
||||
|
||||
<div class="chat_item controls_area">
|
||||
</div>
|
||||
|
||||
<label class="chat_item open_close button" for="chat_input">
|
||||
<p>chat</p>
|
||||
</label>
|
||||
|
||||
<div class="chat_item msg_thread">
|
||||
<div class="msg_box" id="msg_thread">
|
||||
<!-- messages go there -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="chat_item msg_write">
|
||||
<div class="text_area" id="msg_write" contenteditable>
|
||||
<!-- write message here -->
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<button class="chat_item msg_send button" onclick="submit_new_message()">
|
||||
<p>send</p>
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
<!-- https://socket.io/docs/v4/client-installation/ -->
|
||||
<!-- https://socket.io/docs/v4/client-api/ -->
|
||||
<script src="https://cdn.socket.io/4.5.3/socket.io.min.js" integrity="sha384-WPFUvHkB1aHA5TDSZi6xtDgkF0wXJcIIxXhC6h8OT8EH3fC5PWro5pWJ1THjcfEi" crossorigin="anonymous"></script>
|
||||
<script>
|
||||
const socket = io("http://localhost:3000");
|
||||
socket.on("connect", () => {
|
||||
console.log("client: connection");
|
||||
});
|
||||
</script>
|
||||
|
||||
<script src="./chat_submit_msg.js"></script>
|
||||
<script src="./chat_receive_msg.js"></script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
20
tests_hugo/chat_node/chat_client/chat_receive_msg.js
Normal file
20
tests_hugo/chat_node/chat_client/chat_receive_msg.js
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
socket.on('chat_message', ({ data }) => {
|
||||
handle_new_message(data);
|
||||
});
|
||||
|
||||
const handle_new_message = (message) => {
|
||||
const div_thread = document.getElementById('msg_thread');
|
||||
|
||||
console.log("received message:");
|
||||
console.log(`[${message}]`);
|
||||
|
||||
div_thread.appendChild(build_new_message(message));
|
||||
}
|
||||
|
||||
const build_new_message = (message) => {
|
||||
const p = document.createElement("p");
|
||||
p.appendChild(document.createTextNode(message));
|
||||
return p;
|
||||
}
|
||||
|
||||
18
tests_hugo/chat_node/chat_client/chat_submit_msg.js
Normal file
18
tests_hugo/chat_node/chat_client/chat_submit_msg.js
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
const submit_new_message = () => {
|
||||
const div_msg = document.getElementById('msg_write');
|
||||
/*
|
||||
const msg = div_msg.value;
|
||||
const msg = div_msg.innerText;
|
||||
*/
|
||||
const msg = div_msg.innerText.trim();
|
||||
div_msg.innerText = "";
|
||||
|
||||
console.log("msg:");
|
||||
console.log(`[${msg}]`);
|
||||
console.log(msg.length);
|
||||
|
||||
if (msg.length > 0)
|
||||
socket.emit('chat_message', { data: msg });
|
||||
}
|
||||
|
||||
113
tests_hugo/chat_node/chat_client/style/chat.css
Normal file
113
tests_hugo/chat_node/chat_client/style/chat.css
Normal file
@@ -0,0 +1,113 @@
|
||||
|
||||
@import 'msg_thread.css';
|
||||
@import 'msg_write.css';
|
||||
|
||||
/**
|
||||
* GRID
|
||||
*/
|
||||
|
||||
|
||||
/* Hide scrollbar */
|
||||
.chat_box * {
|
||||
-ms-overflow-style: none; /* IE and Edge */
|
||||
scrollbar-width: none; /* Firefox */
|
||||
}
|
||||
.chat_box *::-webkit-scrollbar {
|
||||
display: none; /* Chrome, Safari and Opera */
|
||||
}
|
||||
|
||||
/* global settings */
|
||||
.chat_box * {
|
||||
position: relative;
|
||||
box-sizing: border-box;
|
||||
/* Hide scrollbar for IE, Edge and Firefox */
|
||||
-ms-overflow-style: none; /* IE and Edge */
|
||||
scrollbar-width: none; /* Firefox */
|
||||
}
|
||||
.chat_box .chat_item.controls_area { grid-area: controls;}
|
||||
.chat_box .chat_item.open_close { grid-area: open_close;}
|
||||
.chat_box .chat_item.msg_thread { grid-area: msg_thread;}
|
||||
.chat_box .chat_item.msg_write { grid-area: msg_write;}
|
||||
.chat_box .chat_item.msg_send { grid-area: msg_send;}
|
||||
.chat_box {
|
||||
position: fixed;
|
||||
bottom: 20px;
|
||||
right: 20px;
|
||||
display: grid;
|
||||
grid:
|
||||
' controls open_close ' auto
|
||||
' msg_thread msg_thread ' 1fr
|
||||
' msg_write msg_send ' auto
|
||||
/ 1fr auto;
|
||||
gap: 0px;
|
||||
padding: 0px;
|
||||
width: auto;
|
||||
height: auto;
|
||||
|
||||
border: 1px solid green;
|
||||
}
|
||||
.chat_box .chat_item * {
|
||||
display: flex;
|
||||
}
|
||||
/* buttons settings */
|
||||
.chat_box .chat_item.button {
|
||||
display: flex;
|
||||
width: auto;
|
||||
padding: 10px;
|
||||
cursor: pointer;
|
||||
outline: none;
|
||||
border: none;
|
||||
background-color: rgb(220, 220, 220);
|
||||
}
|
||||
.chat_box .chat_item.button p {
|
||||
margin: auto;
|
||||
}
|
||||
.chat_box .chat_item.button:hover {
|
||||
background-color: rgb(200, 200, 200);
|
||||
}
|
||||
.chat_box .chat_item.button:active {
|
||||
background-color: rgb(220, 220, 220);
|
||||
}
|
||||
/* collapse settings */
|
||||
.chat_box .chat_item:not(.open_close) {
|
||||
display: none;
|
||||
}
|
||||
#chat_input:checked +
|
||||
.chat_box {
|
||||
gap: 5px;
|
||||
padding: 5px;
|
||||
width: 300px;
|
||||
height: 400px;
|
||||
}
|
||||
#chat_input:checked +
|
||||
.chat_box .chat_item {
|
||||
display: flex;
|
||||
}
|
||||
#chat_input:checked +
|
||||
.chat_box .chat_item.open_close p {
|
||||
display: none;
|
||||
}
|
||||
#chat_input:checked +
|
||||
.chat_box .chat_item.open_close {
|
||||
/*
|
||||
*/
|
||||
width: 30px;
|
||||
height: 20px;
|
||||
padding: 0px;
|
||||
justify-self: end;
|
||||
background-color: transparent;
|
||||
border: none;
|
||||
}
|
||||
#chat_input:checked +
|
||||
.chat_box .chat_item.open_close::before {
|
||||
--collapse_width: 20px;
|
||||
content: "";
|
||||
position: absolute;
|
||||
left: 50%;
|
||||
top: 50%;
|
||||
transform: translate(-50%, -50%);
|
||||
height: 2px;
|
||||
width: 15px;
|
||||
background-color: black;
|
||||
}
|
||||
|
||||
19
tests_hugo/chat_node/chat_client/style/msg_thread.css
Normal file
19
tests_hugo/chat_node/chat_client/style/msg_thread.css
Normal file
@@ -0,0 +1,19 @@
|
||||
.chat_box .chat_item.msg_thread {
|
||||
flex-direction: column-reverse;
|
||||
overflow: scroll;
|
||||
border: 1px solid blue;
|
||||
}
|
||||
|
||||
.chat_box .chat_item .msg_box {
|
||||
flex-direction: column;
|
||||
width: 100%;
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
.chat_box #msg_thread p {
|
||||
white-space: pre-wrap;
|
||||
margin: 5px auto 5px 0px;
|
||||
padding: 5px;
|
||||
border-radius: 5px;
|
||||
background-color: rgb(210, 210, 210);
|
||||
}
|
||||
20
tests_hugo/chat_node/chat_client/style/msg_write.css
Normal file
20
tests_hugo/chat_node/chat_client/style/msg_write.css
Normal file
@@ -0,0 +1,20 @@
|
||||
.chat_box .chat_item .text_area {
|
||||
display: block;
|
||||
position: absolute;
|
||||
bottom: 0px;
|
||||
left: 0px;
|
||||
width: 100%;
|
||||
height: auto;
|
||||
min-height: 100%;
|
||||
max-height: 300px;
|
||||
/*
|
||||
resize: none;
|
||||
*/
|
||||
overflow-x: hidden;
|
||||
overflow-y: scroll;
|
||||
background-color: white;
|
||||
border: 1px solid red;
|
||||
}
|
||||
.chat_box .chat_item .text_area div {
|
||||
display: block ruby;
|
||||
}
|
||||
Reference in New Issue
Block a user