|
|
@@ -1,23 +1,22 @@
|
|
|
package com.coffee.common.config.websocket.handler;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.coffee.common.Constants;
|
|
|
+import com.coffee.common.bo.LoginUser;
|
|
|
import com.coffee.common.config.websocket.DefaultMessageListener;
|
|
|
-import com.coffee.common.config.websocket.MessageResponse;
|
|
|
-import io.netty.channel.DefaultEventLoop;
|
|
|
-import io.netty.channel.EventLoop;
|
|
|
+import com.coffee.common.config.websocket.MessagingRequest;
|
|
|
+import com.coffee.common.config.websocket.WebSocketConstant;
|
|
|
import org.springframework.dao.DataAccessException;
|
|
|
import org.springframework.data.redis.connection.RedisConnection;
|
|
|
-import org.springframework.data.redis.connection.Subscription;
|
|
|
import org.springframework.data.redis.core.RedisCallback;
|
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
|
import org.springframework.util.ConcurrentReferenceHashMap;
|
|
|
import org.tio.core.ChannelContext;
|
|
|
-import reactor.util.function.Tuple3;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author lifang
|
|
|
@@ -43,7 +42,38 @@ public abstract class Subscribe implements WsHandler {
|
|
|
private Map<String,RedisConnection> redisConnectionMap=new ConcurrentReferenceHashMap<>();
|
|
|
|
|
|
|
|
|
- private EventLoop singleThreadEventLoop=new DefaultEventLoop() ;
|
|
|
+ public String getTopic(String productName,String param,String tenantId){
|
|
|
+ return WebSocketConstant.getTopic(this.getId(),productName, param, tenantId);
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onMessage(MessagingRequest message, ChannelContext channelContext) {
|
|
|
+ LoginUser loginUser = (LoginUser) channelContext.get(Constants.LOGIN_USER_KEY);
|
|
|
+ if(loginUser==null){
|
|
|
+ channelContext.setClosed(true);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //获取所有设备id
|
|
|
+ List<String> params = message.getParams();
|
|
|
+ if(CollectionUtil.isEmpty(params)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //需要处理的主题
|
|
|
+ List<String> subScribeTopic =
|
|
|
+ params.stream().map(deviceId -> getTopic(message.getProductName(), deviceId, loginUser.getTenantId()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ MessagingRequest.Type type = message.getType();
|
|
|
+ if(MessagingRequest.Type.sub==type){
|
|
|
+ //订阅主题
|
|
|
+ subScribeTopic.forEach(topic->this.subscribe(channelContext,topic));
|
|
|
+ }else {
|
|
|
+ //取消订阅主题
|
|
|
+ subScribeTopic.forEach(topic->this.unsubscribe(channelContext,topic));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* ws 订阅主题
|
|
|
* @param channelContext
|