SimpMessagingTemplate not sending messages to websocket

前端 未结 2 1740
暖寄归人
暖寄归人 2020-12-17 02:51

I have the following contoller

@Controller
public class GreetingController 
{
        @MessageMapping(\"/hello\")
        @SendTo(\"/topic/greetings\")
              


        
相关标签:
2条回答
  • 2020-12-17 03:41

    I ran your code and instead of sending simple string you should send object so instead of

    this.template.convertAndSend("/topic/greetings", "message");
    

    should be

    this.template.convertAndSend("/topic/greetings", messageObject);

    and your object should have some method to access content like

    MessageObject.getConent();
    

    otherwise you can always use toString()

    and your js should be

    stompClient.subscribe('/topic/greetings', function(greeting){
        console.log(JSON.parse(greeting.body).content);
    });
    

    notice content on the end of received object

    0 讨论(0)
  • 2020-12-17 03:51

    @RequestMapping(path="/meeting",method=RequestMethod.POST) can not be used because it is a @Controller not a @RestController

    0 讨论(0)
提交回复
热议问题