def connect(sid, data): #服务器接收登录消息,登录data = {username:xxxxx,password:xxxxx(未加密)} print(data) sql = 'select username,password,imgheader,sid,id from user where username="******";' % data[ 'username'] sqlres = fnSql(sql) if len(sqlres) > 0: #返回来的数据是元祖,((username,password),) md5password = hashlib.md5(bytes(M_md5num.encode('utf-8'))) # 双重加密的加密参数 md5password.update(bytes(data['password'].encode('utf-8'))) data['password'] = md5password.hexdigest() if sqlres[0][1] == data['password']: sio.emit('login', list(sqlres[0]), room=sid) sql = 'update user set islogin=1,sid="%s" where username = "******";' % ( sid, data['username']) fnSql(sql) #查询未读的消息发送给用户 #sqlmessage = 'select * from message where destid = "%s" and not isRead="1"'% sqlres[0][4]; # sqlmessage1 = 'select username,imgheader from user where id = "%s"'% sqlmessage[0][1]; sqlweidu = 'select u.username,u.imgheader,m.* from user as u inner join message as m on u.id=m.sendid where m.destid= "%s" and not isRead="1";' % sqlres[ 0][4] #print(sqlmessage) resMessage = fnSql(sqlweidu) #print(resMessage) sio.emit('loginweidu', list(resMessage), room=sid) else: sio.emit('login', 'fail', room=sid) else: sio.emit('login', 'fail', room=sid) #判断是否账号和密码是否正确 #如果密码未加密,那么就对密码加密 #通过数据库查询语句,查找username的数据库密码 # if 加密密码== 数据库密码: #登录成功 #数据库里用户信息里面他的是否在线字段要修改为真,sid的值要更新 # else: #登录失败 #密码或者用户名不正确,可以再次尝试,或者是重新注册。 #没有这个用户,可以尝试注册 #密码不正确 print('login') print(data)
def connect(sid, data): print('sendMessage') print(data) #查找数据库自己的id sql0 = 'select * from user where sid = "%s"' % sid res0 = fnSql(sql0) id = res0[0][0] data['sendid'] = id #查询目标ID是否已经上线 sql1 = 'select islogin,sid from user where id="%s"' % data['destid'] res1 = fnSql(sql1) if res1[0][0] == '1': sql2 = 'insert into message (sendid,destid,type,content,time,isRead) values ("%s","%s","%s","%s","%s","%s")' % ( id, data['destid'], data['type'], data['content'], data['time'], 1) fnSql(sql2) # 查询发送方的信息 sql3 = 'select * from user where id = "%s"' % id print(sql3) data['info'] = fnSql(sql3)[0] sio.emit('sendMessage', data, room=res1[0][1]) else: #将未读消息插入数据库,下次用户登陆时,接受的消息中未读的消息 sql2 = 'insert into message (sendid,destid,type,content,time,isRead) values ("%s","%s","%s","%s","%s","%s")' % ( id, data['destid'], data['type'], data['content'], data['time'], 0) fnSql(sql2)
def connect(sid, data): print('updateFriends') print(data) #通过sid查找用户id sql0 = 'select * from user where sid = "%s"' % sid res0 = fnSql(sql0) #通过用户id,查找好友 sql1 = 'SELECT * from user WHERE id in (SELECT user2id from friendrelation WHERE user1id="%s")' % res0[ 0][0] res1 = fnSql(sql1) #通过用户id,查找相关性 sql2 = 'SELECT * from friendrelation WHERE user1id = "%s";' % res0[0][0] res2 = fnSql(sql2) #通过用户id,查找好友组 sql3 = 'select * from friend_group where userid = "%s"' % res0[0][0] res3 = fnSql(sql3) data = {'res1': list(res1), 'res2': list(res2), 'res3': list(res3)} sio.emit('updateFriends', data, room=sid)
def connect(sid, data): # print('register') #用户发送注册信息,data={ username:xxx,password:xxxx} sql = 'select * from user where username="******";' % data['username'] sqlres = fnSql(sql) if len(sqlres) > 0: print('注册失败') print(sqlres) sio.emit('login', 'fail', room=sid) else: md5password = hashlib.md5(bytes(M_md5num.encode('utf-8'))) #双重加密的加密参数 md5password.update(bytes(data['password'].encode('utf-8'))) data['password'] = md5password.hexdigest() sql = 'insert into user (username,password,imgheader) values ("%s","%s","%s")' % ( data['username'], data['password'], M_userImgheader) fnSql(sql) print('插入成功') sio.emit('register', 'success', room=sid)
def connect(sid, data): print('addfriend') print(data) #通过用户名查找是否有这个用户 sql = 'select * from user where username="******";' % data['friendname'] res = fnSql(sql)[0] #查找是否已经是好友 #查找自己的信息 sql1 = 'select * from user where sid = "%s"' % sid selfid = fnSql(sql1)[0][0] #查找关系表里面是否自己跟好友已经建立起联系 sql2 = 'select * from friendrelation where user1id="%s" and user2id="%s"' % ( selfid, res[0]) print(sql2) res2 = fnSql(sql2) print(res) print(res2) if len(res) > 0 and len(res2) == 0: #判断好友组表里这个userid有没有这个组 sql4 = 'select * from friend_group where userid="%s" and user_group = "%s"; ' % ( selfid, data['groupname']) #插入数据到好友组里面 res4 = fnSql(sql4) if len(res4) == 0: sql3 = 'insert into friend_group (userid,user_group) values (%s,"%s");' % ( selfid, data['groupname']) #再给朋友和自己建立起联系 sql5 = 'insert into friend_group (userid,user_group) values (%s,"%s");' % ( res[0], data['groupname']) print(sql3) print(sql5) res3 = fnSql(sql3) res5 = fnSql(sql5) #建立起真正的好友关系 #先查组ID sql6 = 'select * from friend_group where userid = "%s" and user_group = "%s"; ' % ( selfid, data['groupname']) sql7 = 'select * from friend_group where userid = "%s" and user_group = "%s"; ' % ( res[0], data['groupname']) groupid1 = fnSql(sql6)[0][0] groupid2 = fnSql(sql7)[0][0] #真正给表插入数据 sql8 = 'insert into friendrelation (user1id,user2id,friend_group_id) values ("%s","%s","%s")' % ( selfid, res[0], groupid1) sql9 = 'insert into friendrelation (user1id,user2id,friend_group_id) values ("%s","%s","%s")' % ( res[0], selfid, groupid2) fnSql(sql8) fnSql(sql9) #查询两个用户的好友数据让其更新 sio.emit('updateFriends', 'success', room=sid)
def disconnect(sid): print('disconnect ', sid) sql = 'update user set islogin=0,sid="0" where sid = "%s";' % (sid) fnSql(sql)