msg.html 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <h1>message chat</h1>
  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'> </div>
  12. <input type="text" id="nick" name="name" /><br>
  13. <input type="text" id="msg" name="name" /><br>
  14. <button type="button" id="btn"> SEND</button>
  15. <script>
  16. const socket = io("http://socketchat.ed.asmer.org.ua");
  17. socket.on('msg', (msg) => {
  18. console.log(msg);
  19. // document.createElement
  20. const div = document.createElement('div')
  21. div.innerHTML = `<strong> ${msg.nick}</strong>: ${msg.message}`
  22. formContainer.append(div);
  23. });
  24. document.getElementById('btn').onclick = () => {
  25. console.log(
  26. `${document.getElementById('nick').value}: ${document.getElementById('msg').value}`
  27. );
  28. socket.emit('msg', { nick: nick.value, message: msg.value });
  29. };
  30. </script>
  31. </body>
  32. </html>