practice.html 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. <!DOCTYPE HTML>
  2. <html lang="ru">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>
  6. Static File Index.HTML
  7. </title>
  8. <script src='https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js'></script>
  9. </head>
  10. <body>
  11. <div id='formContainer'>
  12. <input id="nickInput" class="nick" type="text" placeholder="Ник"><br>
  13. <input id="messageInput" class="message" type="text" placeholder="Сообщение"><br>
  14. <button id="sendButton" type="button">Отправить</button>
  15. </div>
  16. <script>
  17. const socket = io('http://socketchat.ed.asmer.org.ua/');
  18. socket.on('msg', ({nick, message}) => {
  19. let divElement = document.createElement('div');
  20. divElement.innerText = `nick - ${nick}, message - ${message}`
  21. document.querySelector('#formContainer').appendChild(divElement);
  22. });
  23. document.querySelector('#sendButton').addEventListener('click', () => {
  24. let nickInput = document.querySelector('#nickInput').value;
  25. let messageInput = document.querySelector('#messageInput').value;
  26. socket.emit('msg', {nick: nickInput, message: messageInput});
  27. });
  28. </script>
  29. </body>
  30. </html>