gemercheung 3 роки тому
коміт
bf51c5fc1c
10 змінених файлів з 2617 додано та 0 видалено
  1. 5 0
      .env
  2. 22 0
      .gitignore
  3. 11 0
      index.js
  4. 2427 0
      package-lock.json
  5. 32 0
      package.json
  6. 6 0
      src/connection/redis.js
  7. 4 0
      src/controller/basicController.js
  8. 17 0
      src/controller/index.js
  9. 57 0
      src/core/cluster.js
  10. 36 0
      src/core/io.js

+ 5 - 0
.env

@@ -0,0 +1,5 @@
+PORT=3000
+REDIS_HOST=localhost
+REDIS_PORT=6379
+REDIS_USER=root
+REDIS_PASSWORD=s1mpl3

+ 22 - 0
.gitignore

@@ -0,0 +1,22 @@
+.DS_Store
+node_modules
+dist
+
+# local env files
+.env.local
+.env.*.local
+
+# Log files
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+
+# Editor directories and files
+.idea
+.vscode
+*.suo
+*.ntvs*
+*.njsproj
+*.sln
+*.sw*
+*.bak

+ 11 - 0
index.js

@@ -0,0 +1,11 @@
+import "dotenv/config";
+import { CoreCluster } from "./src/core/cluster.js";
+
+// import fastify from "fastify";
+// import fastifyIO from "fastify-socket.io";
+
+// const server = fastify();
+
+// server.register(fastifyIO);
+
+const ClusterServer = new CoreCluster(process.env.PORT);

Різницю між файлами не показано, бо вона завелика
+ 2427 - 0
package-lock.json


+ 32 - 0
package.json

@@ -0,0 +1,32 @@
+{
+  "name": "chatim",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "dev": "DEBUG=* node index.js",
+    "hot-serve": "npx nodemon index.js"
+  },
+  "type": "module",
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "@socket.io/admin-ui": "^0.2.0",
+    "@socket.io/cluster-adapter": "^0.1.0",
+    "@socket.io/redis-adapter": "^7.1.0",
+    "@socket.io/sticky": "^1.0.1",
+    "dotenv": "^14.2.0",
+    "eiows": "^3.7.3",
+    "engine.io": "^6.1.2",
+    "fastify": "^3.25.3",
+    "fastify-socket.io": "^3.0.0",
+    "recluster": "^1.0.0",
+    "redis": "^4.0.2",
+    "socket.io": "^4.4.1",
+    "uWebSockets.js": "github:uNetworking/uWebSockets.js#v20.4.0"
+  },
+  "optionalDependencies": {
+    "bufferutil": "^4.0.6",
+    "utf-8-validate": "^5.0.8"
+  }
+}

+ 6 - 0
src/connection/redis.js

@@ -0,0 +1,6 @@
+import { createClient } from "redis";
+
+const pubClient = createClient({ url: "redis://localhost:6379" });
+const subClient = pubClient.duplicate();
+
+export { pubClient, subClient };

+ 4 - 0
src/controller/basicController.js

@@ -0,0 +1,4 @@
+export async function basicController(socket) {
+  let user = socket.handshake.query;
+  console.log("user", user);
+}

+ 17 - 0
src/controller/index.js

@@ -0,0 +1,17 @@
+import { io } from "../core/io.js";
+
+import { basicController } from "./basicController.js";
+
+class mainController {
+  constructor(io) {
+    this.io = io;
+    this.basicController = basicController.bind(this);
+  }
+  run() {
+    this.io.on("connection", this.basicController);
+  }
+}
+
+const controller = new mainController(io);
+
+export { controller };

+ 57 - 0
src/core/cluster.js

@@ -0,0 +1,57 @@
+import cluster from "cluster";
+import os from "os";
+import { setupMaster, setupWorker } from "@socket.io/sticky";
+import { createAdapter, setupPrimary } from "@socket.io/cluster-adapter";
+import { io } from "./io.js";
+import http from "http";
+import { Server } from "socket.io";
+import { controller } from "../controller/index.js";
+
+const numCPUs = os.cpus().length;
+
+export class CoreCluster {
+  constructor(port) {
+    this.port = port;
+    this.attachThead();
+  }
+  attachThead() {
+    if (cluster.isMaster) {
+      console.log("Total Number of Cores: %o", numCPUs);
+      console.log(`Master ${process.pid} is running`);
+
+      // setup sticky sessions
+      const httpServer = http.createServer();
+      setupMaster(httpServer, {
+        loadBalancingMethod: "least-connection",
+      });
+
+      setupPrimary();
+
+      cluster.setupPrimary({
+        serialization: "advanced",
+      });
+
+      httpServer.listen(this.port);
+      console.log(`server  is running on port ${this.port}`);
+
+      for (let i = 0; i < numCPUs; i++) {
+        cluster.fork();
+      }
+      cluster.on("exit", (worker) => {
+        console.log(`Worker ${worker.process.pid} died`);
+        cluster.fork();
+      });
+      cluster.on("online", (worker) => {
+        console.log("Worker %o is listening", worker.process.pid);
+      });
+    } else {
+      console.log(`Worker ${process.pid} started`);
+      // use the cluster adapter
+      io.adapter(createAdapter());
+
+      // setup connection with the primary process
+      controller.run();
+      setupWorker(io);
+    }
+  }
+}

+ 36 - 0
src/core/io.js

@@ -0,0 +1,36 @@
+import http from "http";
+import { Server } from "socket.io";
+import { instrument } from "@socket.io/admin-ui";
+import { createAdapter } from "@socket.io/redis-adapter";
+import { pubClient, subClient } from "../connection/redis.js";
+
+const httpServer = http.createServer();
+const io = new Server(httpServer, {
+  cors: {
+    origin: "*",
+  },
+  path: "/vr-node",
+  pingInterval: 10000,
+  pingTimeout: 5000,
+});
+
+instrument(io, {
+  //   auth: {
+  //     type: "basic",
+  //     username: "admin",
+  //     password: "$2b$10$heqvAkYMez.Va6Et2uXInOnkCT6/uQj1brkrbyG3LpopDklcq7ZOS", // "changeit" encrypted with bcrypt
+  //   },
+  auth: false,
+  namespaceName: "/watch",
+});
+
+Promise.all([pubClient.connect(), subClient.connect()])
+  .then(() => {
+    io.adapter(createAdapter(pubClient, subClient));
+    console.log("redis is conetcted");
+  })
+  .catch((error) => {
+    console.log("redis is fail", error);
+  });
+
+export { io };