如何从普通请求调用中调用@SendTo,即@RequestMapping [英] How to call @SendTo from Normal Request Call i.e @RequestMapping

查看:61
本文介绍了如何从普通请求调用中调用@SendTo,即@RequestMapping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Spring MVC 实现了 Web Socket,它对我来说工作正常,即从一个浏览器工作到另一个浏览器,该浏览器使用此代码为那些套接字打开.

I have implemented Web Socket using Spring MVC and it is working fine for me i.e work from one browser to another browser which is open for those socket using this code.

@MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public HelloMessage greeting(HelloMessage message) throws Exception {
        Thread.sleep(3000); // simulated delay
        return message;
    }

任何人都可以帮助我从普通的 api 控制器调用 @SendTo("/topic/greetings") 吗?我尝试使用它,但它对我不起作用

Can any one help me for who to call @SendTo("/topic/greetings") from normal api controller.I have try using this but it is not working for me

@RequestMapping(value = "/sendMessage")
    @SendTo("/topic/greetings")
    public HelloMessage sendMessage() throws Exception {
        return new HelloMessage((int) Math.random(), "This is Send From Server");
    }

对此有什么想法吗?

谢谢

推荐答案

我已经找到了解决方案

@Autowired
private SimpMessagingTemplate template;


@RequestMapping(value = "/sendMessage")
public void sendMessage() throws Exception {
    this.template.convertAndSend("/topic/greetings", new HelloMessage(
            (int) Math.random(), "This is Send From Server"));
}

通过使用这个,我们可以发送消息来打开 WebSocket.

by using this we can send message to open WebSocket.

谢谢

这篇关于如何从普通请求调用中调用@SendTo,即@RequestMapping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆