Spring 4 STOMP Websockets -如何實現多通道。

[英]Spring 4 STOMP Websockets - How to realise Multichannel


As in the newest Spring 4 tutorial with STOMP and SockJS, we find a RequestMapping of the following kind :

在與STOMP和SockJS的最新Spring 4教程中,我們發現了以下類型的請求映射:

Homecontroller.java

Homecontroller.java

@MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        Thread.sleep(3000); // simulated delay
        return new Greeting("Hello, " + message.getName() + "!");
    }

now I tried to realize multichannel support by modifying this requestmapping:

現在我嘗試通過修改這個請求映射來實現多通道支持:

@MessageMapping("/hello/{id}")
    @SendTo("/topic/greetings/{id}")
    public Greeting greetingMultichannel(HelloMessage message) throws Exception {
        this.info();
        Thread.sleep(300); // simulated delay
        return new Greeting(" Hello, " + message.getName() + "!");
    }

the MessageMapping does not work,sendTo does not deliver back to the sepecified URL. Although I did subscribe to the right channel and the message goes out without problems, as seen in this

MessageMapping不起作用,sendTo沒有返回到指定的URL。雖然我確實訂閱了正確的頻道,並且消息沒有出現問題,就像這里看到的那樣


debug log:

調試日志:

Opening Web Socket... stomp.js:130 Web Socket Opened... stomp.js:130

開放的網絡套接字……跺腳。js:130網絡套接字了……stomp.js:130

CONNECT accept-version:1.1,1.0 heart-beat:10000,10000

連接accept-version:1.1、1.0心跳:10000、10000

stomp.js:130 <<< CONNECTED heart-beat:0,0 version:1.1

跺腳。<< <連接的心跳:0,0版本:1.1< p>

stomp.js:130 connected to server undefined stomp.js:130 Connected: CONNECTED version:1.1 heart-beat:0,0

跺腳。130連接到服務器未定義的stomp。js:130連接:連接版本:1.1心跳:0,0。

(index):23

(指數):23

SUBSCRIBE id:sub-0 destination:/topic/greetings/1

訂閱id:為目的地:/ 1 /主題/問候

stomp.js:130

stomp.js:130

SEND destination:/app/hello/1 content-length:18

內容長度發送目的地:/ app / hello / 1:18

{"name":"textext"}

{ " name ":" textext " }


there should be a response but there is nothing coming back..

應該會有回應,但是不會有回應。

Can someone see what I am missing here?

有人能看到我在這里錯過了什么嗎?

cheers, Heinrich

歡呼,海因里希

1 个解决方案

#1


3  

There is currently no way to pass parameters to @SendTo/ @SendToUser, placeholders on @MessageMapping are not available in @SendTo / @SendToUser. There's an open Jira for that, keep an eye on it.

目前無法將參數傳遞給@SendTo/ @SendToUser, @MessageMapping的占位符在@SendTo/ @SendToUser中不可用。有一個公開的Jira,關注它。

In the meantime, use the SimpMessagingTemplate:

同時,使用SimpMessagingTemplate:

@Autowired 
private SimpMessagingTemplate simpMessagingTemplate;

@MessageMapping("/hello/{id}")
public void greetingMultichannel(HelloMessage message, @DestinationVariable("id") Long id) throws Exception {
    this.info();
    Thread.sleep(300); // simulated delay

    simpMessagingTemplate.convertAndSend("/topic/greetings/" + id, new Greeting(" Hello, " + message.getName() + "!"));
}

Update 8 September 2015:

2015年9月8日更新:

As of Spring 4.2, destination variable placeholders can be used in @SendTo / @SendToUser. This is now possible:

從Spring 4.2開始,可以在@SendTo / @SendToUser中使用目標變量占位符。這是可能的:

@MessageMapping("/hello/{id}")
@SendTo("/topic/greetings/{id}")
public Greeting greetingMultichannel(HelloMessage message) throws Exception {
    this.info();
    Thread.sleep(300); // simulated delay
    return new Greeting(" Hello, " + message.getName() + "!");
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/04/10/8f3df7a4613156beb5184697c9f4bd44.html



 
  © 2014-2022 ITdaan.com 联系我们: