124 lines
4.5 KiB
HTML
124 lines
4.5 KiB
HTML
<!doctype html>
|
|
<html>
|
|
<head>
|
|
<title>Socket.IO chat</title>
|
|
<script src="/socket.io/socket.io.js"></script>
|
|
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
|
|
<link rel="stylesheet" type="text/css" href="//use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
|
|
<script>
|
|
$(function () {
|
|
// Create socket
|
|
var socket = io();
|
|
const date = new Date();
|
|
|
|
// Add zero to values under 10 (time)
|
|
function addZero(i) {
|
|
if (i < 10) {
|
|
i = '0' + i;
|
|
}
|
|
return i;
|
|
}
|
|
|
|
// Submit textarea on enter
|
|
$('div.input_container > form > textarea').on('keydown', function(event) {
|
|
if (event.keyCode == 13)
|
|
if (!event.shiftKey) {
|
|
event.preventDefault;
|
|
$('div.input_container > form').submit();
|
|
}
|
|
});
|
|
|
|
// Wait for server response and identify client
|
|
socket.on('user_identify', function(){
|
|
socket.emit('user_login', {
|
|
'username': 'BUUUUUUUUUURTE',
|
|
'mailHash': '289e66591abdb9c2d4cb15517f0fd8d8'
|
|
});
|
|
});
|
|
|
|
// Send data to server on form submit
|
|
$('form').submit(function(e){
|
|
e.preventDefault();
|
|
socket.emit('message_send', $('.input_container textarea[name="message"]').val());
|
|
$('.input_container textarea[name="message"]').val('');
|
|
return false;
|
|
});
|
|
|
|
// Receive messages from server
|
|
socket.on('message_receive', function(msg){
|
|
if (msg.username !== 'Server') {
|
|
var date = new Date();
|
|
document.getElementById('message_container').innerHTML +=
|
|
`<div class="message">
|
|
<img src="https://www.gravatar.com/avatar/${msg.mailHash}?s=48">
|
|
<div>
|
|
<span>${msg.username}</span>
|
|
<span>${addZero(date.getHours()) + ':' + addZero(date.getMinutes())}</span>
|
|
<span>${msg.message}</span>
|
|
</div>
|
|
</div>`;
|
|
} else {
|
|
document.getElementById('message_container').innerHTML +=
|
|
`<div class="servermessage">
|
|
<span>${msg.username}: ${msg.message}</span>
|
|
</div>`;
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<style>
|
|
body {
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
}
|
|
div.chat_container {
|
|
border: 1px solid black;
|
|
width: 500px;
|
|
}
|
|
div#message_container {
|
|
border: 1px solid black;
|
|
height: 500px;
|
|
overflow: auto;
|
|
}
|
|
div.input_container {
|
|
border: 1px solid black;
|
|
}
|
|
div.input_container > form > textarea, div.input_container > form > button {
|
|
margin: 10px;
|
|
width: calc(100% - 20px);
|
|
}
|
|
div.message {
|
|
background-color: white;
|
|
height: auto;
|
|
word-wrap: anywhere;
|
|
display: grid;
|
|
grid-template-columns: 48px auto;
|
|
}
|
|
div.message > div {
|
|
height: auto;
|
|
display: grid;
|
|
padding: 10px 10px 10px 20px;
|
|
grid-template-areas:
|
|
'username time'
|
|
'message message';
|
|
}
|
|
div.message > img { padding: 10px; }
|
|
div.message > div > span:nth-child(1) { grid-area: username; border-left: 3px solid black; padding-left: 5px;}
|
|
div.message > div > span:nth-child(2) { grid-area: time; text-align: right;}
|
|
div.message > div > span:nth-child(3) { grid-area: message; border-left: 3px solid black; padding-left: 5px;}
|
|
div.servermessage > span { color: purple; border-left: 3px solid purple; padding-left: 5px; margin-left: 10px;}
|
|
</style>
|
|
<div class="chat_container">
|
|
<div id="message_container">
|
|
<!-- JS will push messages here -->
|
|
</div>
|
|
<div class="input_container">
|
|
<form action="">
|
|
<textarea name="message"></textarea>
|
|
<button type="submit"><i class="fas fa-paper-plane"></i> Send</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html> |