spring-mvc、websockets推送集成 [英] spring-mvc, websockets push integration

查看:54
本文介绍了spring-mvc、websockets推送集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经点击了这个链接 http://spring.io/guides/gs/messaging-stomp-websocket/ 并启动并运行应用程序.

I've followed this link http://spring.io/guides/gs/messaging-stomp-websocket/ and got the app up and running.

我想要的远不止这些,我希望能够将数据推送回客户端,而客户端无需发送任何东西.

What I wanted was a little more than that, I wanted to to be able to push the data back to the client without the client having to send any thing.

所以我用类似于下面的监听器设置了一个长时间运行的任务

So I've setup a long running task with a listener similar to the below

GreetingController 实现 RunnableListener并且 RunnableListener 有一个方法public Greeting greeting(HelloMessage message);

GreetingController implements RunnableListener and the RunnableListener has a method public Greeting greeting(HelloMessage message);

该方法的实现是启动一个线程,然后调用监听器方法..

The implementation of the method is to kick off a thread and then call the listener method..

发生这种情况时,我会在控制台上看到输出,但在浏览器上看不到任何内容.

I see the output on the console when that happens, but I don't see anything on the browser.

谁能告诉我如何启动一个正在运行的任务并让服务器使用 Spring 而不是 poll 将内容推送到浏览器(javascript 中的 setTimeout 内容?)

Could anyone please show me how to kick off a running task and let the server push the content to the browser using Spring instead of poll (setTimeout stuff in javascript?)

问候锡

推荐答案

这个 RunnableListener 接口是什么?是什么触发了此任务 - 是否定期安排?

What is this RunnableListener interface? What is triggering this task - is it scheduled regularly?

一旦客户端订阅了给定的主题(此处为 /topic/greetings),您可以随时使用 MessagingTemplate 向该主题发送消息.例如,您可以安排此任务并让它定期发送有关给定主题的消息:

Once the client has subscribed to a given topic (here, /topic/greetings), you can send messages to that topic whenever you want using a MessagingTemplate. For example, you could schedule this task and let it send messages regularly on a given topic:

@Service
public class GreetingService {

    private SimpMessagingTemplate template;

    @Autowired
    public GreetingService(SimpMessagingTemplate template) {
        this.template = template;
    }

    @Scheduled(fixedDelay=10000)
    public void greet() {
        this.template.convertAndSend("/topic/greetings", "Hello");
    }

}

查看 更多详细信息的参考文档.

这篇关于spring-mvc、websockets推送集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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