Added new websocket endpoint

This commit is contained in:
Rohan Sircar 2020-08-06 19:35:46 +05:30
parent 5f01174764
commit 21fa7b5e64
2 changed files with 15 additions and 3 deletions

View File

@ -1,5 +1,8 @@
package org.ros.chatto.websocket; package org.ros.chatto.websocket;
import javax.validation.Valid;
import org.ros.chatto.model.ChatMessage;
import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.Payload; import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.messaging.simp.SimpMessagingTemplate; import org.springframework.messaging.simp.SimpMessagingTemplate;
@ -14,11 +17,20 @@ public class WebSocketController {
private final SimpMessagingTemplate smt; private final SimpMessagingTemplate smt;
@MessageMapping("/chat2")
// @SendTo("/topic/messages")
// @SendToUser("/queue/reply")
public void send2(@Payload final Message message) throws Exception {
smt.convertAndSendToUser(message.getTo(), "/queue/reply", message);
// return message;
}
@MessageMapping("/chat") @MessageMapping("/chat")
// @SendTo("/topic/messages") // @SendTo("/topic/messages")
// @SendToUser("/queue/reply") // @SendToUser("/queue/reply")
public void send(@Payload final Message message) throws Exception { public void send(@Valid final ChatMessage message) throws Exception {
smt.convertAndSendToUser(message.getTo(), "/queue/reply", message); smt.convertAndSendToUser(message.getToUser().getUserName(),
"/queue/reply", message);
// return message; // return message;
} }

View File

@ -14,7 +14,7 @@
} }
function connect() { function connect() {
var socket = new SockJS('/chat'); var socket = new SockJS('/chat2');
stompClient = Stomp.over(socket); stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) { stompClient.connect({}, function (frame) {
setConnected(true); setConnected(true);