websocket 增加多分组 fork https://github.com/olahol/melody
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.

50 lines
1.1 KiB

  1. <html>
  2. <head>
  3. <title>Melody example: chatting</title>
  4. </head>
  5. <style>
  6. #chat {
  7. text-align: left;
  8. background: #f1f1f1;
  9. width: 500px;
  10. min-height: 300px;
  11. padding: 20px;
  12. }
  13. </style>
  14. <body>
  15. <center>
  16. <h3>Chat</h3>
  17. <pre id="chat"></pre>
  18. <input placeholder="say something" id="text" type="text">
  19. </center>
  20. <script>
  21. var url = "ws://" + window.location.host + "/ws";
  22. var ws = new WebSocket(url);
  23. var name = "Guest" + Math.floor(Math.random() * 1000);
  24. var chat = document.getElementById("chat");
  25. var text = document.getElementById("text");
  26. var now = function () {
  27. var iso = new Date().toISOString();
  28. return iso.split("T")[1].split(".")[0];
  29. };
  30. ws.onmessage = function (msg) {
  31. var line = now() + " " + msg.data + "\n";
  32. chat.innerText += line;
  33. };
  34. text.onkeydown = function (e) {
  35. if (e.keyCode === 13 && text.value !== "") {
  36. ws.send("<" + name + "> " + text.value);
  37. text.value = "";
  38. }
  39. };
  40. </script>
  41. </body>
  42. </html>