💩 websocket
This commit is contained in:
parent
ec642e3f2e
commit
f9528399bf
@ -30,7 +30,7 @@ public class WebSocketStompConfig implements WebSocketMessageBrokerConfigurer {
|
|||||||
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
public void configureMessageBroker(MessageBrokerRegistry registry) {
|
||||||
|
|
||||||
// 订阅Broker名称 user点对点 topic广播即群发
|
// 订阅Broker名称 user点对点 topic广播即群发
|
||||||
registry.enableSimpleBroker("/user","/public");
|
registry.enableSimpleBroker("/topic", "/user");
|
||||||
// 全局(客户端)使用的消息前缀
|
// 全局(客户端)使用的消息前缀
|
||||||
registry.setApplicationDestinationPrefixes("/app");
|
registry.setApplicationDestinationPrefixes("/app");
|
||||||
// 点对点使用的前缀 无需配置 默认/user
|
// 点对点使用的前缀 无需配置 默认/user
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package com.rymcu.forest.web.api.common;
|
package com.rymcu.forest.web.api.common;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
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.MessageMapping;
|
||||||
import org.springframework.messaging.handler.annotation.SendTo;
|
import org.springframework.messaging.handler.annotation.SendTo;
|
||||||
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
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.messaging.simp.stomp.StompHeaderAccessor;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import javax.annotation.Resource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author ronger
|
* @author ronger
|
||||||
@ -17,42 +16,18 @@ import java.util.HashMap;
|
|||||||
@Controller
|
@Controller
|
||||||
public class WebSocketController {
|
public class WebSocketController {
|
||||||
|
|
||||||
@Autowired
|
@Resource
|
||||||
private SimpMessagingTemplate template;
|
private SimpMessagingTemplate template;
|
||||||
|
|
||||||
@MessageMapping("/sendMessage")
|
@MessageMapping("/sendMessage")
|
||||||
@SendTo("/public/greetings")
|
@SendTo("/topic/greening")
|
||||||
public void sendMessage(JSONObject message, StompHeaderAccessor headerAccessor) {
|
public void sendMessage(JSONObject message, StompHeaderAccessor headerAccessor) {
|
||||||
this.template.convertAndSend("/public/greetings",message);
|
this.template.convertAndSend("/topic/greening", message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@MessageMapping("/message")
|
@MessageMapping("/message")
|
||||||
@SendToUser("/message")
|
@SendToUser("/message")
|
||||||
public void message(JSONObject message){
|
public void message(JSONObject message, StompHeaderAccessor headerAccessor) {
|
||||||
String type = message.get("type").toString();
|
this.template.convertAndSendToUser(message.getString("to"), "/message", message);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user