|
|
@@ -2,10 +2,15 @@ package com.nb.im.ws.filter;
|
|
|
|
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import com.nb.common.websocket.msg.MessageResponse;
|
|
|
import com.nb.common.websocket.msg.MessagingRequest;
|
|
|
import com.nb.common.websocket.PubResponse;
|
|
|
import com.nb.common.websocket.filter.PubMsgFilter;
|
|
|
+import com.nb.im.enums.ImMsgType;
|
|
|
import com.nb.im.ws.PubMsgInfo;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.tio.core.ChannelContext;
|
|
|
import org.tio.core.Tio;
|
|
|
@@ -21,25 +26,44 @@ import org.tio.websocket.common.WsResponse;
|
|
|
*/
|
|
|
@Component
|
|
|
public class MsgFormatFilter implements PubMsgFilter {
|
|
|
+ @Autowired
|
|
|
+ ObjectMapper objectMapper;
|
|
|
@Override
|
|
|
public boolean doFilter(ChannelContext channelContext, MessagingRequest source) {
|
|
|
String id = source.getId();
|
|
|
if("im".equalsIgnoreCase(id)){
|
|
|
if(ObjectUtil.isEmpty(source.getPayload())){
|
|
|
- Tio.send(channelContext,WsResponse.fromText(JSONUtil.toJsonStr(PubResponse.fail("","发布消息不可为空")),WsPacket.CHARSET_NAME));
|
|
|
+ for (String param : source.getParams()) {
|
|
|
+ Tio.send(channelContext, WsResponse.fromText(JSONUtil.toJsonStr(
|
|
|
+ MessageResponse.of("im","im-result",
|
|
|
+ param,
|
|
|
+ PubResponse.fail(source.getKey(),"发布消息不能为空"),
|
|
|
+ source.getKey())
|
|
|
+
|
|
|
+ ), WsPacket.CHARSET_NAME));
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
- PubMsgInfo pubMsgInfo = JSONUtil.toBean(JSONUtil.toJsonStr(source.getPayload()), PubMsgInfo.class);
|
|
|
+ PubMsgInfo pubMsgInfo =null;
|
|
|
try {
|
|
|
+ pubMsgInfo = objectMapper.readerFor(PubMsgInfo.class).readValue(String.valueOf(source.getPayload()));
|
|
|
pubMsgInfo.validate();
|
|
|
- }catch (RuntimeException e){
|
|
|
- Tio.send(channelContext,WsResponse.fromText(JSONUtil.toJsonStr(PubResponse.fail(pubMsgInfo.getKey(),e.getMessage())),WsPacket.CHARSET_NAME));
|
|
|
+ }catch (Exception e){
|
|
|
+ Tio.send(channelContext,WsResponse.fromText(JSONUtil.toJsonStr(PubResponse.fail(pubMsgInfo!=null?pubMsgInfo.getKey():"",e.getMessage())),WsPacket.CHARSET_NAME));
|
|
|
+ for (String param : source.getParams()) {
|
|
|
+ Tio.send(channelContext, WsResponse.fromText(JSONUtil.toJsonStr(
|
|
|
+ MessageResponse.of("im","im-result",
|
|
|
+ param,
|
|
|
+ PubResponse.fail(pubMsgInfo!=null?pubMsgInfo.getKey():"",e.getMessage()),
|
|
|
+ source.getKey())
|
|
|
+
|
|
|
+ ), WsPacket.CHARSET_NAME));
|
|
|
+ }
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
//敏感词过滤
|
|
|
}
|