Springboot+Vue实现在线聊天室项目-推送好友请求消息接口

本文介绍了用Springboot和Vue实现在线聊天室项目,这是大二上计算机网络大作业,也是首次用Vue实现前后端分离。详细阐述了发送和接收好友请求的流程,包括Controller配置、Service业务处理,如获取双方id、发送WebSocket消息等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Springboot+Vue实现在线聊天室项目

该聊天室为大二上学期计算机网络大作业,并且是本人第一次使用vue实现前后端分离的项目,前端架构尚未熟悉可能会出现一些不妥之处,还请大佬们指出。(本文章写于项目整体完成上线之后,所以一些细节并未写出)

# Springboot+Vue实现在线聊天室项目目录

发送好友请求

Controller配置

    @ResponseBody
    @RequestMapping({"/sendFriendRequest"})
    public Result sendFriendRequest(int toUid) {
        return this.userService.sendFriendRequest(toUid);
    }

Service

public Result sendFriendRequest(int toUid) {
        Result result = new Result();
        int fromUid = getUserIdCurrent();
        if (this.userDao.getInfor(fromUid, toUid) != null) {
            String msg = this.userDao.getInfor(fromUid, toUid).getFinalMsg();
            if (msg.equals(""))
                msg = "尚未响应";
            result.setMsg("好友申请已存在,对方"+ msg);
                    result.setStatus(200);
            return result;
        }
        try {
            this.userDao.insertInfor(fromUid, toUid);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(403);
            result.setMsg("发送失败,出现异常");
        }
        result.setStatus(200);
        result.setMsg("发送成功");
        WebSocketMessage message = new WebSocketMessage();
        message.setTimeStamp(System.currentTimeMillis());
        message.setContent("请求加为好友");
        message.setUid(fromUid+"");
        message.setName(userDao.getMyName(fromUid));
        messagingTemplate.convertAndSend("/topic/" + toUid,message);
        return result;
    }

发送方id通过SpringSecurity获取,接收方id通过请求params查询获取,请求后通过messagingTemplate向接收方发送webSocket消息,以便接收方接收好友请求

接收好友请求

Controller配置 /getAllInfor接口获取所有好友请求

    @ResponseBody
    @RequestMapping({"/getAllInfor"})
    public Result getAllInfor() {
        return this.userService.getAllInfor();
    }

/resInfor接口响应

    @ResponseBody
    @RequestMapping({"/resInfor"})
    public Result resInfor(int fromUid, String finalMsg) {
        return this.userService.resInfor(fromUid, finalMsg);
    }

Service

public Result resInfor(int fromUid, String finalMsg) {
        Result result = new Result();
        int toUid = getUserIdCurrent();
        try {
            this.userDao.updateInfor(fromUid, toUid, finalMsg);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(403);
            result.setMsg("出现异常");
            return result;
        }
        result.setStatus(200);
        result.setMsg("成功");
        int id1 = (fromUid > toUid) ? toUid : fromUid;
        int id2 = (fromUid > toUid) ? fromUid : toUid;
        String name = "|" + this.userDao.getMyName(id1) + "|" + this.userDao.getMyName(id2) + "|";
        if (this.userDao.getSmallRoom(name) != null) {
            result.setMsg("成功,房间已存在");
            return result;
        }
        this.userDao.addFriend(fromUid, toUid);
        this.userDao.addFriend(toUid, fromUid);
        String img = "|" + this.userDao.getMyImg(id1) + "|" + this.userDao.getMyImg(id2) + "|";
        String uids = "|" + id1 + "|" + id2 + "|";
        System.out.println("img:" + img + "uids:" + uids + "name:" + name);
        this.userDao.insertSmallRoom(name, uids, img, new Date());
        int roomId = userDao.getRoomIdByName(name);
        System.out.println("创建房间成功,发送第一条初始消息" + roomId);
        userDao.insertMsgByRoomId(roomId,toUid,new Date(),"我同意了你的好友请求",userDao.getMyImg(toUid),userDao.getMyName(toUid));
        WebSocketMessage message = new WebSocketMessage();
        message.setTimeStamp(System.currentTimeMillis());
        message.setContent("我同意了你的好友请求");
        message.setImg(userDao.getMyImg(toUid));
        message.setName(userDao.getMyName(toUid));
        message.setRoomId(roomId+"");
        message.setUid(toUid+"");
        messagingTemplate.convertAndSend("/topic/" + toUid,message);
        messagingTemplate.convertAndSend("/topic/" + fromUid,message);
        return result;
    }

业务层先后经历:判断双方的聊天室房间是否存在 -> 创建聊天室? -> 修改infor消息 -> 在user表中添加好友 -> 在这个聊天室方建中由接收好友请求方发送一条"我同意了你的好友请求" 的消息 -> 将这个消息以webSocket的方式发送给双方

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值