|
|
@@ -1,25 +1,13 @@
|
|
|
package com.xunmei.host.websocket.controller;
|
|
|
|
|
|
import com.xunmei.common.core.web.domain.AjaxResult;
|
|
|
-import com.xunmei.common.redis.utils.RedisUtils;
|
|
|
-import com.xunmei.host.websocket.holder.WebSocketSessionHolder;
|
|
|
-import com.xunmei.host.websocket.redis.WebsocketPublisher;
|
|
|
+import com.xunmei.host.websocket.service.WebsocketService;
|
|
|
import com.xunmei.system.api.domain.websocket.RedisWebsocketMsg;
|
|
|
-import org.redisson.api.RKeys;
|
|
|
-import org.redisson.api.RedissonClient;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.HashSet;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
-import java.util.regex.Matcher;
|
|
|
-import java.util.regex.Pattern;
|
|
|
-
|
|
|
/**
|
|
|
* @author gaoxiong
|
|
|
* @Title:
|
|
|
@@ -32,79 +20,20 @@ import java.util.regex.Pattern;
|
|
|
public class WebsocketController {
|
|
|
|
|
|
@Autowired
|
|
|
- private WebsocketPublisher websocketPublisher;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ private WebsocketService websocketService;
|
|
|
|
|
|
-
|
|
|
- @GetMapping("/send")
|
|
|
- public String sendMessage(String message) {
|
|
|
- RedisWebsocketMsg msg = new RedisWebsocketMsg();
|
|
|
- Map<String,String> map = new HashMap<>();
|
|
|
- map.put("name","王燕妮");
|
|
|
- map.put("sex","女");
|
|
|
- msg.setContent(map);
|
|
|
- RedissonClient client = RedisUtils.getClient();
|
|
|
- RKeys keys = client.getKeys();
|
|
|
- Iterable<String> keyTokens = keys.getKeysByPattern(WebSocketSessionHolder.REDIS_TOPIC_WEBSOCKET_TOKEN + "*");
|
|
|
- Set<String> tokens = new HashSet<>();
|
|
|
- for (String keyToken : keyTokens) {
|
|
|
- String token = RedisUtils.getCacheObject(keyToken);
|
|
|
- tokens.add(token);
|
|
|
- }
|
|
|
- msg.setTokens(tokens);
|
|
|
- websocketPublisher.sendMessage(msg);
|
|
|
- return "发送成功";
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 给所有连接的主机发送命令消息
|
|
|
- * @param obj
|
|
|
- * @return
|
|
|
- */
|
|
|
@PostMapping("/sendAllMsg")
|
|
|
- public AjaxResult sendAllMessage(Object obj){
|
|
|
+ public AjaxResult sendAllMessage(Object obj) {
|
|
|
RedisWebsocketMsg msg = new RedisWebsocketMsg();
|
|
|
- msg.setContent(msg);
|
|
|
- RedissonClient client = RedisUtils.getClient();
|
|
|
- RKeys keys = client.getKeys();
|
|
|
- Iterable<String> keyTokens = keys.getKeysByPattern(WebSocketSessionHolder.REDIS_TOPIC_WEBSOCKET_TOKEN + "*");
|
|
|
- Set<String> tokens = new HashSet<>();
|
|
|
- for (String keyToken : keyTokens) {
|
|
|
- String token = RedisUtils.getCacheObject(keyToken);
|
|
|
- tokens.add(token);
|
|
|
- }
|
|
|
- msg.setTokens(tokens);
|
|
|
- websocketPublisher.sendMessage(msg);
|
|
|
+ msg.setContent(obj);
|
|
|
+ websocketService.sendMgsToAll(msg);
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
|
|
|
|
-
|
|
|
@PostMapping("/sendListMsg")
|
|
|
- public AjaxResult sendListMessage(RedisWebsocketMsg msg){
|
|
|
- websocketPublisher.sendMessage(msg);
|
|
|
+ public AjaxResult sendListMessage(RedisWebsocketMsg msg) {
|
|
|
+ websocketService.sendMgsToAll(msg);
|
|
|
return AjaxResult.success();
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- String url = "/things/SmartBulb/LivingRoom/service/invoke/reply";
|
|
|
-
|
|
|
- // 正则表达式模式
|
|
|
- Pattern pattern = Pattern.compile("^/things/(\\w+)/(\\w+)/service/invoke$");
|
|
|
-
|
|
|
- // 创建Matcher对象
|
|
|
- Matcher matcher = pattern.matcher(url);
|
|
|
-
|
|
|
- // 检查是否匹配成功
|
|
|
- if (matcher.find()) {
|
|
|
- // 提取动态参数
|
|
|
- String productName = matcher.group(1);
|
|
|
- String deviceName = matcher.group(2);
|
|
|
- System.out.println("Product Name: " + productName);
|
|
|
- System.out.println("Device Name: " + deviceName);
|
|
|
- } else {
|
|
|
- System.out.println("URL does not match the expected format.");
|
|
|
- }
|
|
|
- }
|
|
|
}
|