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.

70 lines
1.7 KiB

9 years ago
  1. <html>
  2. <head>
  3. <meta charset="utf-8">
  4. <title>goofy gophers</title>
  5. <style>
  6. body {
  7. cursor: none;
  8. overflow: hidden;
  9. }
  10. .gopher {
  11. background-image: url('https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Go_gopher_mascot_bw.png/768px-Go_gopher_mascot_bw.png');
  12. width: 95px;
  13. height: 95px;
  14. background-size: cover;
  15. position: absolute;
  16. left: 0px;
  17. top: 0px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <script>
  23. var url = "ws://" + window.location.host + "/ws";
  24. var ws = new WebSocket(url);
  25. var myid = -1;
  26. ws.onmessage = function (msg) {
  27. var cmds = {"iam": iam, "set": set, "dis": dis};
  28. if (msg.data) {
  29. var parts = msg.data.split(" ")
  30. var cmd = cmds[parts[0]];
  31. if (cmd) {
  32. cmd.apply(null, parts.slice(1));
  33. }
  34. }
  35. };
  36. function iam(id) {
  37. myid = id;
  38. }
  39. function set(id, x, y) {
  40. var node = document.getElementById("gopher-" + id);
  41. if (!node) {
  42. node = document.createElement("div");
  43. document.body.appendChild(node);
  44. node.className = "gopher";
  45. node.style.zIndex = id + 1;
  46. node.id = "gopher-" + id;
  47. }
  48. node.style.left = x + "px";
  49. node.style.top = y + "px";
  50. }
  51. function dis(id) {
  52. var node = document.getElementById("gopher-" + id);
  53. if (node) {
  54. document.body.removeChild(node);
  55. }
  56. }
  57. window.onmousemove = function (e) {
  58. if (myid > -1) {
  59. set(myid, e.pageX, e.pageY);
  60. ws.send([e.pageX, e.pageY].join(" "));
  61. }
  62. }
  63. </script>
  64. </body>
  65. </html>