go to  ForumEasy.com   
JavaPro
Home » Archive » Message


[Email To Friend][View in Live Context][prev topic « prev post | next post » next topic]
  Example -- JS Client
 
Subject: Example -- JS Client
Author: WebSpider
In response to: How it works -- how the broker wire the publish-subscribe mapping internally?
Posted on: 07/11/2020 01:01:42 AM

Connect & Subscribe

function connect() {
    // 'websocket' -- naming context
    // 'api' -- servlet context
    // 'stomp' -- STOMP endpoint
    var socket = new SockJS('/websocket/api/stomp'); 
    stompClient = Stomp.over(socket);
    stompClient.connect({}, function(frame) {
        stompClient.subscribe('/topic/messages', 
            function(messageOutput) { // JS Object <-- Java OutputMessage
                showMessageOutput(JSON.parse(messageOutput.body)); // JS String --> JS Object
            }
        );
   });
}



Publish
function sendMessage() {
    stompClient.send('/channel/chat', 
        {}, // header
	JSON.stringify({...}) // JS Object --> JS String
    );
}                



 

> On 07/11/2020 12:59:42 AM WebSpider wrote:

@Controller
public class WebSocketController {

	// publisher send message to --> "/channel/chat"
	//       --> broker relay it to --> "/topic/messages"
        //       --> broker push it to all subscribers
	@MessageMapping("/chat")
	@SendTo("/topic/messages")
	public OutputMessage send(Message message) throws Exception {
	    String time = new SimpleDateFormat("HH:mm").format(new Date());
	    return new OutputMessage(message.getFrom(), message.getText(), time);
	}
}





References:

 


 
Powered by ForumEasy © 2002-2022, All Rights Reserved. | Privacy Policy | Terms of Use
 
Get your own forum today. It's easy and free.