You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
914 B
HTML
41 lines
914 B
HTML
<!DOCTYPE HTML>
|
|
<html>
|
|
<head>
|
|
<title>Chat App</title>
|
|
</head>
|
|
<body>
|
|
<h1> A new Chat App </h1>
|
|
|
|
<ul id='messages'></ul>
|
|
<form id='form'>
|
|
<input id='input'autocomplete='off'/><button type='submit'>Send</button>
|
|
</form>
|
|
|
|
|
|
<p>This is some text </p>
|
|
<script src='/socket.io/socket.io.js'></script>
|
|
<script>
|
|
const socket = io();
|
|
const form = document.getElementById('form');
|
|
const input = document.getElementById('input');
|
|
const messages = document.getElementById('messages');
|
|
|
|
// send message
|
|
form.addEventListener('submit', function(e) {
|
|
e.preventDefault();
|
|
if(input.value) {
|
|
socket.emit('chat message',input.value);
|
|
input.value = '';
|
|
}
|
|
});
|
|
|
|
//receive message
|
|
socket.on('chat message', function(msg){
|
|
const item = document.createElement('li');
|
|
item.textContent = msg;
|
|
messages.appendChild(item);
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|