muti-client.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { io } from "socket.io-client";
  2. import customParser from "socket.io-msgpack-parser";
  3. const URL = process.env.URL || "wss://test-socket.4dkankan.com";
  4. const MAX_CLIENTS = 600;
  5. const POLLING_PERCENTAGE = 0.05;
  6. const CLIENT_CREATION_INTERVAL_IN_MS = 10;
  7. const EMIT_INTERVAL_IN_MS = 1000;
  8. // wws://test-socket.4dkankan.com/watch
  9. let clientCount = 0;
  10. let lastReport = new Date().getTime();
  11. let packetsSinceLastReport = 0;
  12. let testSceneNum = "t-test";
  13. let roomId = "00001";
  14. let userLimitNum = 2000;
  15. let agentId = 0;
  16. const createAgent = () => {
  17. agentId += 1;
  18. const nickName = `test_name_${agentId}`;
  19. const userId = `6666666${agentId}`;
  20. const role = agentId === 1 ? "leader" : "role";
  21. createClient({ userId, nickName, from: '0', role });
  22. createClient({ userId, nickName, from: '1', role });
  23. createClient({ userId, nickName, from: '2', role });
  24. };
  25. const createClient = ({ userId, nickName, from, role }) => {
  26. // for demonstration purposes, some clients stay stuck in HTTP long-polling
  27. const socket = io(URL, {
  28. path: "/fsl-node",
  29. transport: ["websocket"],
  30. parser: customParser,
  31. query: {
  32. userId: userId,
  33. from: from || 2,
  34. sceneNum: testSceneNum,
  35. role: role,
  36. nickName: nickName,
  37. roomId: roomId,
  38. voiceStatus: 0,
  39. enableTalk: true,
  40. isAuthMic: 0,
  41. isAllowMic: 0,
  42. userLimitNum,
  43. myHeadUrl: "http://downza.img.zz314.com/edu/pc/wlgj-1008/2016-06-23/64ec0888b15773e3ba5b5f744b9df16c.jpg",
  44. },
  45. });
  46. setInterval(() => {
  47. socket.emit("client to server event");
  48. }, EMIT_INTERVAL_IN_MS);
  49. socket.on("server to client event", () => {
  50. packetsSinceLastReport++;
  51. });
  52. socket.on("disconnect", (reason) => {
  53. console.log(`disconnect due to ${reason}`);
  54. });
  55. if (++clientCount < MAX_CLIENTS) {
  56. setTimeout(createAgent, CLIENT_CREATION_INTERVAL_IN_MS);
  57. }
  58. };
  59. createAgent();
  60. const printReport = () => {
  61. const now = new Date().getTime();
  62. const durationSinceLastReport = (now - lastReport) / 1000;
  63. const packetsPerSeconds = (packetsSinceLastReport / durationSinceLastReport).toFixed(2);
  64. console.log(`client count: ${clientCount} ; average packets received per second: ${packetsPerSeconds}`);
  65. packetsSinceLastReport = 0;
  66. lastReport = now;
  67. };
  68. setInterval(printReport, 5000);