From a26bd4629e348908f4066f54ebbcec4f6236c19e Mon Sep 17 00:00:00 2001 From: cleverqin <705597001@qq.com> Date: Thu, 11 Jun 2020 10:43:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=94=A8=E6=88=B7=E6=98=AF?= =?UTF-8?q?=E5=90=A6=E5=90=88=E6=B3=95=E7=9A=84=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index a8ad417..e89d91d 100644 --- a/app.js +++ b/app.js @@ -39,6 +39,21 @@ let kit = { } else { return "pc"; } + }, + addUser(user){ + if(!user.id){ + return + } + let id=user.id; + let index=-1; + users.forEach((item,i)=>{ + if(item.id==id){ + index=i; + } + }) + if(index==-1){ + users.push(user); + } } } //设置静态资源 @@ -112,13 +127,17 @@ io.sockets.on('connection',(socket)=>{ //判断用户重新连接 if(socket.handshake.query.User){ let user=JSON.parse(socket.handshake.query.User); - socket.user = user; - user.roomId = socket.id; - user.address = socket.handshake.address.replace(/::ffff:/,""); - console.log("用户<"+user.name+">重新连接成功!") - socket.emit('loginSuccess', user, users); - users.push(user) - socket.broadcast.emit('system', user, 'join'); + if(user.id){ + socket.user = user; + user.roomId = socket.id; + user.address = socket.handshake.address.replace(/::ffff:/,""); + console.log("用户<"+user.name+">重新连接成功!") + socket.emit('loginSuccess', user, users); + kit.addUser(user) + socket.broadcast.emit('system', user, 'join'); + }else { + console.log("非法链接用户") + } } }); //启动服务器