You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.6 KiB

4 years ago
  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="" id="messages-form">
  49. <input id="m" autocomplete="off"/>
  50. <button>Send</button>
  51. </form>
  52. <script src="https://code.jquery.com/jquery-3.5.1.min.js"
  53. integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
  54. <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
  55. <script>
  56. const params = new URLSearchParams(window.location.search);
  57. // const auth = params.get('auth');
  58. const auth = "j1ggYdoF0jccFuKesfcmboitSXYJjhJUPKGUI1Re6tdHeOJ5Fqa8K7k04APz";
  59. let socket ='';
  60. if (auth != null) {
  61. socket = io({
  62. query: {auth: auth, business:1}
  63. });
  64. } else {
  65. socket = io({});
  66. }
  67. console.log(window.location.host);
  68. </script>
  69. <script>
  70. $(document).ready(function () {
  71. $('#messages-form').on('submit', function (e) {
  72. e.preventDefault();
  73. socket.emit('chat-message', $('#m').val());
  74. $('#m').val('');
  75. return false;
  76. });
  77. socket.on('new-chat', function(msg){
  78. $('#messages').append($('<li>').text(msg));
  79. });
  80. socket.on('server-msg',function(msg){
  81. $('#messages').append('<li><b>' + msg + '</b></li>');
  82. });
  83. socket.on('public-event',function(msg){
  84. alert('public :' + msg);
  85. });
  86. socket.on('private-event', function(msg){
  87. alert('private :' + msg);
  88. });
  89. socket.on('room-event', function(msg) {
  90. $('#messages').append('<li><b>' + msg + '</b></li>');
  91. })
  92. });
  93. </script>
  94. </body>
  95. </html>