index.html 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Socket.IO chat</title>
  5. <style>
  6. * {
  7. margin: 0;
  8. padding: 0;
  9. box-sizing: border-box;
  10. }
  11. body {
  12. font: 13px Helvetica, Arial;
  13. }
  14. form {
  15. background: #000;
  16. padding: 3px;
  17. position: fixed;
  18. bottom: 0;
  19. width: 100%;
  20. }
  21. form input {
  22. border: 0;
  23. padding: 10px;
  24. width: 90%;
  25. margin-right: 0.5%;
  26. }
  27. form button {
  28. width: 9%;
  29. background: rgb(130, 224, 255);
  30. border: none;
  31. padding: 10px;
  32. }
  33. #messages {
  34. list-style-type: none;
  35. margin: 0;
  36. padding: 0;
  37. }
  38. #messages li {
  39. padding: 5px 10px;
  40. }
  41. #messages li:nth-child(odd) {
  42. background: #eee;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <ul id="messages"></ul>
  48. <form action="">
  49. <input id="m" autocomplete="off" /><button>Send</button>
  50. </form>
  51. <script src="/socket.io/socket.io.js"></script>
  52. <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
  53. <script>
  54. $(function () {
  55. var socket = io();
  56. $("form").submit(function (e) {
  57. e.preventDefault(); // prevents page reloading
  58. socket.emit("chat message", $("#m").val());
  59. $("#m").val("");
  60. return false;
  61. });
  62. socket.on("chat message", function (msg) {
  63. $("#messages").append($("<li>").text(msg));
  64. });
  65. });
  66. </script>
  67. </body>
  68. </html>