简体   繁体   English

Spring 中的多个房间使用 STOMP

[英]Multiple rooms in Spring using STOMP

Is it possible to create rooms with STOMP and Spring 4?是否可以使用 STOMP 和 Spring 4 创建房间? Socket.IO has rooms built in, so I'm wondering if Spring has this Socket.IO 有房间内置,所以我想知道 Spring 是否有这个

My code at the moment:我目前的代码:

@MessageMapping("/room/greet/{room}")
@SendTo("/room/{room}")
public Greeting greet(@DestinationVariable String room, HelloMessage message) throws Exception {
    return new Greeting("Hello, " + room + "!");
}

It would be ideal to have @SendTo("/room/{room}")最好有 @SendTo("/room/{room}")

however, I am limited to:但是,我仅限于:

@SendTo("/room/room1") 
@SendTo("/room/room2")
@SendTo("/room/room3") 

etc...which is VERY VERY unideal等等......这是非常非常不理想的

The client is:客户是:

stompClient.subscribe('/room/' + roomID, function(greeting){
    showGreeting(JSON.parse(greeting.body).content);
});

where roomID can be room1, room2, or room3... What If I want more rooms?其中 roomID 可以是 room1、room2 或 room3... 如果我想要更多房间怎么办? It feels like such a pain right now现在感觉好痛

It looks like this "room" feature is actually a publish/subscribe mechanism, something achieved with topics in Spring Websocket support (see STOMP protocol support and destinations for more info on this). 看起来这个“房间”功能实际上是一个发布/订阅机制,这是通过Spring Websocket支持中的主题实现的(有关此内容的更多信息,请参阅STOMP协议支持和目标 )。

With this example: 有了这个例子:

@Controller
public class GreetingController {

  @MessageMapping("/room/greeting/{room}")
  public Greeting greet(@DestinationVariable String room, HelloMessage message) throws Exception {
    return new Greeting("Hello, " + message.getName() + "!");
  }

}

If a message is sent to "/room/greeting/room1", then the return value Greeting will be automatically sent to "/topic/room/greeting/room1", so the initial destination prefixed with "/topic". 如果消息被发送到“/ room / greeting / room1”,则返回值Greeting将自动发送到“/ topic / room / greeting / room1”,因此初始目的地前缀为“/ topic”。

If you wish to customize the destination, you can use @SendTo just like you did, or use a MessagingTemplate like this: 如果您想自定义目的地,可以像使用@SendTo一样使用@SendTo ,或者像这样使用MessagingTemplate:

@Controller
public class GreetingController {

  private SimpMessagingTemplate template;

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

  @MessageMapping("/room/greeting/{room}")
  public Greeting greet(@DestinationVariable String room, HelloMessage message) throws Exception {
    Greeting greeting = new Greeting("Hello, " + message.getName() + "!");
    this.template.convertAndSend("/topic/room/"+room, greeting);  
  }

}

I think taking a quick look at the reference documentation and some useful examples, such as a portfolio app and a chat app should be useful. 我想快速查看参考文档和一些有用的示例,例如投资组合应用程序聊天应用程序应该很有用。

You can use netty socket the implementation of socket io in java可以使用netty socket在java中实现socket io

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM