|
|
@@ -0,0 +1,90 @@
|
|
|
+package com.coffee.common.config.websocket;
|
|
|
+
|
|
|
+import cn.dev33.satoken.session.SaSession;
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
+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.result.R;
|
|
|
+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;
|
|
|
+import org.tio.http.common.HttpRequest;
|
|
|
+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;
|
|
|
+
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class DefaultWebSocketMsgHandler implements IWsMsgHandler {
|
|
|
+ RedisUtils redisUtils;
|
|
|
+ RedisTemplate redisTemplate;
|
|
|
+ @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"));
|
|
|
+
|
|
|
+
|
|
|
+ return httpResponse;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onAfterHandshaked(HttpRequest httpRequest, HttpResponse httpResponse, ChannelContext channelContext) throws Exception {
|
|
|
+ String authorization = httpRequest.getParam("Authorization");
|
|
|
+ if(log.isDebugEnabled()){
|
|
|
+ log.debug("websocket 握手成功,开始进行权限校验,Authorization:{}",authorization);
|
|
|
+ }
|
|
|
+ if(StrUtil.isNullOrUndefined(authorization)){
|
|
|
+ Tio.send(channelContext,WsResponse.fromText(JSONUtil.toJsonStr(R.fail("授权失败")),"utf-8"));
|
|
|
+ //给返回信息一些时间 todo
|
|
|
+ Thread.sleep(50);
|
|
|
+ channelContext.setClosed(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ SaSession tokenSessionByToken = StpUtil.getTokenSessionByToken(authorization);
|
|
|
+ if(null==tokenSessionByToken){
|
|
|
+ Tio.send(channelContext,WsResponse.fromText(JSONUtil.toJsonStr(R.fail("授权失败")),"utf-8"));
|
|
|
+ if(log.isDebugEnabled()){
|
|
|
+ log.debug("Authorization:{},鉴权失败",authorization);
|
|
|
+ }
|
|
|
+ Tio.unbindToken(channelContext);
|
|
|
+ Thread.sleep(50);
|
|
|
+ channelContext.setClosed(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Tio.bindToken(channelContext, JSONUtil.toJsonStr(tokenSessionByToken));
|
|
|
+ Tio.send(channelContext,WsResponse.fromText(JSONUtil.toJsonStr(R.success("连接成功")),"utf-8"));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object onBytes(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object onClose(WsRequest wsRequest, byte[] bytes, ChannelContext channelContext) throws Exception {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object onText(WsRequest wsRequest, String message, ChannelContext channelContext) throws Exception {
|
|
|
+ System.out.println("接收到文本消息:"+message);
|
|
|
+ if (StrUtil.isEmpty(message)) {
|
|
|
+ Tio.unbindBsId(channelContext);
|
|
|
+ channelContext.setClosed(true);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|