💩 websocket
This commit is contained in:
parent
ec642e3f2e
commit
f9528399bf
@ -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
|
||||
|
@ -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")
|
||||
@SendTo("/topic/greening")
|
||||
public void sendMessage(JSONObject message, StompHeaderAccessor headerAccessor) {
|
||||
this.template.convertAndSend("/public/greetings",message);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user