Ura - 26. 11. 2022
This commit is contained in:
16
static/index.html
Normal file
16
static/index.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<title>Document</title>
|
||||
|
||||
<script src="/socket.io/socket.io.js"></script>
|
||||
<script defer src="script.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<input id="input" type="text" />
|
||||
<button id="send">Send</button>
|
||||
|
||||
<div id="chat"></div>
|
||||
</body>
|
||||
</html>
|
||||
29
static/script.js
Normal file
29
static/script.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const input = document.getElementById('input');
|
||||
const button = document.getElementById('send');
|
||||
const chat = document.getElementById('chat');
|
||||
|
||||
const name = prompt('Enter your name');
|
||||
|
||||
const socket = io();
|
||||
|
||||
socket.on('connect', () => {
|
||||
console.log('connected');
|
||||
|
||||
socket.emit('name', name);
|
||||
});
|
||||
|
||||
socket.on('text', (text) => {
|
||||
console.log(text);
|
||||
addText(text);
|
||||
});
|
||||
|
||||
button.addEventListener('click', () => {
|
||||
socket.emit('text', input.value);
|
||||
addText(input.value);
|
||||
});
|
||||
|
||||
function addText(text) {
|
||||
const p = document.createElement('p');
|
||||
p.innerText = text;
|
||||
chat.appendChild(p);
|
||||
}
|
||||
Reference in New Issue
Block a user