如何使用Spring通过WebSocket向客户端发送消息

编程入门 行业动态 更新时间:2024-10-12 05:51:40
本文介绍了如何使用Spring通过WebSocket向客户端发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试将Spring与websocket一起使用.我从本教程开始调查.

I try to use Spring with websocket. I started my investigation with this tutorial.

在我的副客户端中,我有类似的东西来初始化与服务器的连接:

In my side client I have something like that to initialize the connection to the server :

function connect() { var socket = new SockJS('/myphotos/form'); stompClient = Stomp.over(socket); stompClient.connect({}, function(frame) { setConnected(true); console.log('Connected: ' + frame); stompClient.subscribe('/topic/greetings', function(greeting){ showGreeting(JSON.parse(greeting.body).content); }); }); }

效果很好,在我的控制器中,我可以按照以下课程进行操作:

It works great, in my controller I’m able to do my process in the following class :

@Controller @RequestMapping("/") public class PhotoController { @MessageMapping("/form") @SendTo("/topic/greetings") public Greeting validate(AddPhotosForm addPhotosForm) { return new Greeting("Hello world !"); } }

现在我想要做的是有一个线程向侦听"/topic/greeting"的客户端发送消息.我这样写了Runnable类:

Now what I want to do it’s having a thread sending a message to the client listening on "/topic/greeting". I wrote my Runnable class like this :

public class FireGreeting implements Runnable { private PhotoController listener; public FireGreeting(PhotoController listener) { this.listener = listener; } @Override public void run() { while (true) { try { Thread.sleep( 2000 ); listener.fireGreeting(); } catch ( InterruptedException e ) { e.printStackTrace(); } } } }

并像这样完成我的控制器:

And completed my controller like that :

@Controller @RequestMapping("/") public class PhotoController { @MessageMapping("/form") @SendTo("/topic/greetings") public Greeting validate(AddPhotosForm addPhotosForm) { // added this part FireGreeting r = new FireGreeting( this ); new Thread(r).start(); return new Greeting("Hello world !"); } // added this method @SendTo("/topic/greetings") public Greeting fireGreeting() { System.out.println("Fire"); return new Greeting("Fire"); } }

我需要调用PhotoController.fireGreeting方法,但是客户端没有任何反应. 有什么建议 ?谢谢.

The method PhotoController.fireGreeting is called as I want but nothing happened on the client side. Any suggestions ? Thank you.

推荐答案

感谢@Boris the Spider,我得以解决我的问题.正确的解决方案是执行以下操作:

I was able to solve my problem thanks to @Boris the Spider. The correct solution is to do something like that :

@Controller @RequestMapping("/") public class PhotoController { @Autowired private SimpMessagingTemplate template; @MessageMapping("/form") @SendTo("/topic/greetings") public Greeting validate(AddPhotosForm addPhotosForm) { FireGreeting r = new FireGreeting( this ); new Thread(r).start(); return new Greeting("Hello world !"); } public void fireGreeting() { System.out.println("Fire"); this.template.convertAndSend("/topic/greetings", new Greeting("Fire")); } }

更多推荐

如何使用Spring通过WebSocket向客户端发送消息

本文发布于:2023-11-25 23:19:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631747.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   客户端   发送消息   WebSocket   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!