index.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
  9. </head>
  10. <style>
  11. .chat__header {
  12. text-align: left;
  13. }
  14. .chat__message-content {
  15. border-radius: 5px;
  16. padding: 7px;
  17. background-color: #ccc;
  18. display: inline-block;
  19. margin-left: 5px;
  20. }
  21. </style>
  22. <body>
  23. <main class="main">
  24. <div class="chat">
  25. <h1 class="chat__header">Chat</h1>
  26. <div id="content" class = "chat__header">
  27. <div class="chat__item">
  28. <h1 id = "chat__message-content" class="chat__message-content"></h1>
  29. </div>
  30. <div class="send_form">
  31. Nick :<input type="text" id = 'nick' >
  32. Message :<input type="text" id = 'msg'>
  33. <button id="sendButton">Отправить</button>
  34. </div>
  35. </div>
  36. </main>
  37. <script>
  38. const socket = io('http://socketchat.ed.asmer.org.ua')
  39. socket.on('msg', msg => {
  40. console.log(msg)
  41. const my_P = document.createElement("div");
  42. my_P.innerHTML = 'nick : ' + msg.nick + " | message:" + msg.message
  43. document.getElementById("chat__message-content").appendChild(my_P)
  44. })
  45. sendButton.onclick = () => {
  46. console.log(nick.value , msg.value)
  47. socket.emit('msg', {nick: nick.value, message: msg.value})
  48. }
  49. </script>
  50. </body>
  51. </html>