feat: 完善相关功能

This commit is contained in:
ivmiku
2024-09-13 22:13:21 +08:00
parent 4a39ca48ee
commit 262103e034
18 changed files with 410 additions and 10 deletions

View File

@@ -14,14 +14,29 @@ public class RabbitMqConfig {
return new Queue("queue1",true);
}
@Bean
public Queue queue2() {
return new Queue("queue2",true);
}
@Bean
public FanoutExchange exchange1() {
return new FanoutExchange("exchange1",true, false);
}
@Bean
public FanoutExchange exchange2() {
return new FanoutExchange("exchange2",true, false);
}
@Bean
public Binding binding1() {
return BindingBuilder.bind(queue1()).to(exchange1());
}
@Bean
public Binding binding2() {
return BindingBuilder.bind(queue2()).to(exchange2());
}
}

View File

@@ -2,7 +2,9 @@ package com.ivmiku.tutorial.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ivmiku.tutorial.entity.Message;
import com.ivmiku.tutorial.response.AtNotifier;
import com.ivmiku.tutorial.service.MessageService;
import com.ivmiku.tutorial.service.RelationService;
import com.ivmiku.tutorial.utils.DateUtil;
@@ -84,7 +86,15 @@ public class WebSocketServer implements ApplicationContextAware {
sessionMap.put(userId, session);
List<Message> unreadList = messageService.getUnreadMsg(userId);
for(Message msg : unreadList) {
session.getBasicRemote().sendText(JSON.toJSONString(msg));
JSONObject object = JSONObject.from(msg);
object.put("type", 1);
session.getBasicRemote().sendText(object.toJSONString());
}
List<AtNotifier> atList = messageService.getAtList(userId);
for (AtNotifier notifier : atList) {
JSONObject object = JSONObject.from(notifier);
object.put("type", 2);
session.getBasicRemote().sendText(object.toJSONString());
}
}
@@ -132,8 +142,25 @@ public class WebSocketServer implements ApplicationContextAware {
public void sendMsg(String message) throws IOException {
Message msg = JSON.parseObject(message, Message.class);
if (sessionMap.containsKey(msg.getToId())) {
sessionMap.get(msg.getToId()).getBasicRemote().sendText(JSON.toJSONString(msg));
JSONObject jsonObject = JSONObject.from(msg);
jsonObject.put("type", 1);
sessionMap.get(msg.getToId()).getBasicRemote().sendText(jsonObject.toJSONString());
messageService.deleteMessage(msg);
}
}
@RabbitHandler
@RabbitListener(bindings = @QueueBinding(
value = @Queue(),
exchange = @Exchange(value = "exchange2",type = ExchangeTypes.FANOUT)
))
public void sendAt(String message) throws IOException {
AtNotifier notifier = JSON.parseObject(message, AtNotifier.class);
if (sessionMap.containsKey(notifier.getToId())) {
JSONObject jsonObject = JSONObject.from(notifier);
jsonObject.put("type", 2);
sessionMap.get(notifier.getToId()).getBasicRemote().sendText(jsonObject.toJSONString());
messageService.deleteNotifier(notifier);
}
}
}

View File

@@ -6,6 +6,7 @@ import com.ivmiku.tutorial.entity.ChatId;
import com.ivmiku.tutorial.entity.Message;
import com.ivmiku.tutorial.mapper.ChatIdMapper;
import com.ivmiku.tutorial.mapper.MessageMapper;
import com.ivmiku.tutorial.response.AtNotifier;
import com.ivmiku.tutorial.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
@@ -245,4 +246,12 @@ public class MessageService {
public void deleteMessage(Message message) {
redisUtil.deleteFromList(message);
}
public void deleteNotifier(AtNotifier notifier) {
redisUtil.deleteFromList(notifier);
}
public List<AtNotifier> getAtList(String userId) {
return redisUtil.listGetN("at:" + userId, 0, -1);
}
}

View File

@@ -2,6 +2,7 @@ package com.ivmiku.tutorial.utils;
import com.alibaba.fastjson2.JSON;
import com.ivmiku.tutorial.entity.Message;
import com.ivmiku.tutorial.response.AtNotifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@@ -127,4 +128,24 @@ public class RedisUtil {
String id = message.getToId();
redisTemplate.opsForList().remove("unread:" + id, 0, message);
}
public void listAddN(String key, Object value) {
redisTemplate.opsForList().leftPush(key, value);
}
public List<AtNotifier> listGetN(String key, int s, int e) {
List<Object> list = redisTemplate.opsForList().range(key, s, e);
List<AtNotifier> result = new ArrayList<>();
if (list != null) {
for (Object json : list) {
result.add(JSON.parseObject(JSON.toJSONString(json), AtNotifier.class));
}
}
return result;
}
public void deleteFromList(AtNotifier message) {
String id = message.getToId();
redisTemplate.opsForList().remove("unread:" + id, 0, message);
}
}