rindy 4 年之前
當前提交
a1fad502bf
共有 7 個文件被更改,包括 1412 次插入0 次删除
  1. 25 0
      .gitignore
  2. 162 0
      daikan/rtc-live.js
  3. 1154 0
      package-lock.json
  4. 19 0
      package.json
  5. 9 0
      test/js/socket.io.js
  6. 7 0
      test/js/socket.io.min.js
  7. 36 0
      test/rtc-live.html

+ 25 - 0
.gitignore

@@ -0,0 +1,25 @@
+.DS_Store
+node_modules
+/dist
+/public/static/dll/*.*
+
+# 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
+/release-fix.bat
+/docs/html

+ 162 - 0
daikan/rtc-live.js

@@ -0,0 +1,162 @@
+const { v4: uuidv4 } = require("uuid")
+const server = require("http").createServer()
+const port = 10010
+const io = require("socket.io")(server, {
+    path: "/im-rtc",
+    serveClient: false,
+    pingInterval: 10000,
+    pingTimeout: 5000,
+    cors: {
+        origin: "*",
+        methods: ["GET", "POST"],
+    },
+})
+
+io.on("connection", socket => new ClientRequest(socket))
+
+server.listen(port)
+
+const __rooms = {}
+class ClientRequest {
+    constructor(socket) {
+        this.socket = socket
+        this.option = socket.handshake.query
+        this.init()
+
+        // 没有roomId时新建
+        if (!this.option.roomId) {
+            this.option.roomId = uuidv4()
+        }
+
+        // 没有角色时默认为主持人
+        if (!this.option.role) {
+            this.option.role = "leader"
+        }
+
+        // 判断roomId在房间列表中是否存在
+        if (!__rooms[this.option.roomId]) {
+            // 初始化房间
+            __rooms[this.option.roomId] = []
+        }
+
+        // 没有userId时新建
+        if (!this.option.userId) {
+            // 没有userId就新建
+            this.option.userId = "user-" + (__rooms[this.option.roomId].length + 1)
+        }
+
+        let { role, type, roomId, userId, userName } = this.option
+        let user = { role, type, roomId, userId, userName }
+        let users = __rooms[this.option.roomId]
+
+        users.push(user)
+
+        this.socket.join(this.option.roomId)
+        this.socket.emit("join", {
+            user,
+            users,
+        })
+        this.onUserJoin(user, users)
+    }
+    init() {
+        this.socket.on("disconnect", reason => this.onDisconnect(reason))
+        this.socket.on("reconnect", reason => this.onReconnect(reason))
+
+        // 通用事件
+        this.socket.on("action", data => {
+            this.socket.broadcast.to(this.option.roomId).emit("action", data)
+        })
+
+        // 踢人
+        this.socket.on("getOut", userId => this.onGetOut(userId))
+
+        // 静音
+        this.socket.on("muted", (muted, userId) => this.onMuted(muted, userId))
+    }
+
+    onDisconnect(reason) {
+        let user = null
+        let users = __rooms[this.option.roomId]
+
+        for (let i = 0; i < users.length; i++) {
+            if (this.option.userId == users[i].userId) {
+                let splices = users.splice(i, 1)
+                if (splices.length) {
+                    user = splices[0]
+                }
+                break
+            }
+        }
+
+        user && this.onUserLeave(user, users)
+    }
+    onReconnect(reason) {}
+
+    /**
+     * 踢人
+     */
+    onGetOut(userId) {
+        if (!this.isLeader()) {
+            return
+        }
+        this.socket.broadcast.to(this.option.roomId).emit("getOut", userId)
+    }
+
+    /**
+     * 静音
+     * @param {*} muted 静音状态
+     * @param {*} userId 如果有userId,则设置指定用户静音
+     */
+    onMuted(muted, userId) {
+        // 群体静音只有主持人才能使用
+        if (!this.isLeader()) {
+            return
+        }
+
+        let user = null
+        let users = __rooms[this.option.roomId]
+
+        if (userId) {
+            for (let i = 0; i < users.length; i++) {
+                if (users[i].userId == userId) {
+                    user = users[i]
+                    user.muted = muted
+                    break
+                }
+            }
+
+            // 没找到指定参与人时不做操作
+            if (!user) {
+                return
+            }
+        } else {
+            // 设置所有参与人除主持人外静音状态
+            users.forEach(item => {
+                if(item.role == 'leader'){
+                    return
+                }
+                item.muted = muted
+            });
+        }
+
+        this.socket.broadcast.to(this.option.roomId).emit("muted", muted, userId)
+    }
+    /**
+     * 通知其他用户有人加入房间
+     */
+    onUserJoin(user, users) {
+        this.socket.broadcast.to(this.option.roomId).emit("userJoin", { user, users })
+    }
+
+    /**
+     * 通知其他用户有人离开房间
+     */
+    onUserLeave(user, users) {
+        this.socket.broadcast.to(this.option.roomId).emit("userLeave", { user, users })
+        console.log(`用户[${user.userId}]离开房间`)
+    }
+
+    isLeader() {
+        return this.option.role == "leader"
+    }
+}

File diff suppressed because it is too large
+ 1154 - 0
package-lock.json


+ 19 - 0
package.json

@@ -0,0 +1,19 @@
+{
+  "name": "4dkankan_websocket",
+  "version": "1.0.0",
+  "description": "",
+  "main": "index.js",
+  "scripts": {
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "start": "nodemon ./daikan/rtc-live.js"
+  },
+  "author": "",
+  "license": "ISC",
+  "dependencies": {
+    "socket.io": "^4.1.2",
+    "uuid": "^8.3.2"
+  },
+  "devDependencies": {
+    "nodemon": "^2.0.9"
+  }
+}

File diff suppressed because it is too large
+ 9 - 0
test/js/socket.io.js


File diff suppressed because it is too large
+ 7 - 0
test/js/socket.io.min.js


+ 36 - 0
test/rtc-live.html

@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="UTF-8" />
+        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+        <title></title>
+        <script src="./js/socket.io.min.js"></script>
+    </head>
+    <body>
+        <script>
+            var socket = io("http://127.0.0.1:10010", {
+                transports:['websocket'],
+                path: "/im-rtc",
+                query: {
+                    userName:'张三'
+                },
+            })
+            socket.on('connect',function(){
+                setInterval(() => {
+                    socket.emit('muted',true,"213132")    
+                }, 3000);
+                
+            })
+
+            socket.on('join',function(data){
+               console.log(location.hash)
+               location.hash.substr()
+            })
+
+            socket.on('muted',function(){
+                console.log(arguments)
+            })
+            
+        </script>
+    </body>
+</html>