diff --git a/src/main/java/com/rymcu/forest/config/WebSocketStompConfig.java b/src/main/java/com/rymcu/forest/config/WebSocketStompConfig.java index a158e49..3eb259f 100644 --- a/src/main/java/com/rymcu/forest/config/WebSocketStompConfig.java +++ b/src/main/java/com/rymcu/forest/config/WebSocketStompConfig.java @@ -30,7 +30,7 @@ public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer { public void configureMessageBroker(MessageBrokerRegistry registry) { // 订阅Broker名称 user点对点 topic广播即群发 - registry.enableSimpleBroker("/user","/public"); + registry.enableSimpleBroker("/topic", "/user"); // 全局(客户端)使用的消息前缀 registry.setApplicationDestinationPrefixes("/app"); // 点对点使用的前缀 无需配置 默认/user diff --git a/src/main/java/com/rymcu/forest/web/api/common/WebSocketController.java b/src/main/java/com/rymcu/forest/web/api/common/WebSocketController.java index 62f5e26..7a3a4b1 100644 --- a/src/main/java/com/rymcu/forest/web/api/common/WebSocketController.java +++ b/src/main/java/com/rymcu/forest/web/api/common/WebSocketController.java @@ -1,7 +1,6 @@ package com.rymcu.forest.web.api.common; import com.alibaba.fastjson.JSONObject; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; import org.springframework.messaging.simp.SimpMessagingTemplate; @@ -9,7 +8,7 @@ import org.springframework.messaging.simp.annotation.SendToUser; import org.springframework.messaging.simp.stomp.StompHeaderAccessor; import org.springframework.stereotype.Controller; -import java.util.HashMap; +import javax.annotation.Resource; /** * @author ronger @@ -17,42 +16,18 @@ import java.util.HashMap; @Controller public class WebSocketController { - @Autowired + @Resource private SimpMessagingTemplate template; @MessageMapping("/sendMessage") - @SendTo("/public/greetings") - public void sendMessage(JSONObject message, StompHeaderAccessor headerAccessor){ - this.template.convertAndSend("/public/greetings",message); + @SendTo("/topic/greening") + public void sendMessage(JSONObject message, StompHeaderAccessor headerAccessor) { + this.template.convertAndSend("/topic/greening", message); } @MessageMapping("/message") @SendToUser("/message") - public void message(JSONObject message){ - String type = message.get("type").toString(); - HashMap res = (HashMap) message.get("data"); - HashMap mine = (HashMap) res.get("mine"); - HashMap to = (HashMap) res.get("to"); - System.out.println(to.get("type")); - boolean flag = to.get("type").equals("friend")?true:false; - String id = to.get("id").toString(); - HashMap map = new HashMap(); - map.put("id",mine.get("id")); - map.put("avatar",mine.get("avatar")); - map.put("formid",mine.get("id")); - map.put("username",mine.get("username")); - map.put("type",to.get("type")); - map.put("content",mine.get("content")); - map.put("mine",false); - map.put("cid",0); - map.put("timestamp",""); - JSONObject json = new JSONObject(); - json.put("type",type); - json.put("data",map); - if(flag){ - this.template.convertAndSendToUser(id,"/message",json); - }else{ - this.template.convertAndSendToUser(id,"/message",json); - } + public void message(JSONObject message, StompHeaderAccessor headerAccessor) { + this.template.convertAndSendToUser(message.getString("to"), "/message", message); } }