[英]How do I send a spring message to client from a Java object
I need to send stomp messages from an object to my client using a SimpMessagingTemplate (or any message in the JSON format) 我需要使用SimpMessagingTemplate(或JSON格式的任何消息)将stomp消息从对象发送到我的客户端
However my object is not a Controller and if I declare my class as a controller, my Spring boot application doesn't start due to a bean not set up. 但是我的对象不是Controller,如果我将我的类声明为控制器,我的Spring启动应用程序由于没有设置bean而无法启动。
I am creating a game for my bachelor thesis that is hosted on a Spring server. 我正在为我的学士论文创建一个游戏,该游戏托管在Spring服务器上。 In my app I have rooms which each have a gameSession with one thread running. 在我的应用程序中,我有房间,每个房间都有一个运行一个线程的gameSession。
I currently have a Class called RoomsController which sends scheduled messages to the server like this 我目前有一个名为RoomsController的类,它将预定的消息发送到服务器
@Controller
public class RoomsController {
@Autowired
private SimpMessagingTemplate template;
@Scheduled(fixedRate = 500)
public void updateRoomsInterface() throws Exception {
Game.getInstance().getRooms().forEach((k,v) -> {
if (v != null) {
System.out.println("Ted posilam do room " + k + " ktery se jmenuje " + v.getName() + " cely seznam hracu, schvalne co mi prijde, ok?");
Map <String, Player> playersList = v.getPlayers();
// String message = v.getPlayers().get;
this.template.convertAndSend("/topic/game/room/"+v.getID(), playersList);
}
});
}
}
This is somewhat ok, but I need my GameSession objects to be independent of the server timer (because different games start at different times and I need to use ticks of the game) 这有点好,但我需要我的GameSession对象独立于服务器计时器(因为不同的游戏在不同的时间开始,我需要使用游戏的滴答)
My GameSession currently looks like this: 我的GameSession目前看起来像这样:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
public class GameSession extends Thread {
@Autowired
private SimpMessagingTemplate template;
private volatile boolean exit = false;
private Room room;
public GameSession(Room room) {
this.room = room;
status = "WAITING";
}
@Override
public void run() {
// dokud ne exit
while (!false) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sendMessage();
}
}
public void sendMessage() {
System.out.println("Does this execute?");
this.template.convertAndSend("/topic/game/room/"+room.getID()+"/session", "THIS IS A TEST");
}
}
But when this executes, I get an Exception in thread 但是当这个执行时,我在线程中得到一个异常
Does this execute?
Exception in thread "Thread-6" java.lang.NullPointerException
at cz.vse.pavm07.bp.objects.GameSession.sendMessage(GameSession.java:65)
at cz.vse.pavm07.bp.objects.GameSession.run(GameSession.java:34)
I need each gameSession to send the messages directly from the gameSession objects because of the ticks. 我需要每个gameSession直接从gameSession对象发送消息,因为滴答声。
What am I doing wrong? 我究竟做错了什么?
Your GameSession
is not marked as bean, so add @Component
or another stereotype annotation to mark it as spring bean. 您的GameSession
未标记为bean,因此请添加@Component
或其他GameSession
型注释以将其标记为spring bean。
@Service
public class GameSession extends Thread {
@Autowired
private SimpMessagingTemplate template;
//code here
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.