Browse Source

feat:增加小程序端连接,startCall、stopCall事件

徐志豪 5 năm trước cách đây
mục cha
commit
2658d173a8
2 tập tin đã thay đổi với 22 bổ sung52 xóa
  1. 0 44
      package.json
  2. 22 8
      server/index.js

+ 0 - 44
package.json

@@ -4,50 +4,6 @@
   "description": "",
   "main": "index.js",
   "dependencies": {
-    "_accepts@1.3.7@accepts": "^1.3.7",
-    "_any-promise@1.3.0@any-promise": "^1.3.0",
-    "_cache-content-type@1.0.1@cache-content-type": "^1.0.1",
-    "_co@4.6.0@co": "^4.6.0",
-    "_content-disposition@0.5.3@content-disposition": "^0.5.3",
-    "_content-type@1.0.4@content-type": "^1.0.4",
-    "_cookies@0.8.0@cookies": "^0.8.0",
-    "_debug@3.1.0@debug": "^3.1.0",
-    "_deep-equal@1.0.1@deep-equal": "^1.0.1",
-    "_delegates@1.0.0@delegates": "^1.0.0",
-    "_depd@1.1.2@depd": "^1.1.2",
-    "_depd@2.0.0@depd": "^2.0.0",
-    "_destroy@1.0.4@destroy": "^1.0.4",
-    "_ee-first@1.1.1@ee-first": "^1.1.1",
-    "_encodeurl@1.0.2@encodeurl": "^1.0.2",
-    "_error-inject@1.0.0@error-inject": "^1.0.0",
-    "_escape-html@1.0.3@escape-html": "^1.0.3",
-    "_fresh@0.5.2@fresh": "^0.5.2",
-    "_http-assert@1.4.1@http-assert": "^1.4.1",
-    "_http-errors@1.7.3@http-errors": "^1.7.3",
-    "_inherits@2.0.4@inherits": "^2.0.4",
-    "_is-generator-function@1.0.7@is-generator-function": "^1.0.7",
-    "_keygrip@1.1.0@keygrip": "^1.1.0",
-    "_koa-compose@3.2.1@koa-compose": "^3.2.1",
-    "_koa-compose@4.1.0@koa-compose": "^4.1.0",
-    "_koa-convert@1.2.0@koa-convert": "^1.2.0",
-    "_koa@2.11.0@koa": "^2.11.0",
-    "_media-typer@0.3.0@media-typer": "^0.3.0",
-    "_mime-db@1.43.0@mime-db": "^1.43.0",
-    "_mime-types@2.1.26@mime-types": "^2.1.26",
-    "_ms@2.0.0@ms": "^2.0.0",
-    "_negotiator@0.6.2@negotiator": "^0.6.2",
-    "_nodejs-websocket@1.7.2@nodejs-websocket": "^1.7.2",
-    "_on-finished@2.3.0@on-finished": "^2.3.0",
-    "_only@0.0.2@only": "^0.0.2",
-    "_parseurl@1.3.3@parseurl": "^1.3.3",
-    "_safe-buffer@5.1.2@safe-buffer": "^5.1.2",
-    "_setprototypeof@1.1.1@setprototypeof": "^1.1.1",
-    "_statuses@1.5.0@statuses": "^1.5.0",
-    "_toidentifier@1.0.0@toidentifier": "^1.0.0",
-    "_tsscmp@1.0.6@tsscmp": "^1.0.6",
-    "_type-is@1.6.18@type-is": "^1.6.18",
-    "_vary@1.1.2@vary": "^1.1.2",
-    "_ylru@1.2.1@ylru": "^1.2.1",
     "accepts": "^1.3.7",
     "any-promise": "^1.3.0",
     "cache-content-type": "^1.0.1",

+ 22 - 8
server/index.js

@@ -2,8 +2,8 @@ var server = require("http").createServer();
 module.exports = class WebSocketServer {
   constructor() {
     this._pageMap = new Map();
-    this._userIDs = new Map();
     this._roomIDS = new Map();
+    this._roomPerson = new Map()
 
     this._users = [];
 
@@ -24,16 +24,26 @@ module.exports = class WebSocketServer {
     this.io.on("connection", (socket) => {
       let user = socket.handshake.query
       this._users.push(user)
-      this._userIDs.set(user['agentId'], user['agentId']);
-
-      socket.join(user['roomId'], () => {
-        this._roomIDS.set(user['roomId'], user['roomId']);
-        this.io.to(user['roomId']).emit('vr_request', { userInfo: user });
+      const roomId = user['roomId']
+      socket.join(roomId, () => {
+        let roomsPerson = this._roomPerson.get(roomId) || []
+        roomsPerson.push(user)
+        this._roomPerson.set(roomId, roomsPerson)
+        // 只派发非小程序端的socket连接数及其user数据
+        this.io.to(roomId).emit('vr_request', { persons: roomsPerson.filter(item => !item.isClient) });
       });
 
+      socket.on('startCall', () => {
+        socket.broadcast.to(roomId).emit('answer', { user })
+      })
+
+      socket.on('stopCall', () => {
+        socket.broadcast.to(roomId).emit('putup', { user })
+      })
+
+
 
       socket.on("getJson", (data) => {
-        let roomId = this._roomIDS.get(data.roomId);
         if (roomId) {
           this.io.to(roomId).emit("action",  data.content.action);
           this.io.to(roomId).emit("vr_response", data);
@@ -42,7 +52,11 @@ module.exports = class WebSocketServer {
 
       console.log("WebSocket服务端建立完毕");
 
-      socket.on("disconnect", function (reason) {
+      socket.on("disconnect", (reason) => {
+        let roomsPerson = this._roomPerson.get(roomId) || []
+        // 断开连接的把roomsPerson中的user去掉
+        roomsPerson = roomsPerson.filter(item => item !== user)
+        this._roomPerson.set(roomId, roomsPerson)
         console.log("关闭连接", reason);
       });
       socket.on("error", function (reason) {