Springboot+Vue实现在线聊天室项目
该聊天室为大二上学期计算机网络大作业,并且是本人第一次使用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的方式发送给双方