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