|
@@ -1,10 +1,12 @@
|
|
|
var server = require("http").createServer();
|
|
|
module.exports = class WebSocketServer {
|
|
|
constructor() {
|
|
|
- this.connMap = new Map();
|
|
|
this._pageMap = new Map();
|
|
|
- this._count = 0;
|
|
|
+ this._userIDs = new Map();
|
|
|
+ this._roomIDS = new Map();
|
|
|
+
|
|
|
this._users = [];
|
|
|
+
|
|
|
}
|
|
|
|
|
|
create() {
|
|
@@ -20,35 +22,27 @@ module.exports = class WebSocketServer {
|
|
|
server.listen(3000, { origins: "*" });
|
|
|
|
|
|
this.io.on("connection", (socket) => {
|
|
|
- this._count = this._count >= this._users.length ? 0 : this._count;
|
|
|
let user = socket.handshake.query
|
|
|
this._users.push(user)
|
|
|
- let userId = user.role === 'customer' ? 'customerId' : 'agentId'
|
|
|
- let friendId = user.role !== 'customer' ? 'customerId' : 'agentId'
|
|
|
- this.connMap.set(userId, socket);
|
|
|
- socket.emit('vr_request', { userInfo: user });
|
|
|
- this._count++;
|
|
|
+ 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 });
|
|
|
+ });
|
|
|
|
|
|
- socket.on("getJson", (data) => {
|
|
|
- console.log("收到的信息为:" + data);
|
|
|
- let msg = data;
|
|
|
- let connTemp = this.connMap.get(friendId);
|
|
|
- let action = msg.content.action
|
|
|
-
|
|
|
- socket.emit("action", action,()=>{
|
|
|
- console.log(action)
|
|
|
- });
|
|
|
|
|
|
- !!connTemp && this.sendText(connTemp, msg, (msgSendFinally) => {
|
|
|
- console.log(msgSendFinally)
|
|
|
- });
|
|
|
+ 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);
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
console.log("WebSocket服务端建立完毕");
|
|
|
|
|
|
socket.on("disconnect", function (reason) {
|
|
|
- // this.connMap[userId] && (delete this.connMap[userId])
|
|
|
- // console.log(this.connMap)
|
|
|
console.log("关闭连接", reason);
|
|
|
});
|
|
|
socket.on("error", function (reason) {
|
|
@@ -64,16 +58,4 @@ module.exports = class WebSocketServer {
|
|
|
this.server.disconnect(true);
|
|
|
}
|
|
|
|
|
|
- sendText(socket, msg, cbOk) {
|
|
|
- const msgSendFinally = msg;
|
|
|
- let action = msg.content.action
|
|
|
-
|
|
|
- socket.emit("action", action,()=>{
|
|
|
- console.log(action)
|
|
|
- });
|
|
|
-
|
|
|
- socket.emit("vr_response", msgSendFinally,()=>{
|
|
|
- cbOk && cbOk(msgSendFinally);
|
|
|
- });
|
|
|
- }
|
|
|
};
|