一、前言                                                                                 

 

 

二、主要内容                                                                          

1、分析实现的逻辑

      第一步:用户登录成功之后需要找到目的对象发起私聊。

       第二步:数据双向绑定监听输入内容

       第三步:点击发送按钮的时候请求后端接口。

       第四步:后端建立接口完毕后返回前端聊天数据

       第五步:前端触发后端定义的socket.io中接收消息的逻辑

       第六步:接收到后台回复的数据,然后进行数据渲染。

 

2、分步骤实现

      2.1用户登录成功之后为每个用户分配一个socket.io

      (1)登录成功之后触发后台的

 

asyn Login(){

...
....
....省略

//登录成功后跳转到个人中心界面,触发后台定义的方法为每一个登录成功的用户分配一个socketid

socket.emit('login', user.username);

}

 

      (2)后台新建一个sechma专门用户存放每个用户的socketid

let express = require('express');

let mongoose = require('mongoose');

module.exports = new mongoose.Schema({
    socketid:String,
    username:String
});

       (3)调用封装的保存用户socketid方法

//=====================封装

let Idtoid = require('../Models/Idtoid')

module.exports = class socketHandler{
    static async savaUserSocketId(username, socketId){
        //保存用户的id和socketid
        await Idtoid.findOne({
            username: username
        }).then((rs)=>{
            if(!rs){
                new Idtoid({
                    username: username,
                    socketid: socketId
                }).save().then(()=>{

                })
            }else {
                Idtoid.update({
                    username: username
                }, {
                    socketid: socketId
                }).then(()=>{

                })
            }
        })
    }
}


//=========================调用
     socket.on('login', (username)=>{
           console.log('有用户登录')
           socketHandler.savaUserSocketId(username, socketId)
       })

 

     2.2数据双向绑定触发消息发送的方法

    (1)前端data中定义content实现数据双向绑定,(这里需要再调用请求用户信息的方法,请求到当前聊天的用户信息)

data(){
            return{
                chatId: this.$route.query.chatId,
                chatName: this.$route.query.chatName,
                content:'',//发送的对话内容
                chatUserInfo:{}, //请求到的聊天的用户信息
                chatdataList:[] //当前聊天的信息,自己发的信息存到一个数组中
            }

        },

 

   (2)数据双向绑定完成触发发送

//发送消息
            sendContent(){
                              //1.先判断当前用户是否登录
                if(!this.userInfo._id){
                    Toast("请先登录!")
                    return;

                }
                              //2.校验输入的内容是否为空
                if(this.content==""){
                    return
                }
                             //3.请求后端接口
                this.$axios.post('/api/chat/chatwith', {
                    chatWithId: this.chatUserInfo._id,//聊天对象
                    user_id: this.userInfo._id,//当前登录的用户
                    content:this.content
                }).then((res)=>{
                    console.log(res)
                    this.chatdataList.push({//保存发送到的和接收到的消息
                        user_id:{
                            avater: this.userInfo.avater
                        },

                        chatWith:{
                            _id: this.chatId
                        },

                        addTime: Date.now(),
                        content: this.content
                    })


                    //将当前的发送的信息同步更新到vuex中
                    this.UPDATE_CHATLIST({
                        _id: this.chatUserInfo._id,
                        from_user: this.chatName,
                        message: this.content,
                        time: Date.now(),
                        me:true,//判别是不是自己发送
                        avater:this.chatUserInfo.avater
                    })

                    console.log(this.chatUserInfo._id)
                    console.log(this.chatName)
                    console.log(this.content)
                    console.log(Date.now())

                    console.log(this.chatUserInfo._id)
                    //发给对方的数据
                    let data = {
                        from_user: this.userInfo.username, 
                        to_user: this.chatName,
                        message: this.content,
                        time: Date.now(),
                        avater: this.userInfo.avater,
                        _id: this.userInfo._id
                    }

                    socket.emit('chat', data)
                    this.content=''
                    
                })
            },

  (3)后端实现逻辑

     思路:发送消息过来的时候先判断是否两个聊天用户之间已经建立好了联系,如果已经建立好了联系只需要更新聊天数据,如果还没有建立好联系先建立联系。

router.post('/chatwith', (req, res) => {
    let chatWith = req.body.chatWithId; //获取到前端传来的聊天用户
    let user_id = req.body.user_id;  //获取到当前登录的用户
    let content = req.body.content; //获取到输入的聊天内容

   //新建一个聊天内容对象
    new ChatContent({
        chatWith: chatWith, //聊天用户
        user_id: user_id,
        content: content
    }).save().then((newContent) => {
       
       //查找两个用户之间是否已经建立好了联系
        ChatRelation.findOne({
            $or:[{
                userA:user_id,
                userB:chatWith
            },{
                userB:user_id,
                userA:chatWith
            }]
        }).then((rs)=>{
           //如果已经建立好了联系,只需要更新聊天数组
            if (rs){
                let chatContent = rs.chatContent;
                chatContent.unshift(newContent._id);
                ChatRelation.update({
                    _id:rs.id
                },{
                    chatContent:chatContent
                }).then(()=>{
                    res.json({
                        code:0,
                        data:newContent
                    })
                })
            }else {
                 //没有建立好联系就新建立联系
                new ChatRelation({
                    userA:user_id,
                    userB:chatWith,
                    chatContent:[newContent._id]
                }).save().then(()=>{
                    res.json({
                        code:0,
                        data:newContent
                    })
                })
            }
        })
    })

});

这里涉及到的两个sechma对象:

chatContent.js:

let express = require('express')

let mongoose = require('mongoose')

module.exports = new mongoose.Schema({
    user_id:{//当前的用户id
        type:mongoose.Schema.Types.ObjectId,
        ref:'User'
    },

    chatWith:{//私信的用户
        type:mongoose.Schema.Types.ObjectId,
        ref:'User'

    },
    addTime:{//发送内容的时间
        type:Date,
        default: Date.now
    },

    content:{
        type: String
    },
    unread:{//是否默认未读
        type: Boolean,
        default: true
    }
})
chatContent.js

相关文章:

  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2021-06-03
  • 2022-12-23
  • 2022-12-23
  • 2022-02-02
猜你喜欢
  • 2022-12-23
  • 2021-12-08
  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-30
相关资源
相似解决方案