基于vue2.x实现的即时通讯程序仿微信聊天7开发socket真

2022-11-10  本文已影响0人  风中凌乱的男子
const socket = io.connect('wss://yyds.it98k.cn', {
    transports: ['websocket'],
})
image.png
this._vm.$socket.client.emit('login', {
     nickname: data.nickname,
     _id: data._id
 });
image.png
image.png
image.png
image.png
let obj = {
        come_id: this.$route.params.id, //接收方的id
        send_id: this.userName._id, //发送方的id
        type: 1, //消息类型 1 文字消息
        send_avatar: this.userName.avatar, //发送者的头像
        message: this.value, //发送的文字内容
        send_name: this.userName.nickname, //发送者的昵称
        date: new Date() //发送的当前时间
      }
this.$socket.client.emit('sayTo', obj)
image.png
但是还没有把记录存储到数据库,我们需要调用保存消息的接口
 SendMsg:'/api/send/msg',
// 存储聊天信息 
export function sendMsg(data) {
  return request({
    url: api.SendMsg,
    method: 'post',
    data,
    hideloading: true
  })
}
import {  sendMsg } from '@/api/user'
sendMsg({ toId: obj.come_id, type: 1, message: this.value })
send() {
      if (this.value.trim() == '') {
        this.$toast('请输入内容')
        return
      }
      let obj = {
        come_id: this.$route.params.id, //接收方的id
        send_id: this.userName._id, //发送方的id
        type: 1, //消息类型 1 文字消息
        send_avatar: this.userName.avatar, //发送者的头像
        message: this.value, //发送的文字内容
        send_name: this.userName.nickname, //发送者的昵称
        date: new Date() //发送的当前时间
      }
      this.$socket.client.emit('sayTo', obj)
      sendMsg({ toId: obj.come_id, type: 1, message: this.value })
      this.list.push(obj)
      this.value = ''
    }
上一篇 下一篇

猜你喜欢

热点阅读