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.

45 lines
1.1 KiB

  1. var app = require("http").createServer(handler);
  2. var io = require("socket.io")(app);
  3. var fs = require("fs");
  4. function handler(req, res){
  5. var html = fs.readFileSync("index.html", "utf8");
  6. res.setHeader("Content-Type", "text/html");
  7. res.setHeader("Content-Length", Buffer.byteLength(html, "utf8"));
  8. res.end(html);
  9. }
  10. app.listen(80);
  11. io.on("connection", function(socket){
  12. socket.on("line", function(msg){//画线
  13. //向客户端发命令
  14. msg.name = "line";
  15. msgs.push(msg);
  16. io.sockets.emit("message", msg);
  17. });
  18. socket.on("text", function(msg){//文字
  19. msg.name = "text";
  20. msgs.push(msg);
  21. io.sockets.emit("message", msg);
  22. });
  23. socket.on("eraser", function(msg){//橡皮
  24. msg.name = "eraser";
  25. msgs.push(msg);
  26. io.sockets.emit("message", msg);
  27. });
  28. socket.on("load", function(msg){//从头画
  29. socket.emit("load", {
  30. name : "load",
  31. msgs : msgs
  32. });
  33. });
  34. socket.on("clear", function(msg){
  35. msgs.length = 0;
  36. io.sockets.emit("message", {
  37. name : "clear"
  38. });
  39. });
  40. });
  41. var msgs = [];