|
|
@@ -1,14 +1,15 @@
|
|
|
package com.coffee.common.config.websocket;
|
|
|
|
|
|
-import cn.dev33.satoken.session.SaSession;
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
-import cn.hutool.cron.Scheduler;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
-import com.coffee.common.redis.RedisUtils;
|
|
|
+import com.coffee.common.Constants;
|
|
|
+import com.coffee.common.bo.LoginUser;
|
|
|
+import com.coffee.common.config.websocket.handler.WsHandler;
|
|
|
import com.coffee.common.result.R;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.tio.core.ChannelContext;
|
|
|
import org.tio.core.Tio;
|
|
|
@@ -17,24 +18,16 @@ import org.tio.http.common.HttpResponse;
|
|
|
import org.tio.websocket.common.WsRequest;
|
|
|
import org.tio.websocket.common.WsResponse;
|
|
|
import org.tio.websocket.server.handler.IWsMsgHandler;
|
|
|
-import reactor.core.scheduler.Schedulers;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
@Component
|
|
|
@Slf4j
|
|
|
+@AllArgsConstructor
|
|
|
public class DefaultWebSocketMsgHandler implements IWsMsgHandler {
|
|
|
- RedisUtils redisUtils;
|
|
|
- RedisTemplate redisTemplate;
|
|
|
+ private final List<WsHandler> messageHandlers;
|
|
|
@Override
|
|
|
- public HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
|
|
|
-
|
|
|
-// if(StrUtil.isEmpty(authorization)){
|
|
|
-// return httpResponse.setBody(R.fail());
|
|
|
-// }
|
|
|
-// httpResponse.setBody("123".getBytes());
|
|
|
-// throw new CustomException("授权失败");
|
|
|
-// Tio.send(channelContext,WsResponse.fromText("授权失败","utf-8"));
|
|
|
-
|
|
|
-
|
|
|
+ public HttpResponse handshake(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) {
|
|
|
return httpResponse;
|
|
|
}
|
|
|
|
|
|
@@ -51,40 +44,75 @@ public class DefaultWebSocketMsgHandler implements IWsMsgHandler {
|
|
|
channelContext.setClosed(true);
|
|
|
return;
|
|
|
}
|
|
|
- SaSession tokenSessionByToken = StpUtil.getTokenSessionByToken(authorization);
|
|
|
- if(null==tokenSessionByToken){
|
|
|
+ Object result = StpUtil.getTokenSessionByToken(authorization).get(Constants.LOGIN_USER_KEY);
|
|
|
+ if(null==result){
|
|
|
Tio.send(channelContext,WsResponse.fromText(JSONUtil.toJsonStr(R.fail("授权失败")),"utf-8"));
|
|
|
if(log.isDebugEnabled()){
|
|
|
log.debug("Authorization:{},鉴权失败",authorization);
|
|
|
}
|
|
|
- Tio.unbindToken(channelContext);
|
|
|
+ unbind(channelContext);
|
|
|
Thread.sleep(50);
|
|
|
channelContext.setClosed(true);
|
|
|
return;
|
|
|
}
|
|
|
- Tio.bindToken(channelContext, JSONUtil.toJsonStr(tokenSessionByToken));
|
|
|
+ Tio.bindToken(channelContext, JSONUtil.toJsonStr(authorization));
|
|
|
+ LoginUser loginUser = (LoginUser)result;
|
|
|
+ channelContext.set(Constants.LOGIN_USER_KEY,loginUser);
|
|
|
Tio.send(channelContext,WsResponse.fromText(JSONUtil.toJsonStr(R.success("连接成功")),"utf-8"));
|
|
|
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
|
|
|
+ public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext){
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
|
|
|
+ public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) {
|
|
|
+ messageHandlers.forEach(wsHandler -> wsHandler.close(channelContext));
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public Object onText(WsRequest wsRequest, String message, ChannelContext channelContext) throws Exception {
|
|
|
- System.out.println("接收到文本消息:"+message);
|
|
|
+ public Object onText(WsRequest wsRequest, String message, ChannelContext channelContext) {
|
|
|
if (StrUtil.isEmpty(message)) {
|
|
|
- Tio.unbindBsId(channelContext);
|
|
|
+ unbind(channelContext);
|
|
|
channelContext.setClosed(true);
|
|
|
return null;
|
|
|
}
|
|
|
+ if(log.isDebugEnabled()){
|
|
|
+ log.debug("websocket 接收到消息,message:{},token:{},userId:{}",message,channelContext.getToken(),JSONUtil.toJsonStr(channelContext.get(Constants.LOGIN_USER_KEY)));
|
|
|
+ }
|
|
|
+ //心跳请求 todo
|
|
|
+ if("ping".equals(message.trim().toLowerCase())){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ MessagingRequest messagingRequest = JSONUtil.toBean(message, MessagingRequest.class);
|
|
|
+ messagingRequest.validate();
|
|
|
+ List<WsHandler> collect = messageHandlers
|
|
|
+ .parallelStream()
|
|
|
+ .filter(handler -> messagingRequest.getId().equals(handler.getId()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ //传入格式错误,不支持该订阅id
|
|
|
+ if(CollectionUtil.isEmpty(collect)){
|
|
|
+ unbind(channelContext);
|
|
|
+ channelContext.setClosed(true);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ collect.forEach(handler->handler.onMessage(messagingRequest,channelContext));
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ log.warn("websocket 接收到异常请求,token:{},message:{},userId:{}",channelContext.getToken(),message,JSONUtil.toJsonStr(channelContext.get(Constants.LOGIN_USER_KEY)));
|
|
|
+ unbind(channelContext);
|
|
|
+ channelContext.setClosed(true);
|
|
|
+ }
|
|
|
+
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ private void unbind(ChannelContext channelContext){
|
|
|
+ Tio.unbindToken(channelContext);
|
|
|
+ Tio.unbindUser(channelContext);
|
|
|
+ }
|
|
|
}
|