提交 0de5899c 编写于 作者: yubinCloud's avatar yubinCloud

10-9 通过 ws 完成点赞通知功能,当用户点赞时,后端通过 ws 向所有客户端推送一条点赞通知

上级 b67b6a28
......@@ -18,10 +18,11 @@ import io.github.yubincloud.fairywiki.utils.CopyUtil;
import io.github.yubincloud.fairywiki.utils.RedisUtil;
import io.github.yubincloud.fairywiki.utils.RequestContext;
import io.github.yubincloud.fairywiki.utils.SnowFlake;
import org.apache.commons.lang3.ObjectUtils;
import io.github.yubincloud.fairywiki.websocket.WebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.List;
......@@ -46,6 +47,9 @@ public class DocService {
@Resource
private RedisUtil redisUtil;
@Resource
private WebSocketServer webSocketServer;
/**
* 获取全部 Doc
*/
......@@ -143,7 +147,9 @@ public class DocService {
} else {
throw new BusinessException(BusinessExceptionCode.VOTE_REPEAT);
}
docMapperCustom.increaseVoteCount(docId);
// 向 ws 推送消息
Doc docInDb = docMapper.selectByPrimaryKey(docId);
webSocketServer.sendInfo("【" + docInDb.getName() + "】被点赞!");
}
/**
......
......@@ -9,6 +9,8 @@
import {computed, defineComponent, onMounted} from "vue";
import store from "@/store";
import { Tool } from "@/util/tool";
import { notification } from 'ant-design-vue';
export default defineComponent({
name: 'the-footer',
......@@ -22,6 +24,10 @@ export default defineComponent({
};
const onMessage = (event: any) => {
console.log('WebSocket收到消息:', event.data);
notification['info']({
message: '收到消息',
description: event.data,
});
};
const onError = () => {
console.log('WebSocket连接错误,状态码:', websocket.readyState)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册