1
我正在使用WebSocket聊天。我做了一個簡單的發送每個消息給每個人。但我正在嘗試製作類似私人消息的內容,而且我真的不知道該怎麼做。我嘗試過不同的選擇,但我不知道如何使它工作。WebSocket的Spring STOMP - 「私人」消息傳遞
這裏是我的代碼:
WebSocketConfig.java
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
config.enableSimpleBroker("/topic/", "/queue/", "/user/");
config.setApplicationDestinationPrefixes("/app");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/gs-guide-websocket").withSockJS();
}
}
GreetingController.java
@Controller
public class GreetingController {
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(Message message) throws Exception {
return new Greeting(message.getFrom_user_id(), message.getMessage(), message.getTo_user_id());
}
}
而我的JS函數來連接和發送消息:
function connect() {
var socket = new SockJS('/gs-guide-websocket');
stompClient = Stomp.over(socket);
var theUserId = 1;
stompClient.connect({user:theUserId}, function (frame) {
setConnected(true);
console.log('Connected: ' + frame);
//stompClient.subscribe('/topic/greetings', function (greeting) {
stompClient.subscribe('/topic/greetings', function (greeting) {
showGreeting(JSON.parse(greeting.body).content);
});
});
}
function sendName() {
stompClient.send("/app/hello", {}, JSON.stringify(
{
'from_user_id': parseInt($("#from_user_id").val()),
'message': $("#message").val(),
'to_user_id': parseInt($("#to_user_id").val())
}));
}
如何使私人信息的工作?有人可以解釋我應該如何工作嗎?
乾杯安德魯!
感謝訂閱主題您!奇蹟般有效 :) – Andrew