Stomp订阅回调在Spring MVC中无法工作

3

我使用的是Spring MVC (4.2.2.RELEASE)和Tomcat 8。 我的要求是向浏览器发送通知。 请查看下面的代码。

控制器 -------------- @Controller public class MessageController { //@Autowired private SimpMessagingTemplate template; @Autowired private SimpMessageSendingOperations template; List noteList = new ArrayList();

    public MessageController() {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }  
    //@SubscribeMapping("/topic/notify")
    //@SendToUser("/topic/notify")
    public void sendMessage(String msg){
        Notification note = new Notification();
        note.setMsg(msg);
        noteList.add(note);
        template.convertAndSend("/topic/price", noteList);
    }
}


Configuration
---------------
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").withSockJS();

    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.setApplicationDestinationPrefixes("/app");
        registry.enableSimpleBroker("/queue","/topic");
    }

}

Client code
---------
var socket = new SockJS("/intl-fcstone/ws");
    var stompClient = Stomp.over(socket);
    var connectCallback = function() {
        console.log('----inside connectCallback before----');
        stompClient.subscribe('/topic/price', notifyUser);
        console.log('----inside connectCallback after----');
    };
    var errorCallback = function(error) {
        console.log('----inside errorCallback----');
        alert(error.headers.message);
    };
    stompClient.connect("guest", "guest", connectCallback, errorCallback);

    function notifyUser(frame) {
        console.log('----inside notifyUser1----'+frame);
        console.log('----inside notifyUser2----'+frame.body);
        var notes = JSON.parse(frame.body);
        console.log('----inside notifyUser3----'+notes);
        if(notes !=''){

            var $messageDiv = $('#notification_div'); // get the reference of the div
            $messageDiv.show().html(notes);
        }

Chrome console
---------------
Opening Web Socket...
stomp.js:134 Web Socket Opened...
stomp.js:134 >>> CONNECT
login:guest
passcode:guest
accept-version:1.1,1.0
heart-beat:10000,10000


stomp.js:134 <<< CONNECTED
version:1.1
heart-beat:0,0
user-name:suppliersin@gmail.com


stomp.js:134 connected to server undefined
(index):159 ----inside connectCallback before----CONNECTED
user-name:suppliersin@gmail.com
heart-beat:0,0
version:1.1


stomp.js:134 >>> SUBSCRIBE
id:sub-0
destination:/topic/price


(index):161 ----inside connectCallback after----CONNECTED
user-name:suppliersin@gmail.com
heart-beat:0,0
version:1.1

---------------------------

after this, nothing happens.

http://localhost:8080/intl-fcstone/ws/info shows

{
entropy: -646614392,
origins: [
"*:*"
],
cookie_needed: true,
websocket: true
}


Any help on this is much appreciated.
2个回答

0

非常感谢你,Charnjeet。我觉得我的问题与这个不同。https://gerrydevstory.com/2014/03/04/spring-4-websocket-stock-ticker-example/ - Tiger
非常感谢您的回复,Charnjeet Singh。我认为我的问题与此不同。https://gerrydevstory.com/2014/03/04/spring-4-websocket-stock-ticker-example/上述示例在我的服务器上完美运行。在Chrome控制台中,我可以清楚地看到以下内容:
SUBSCRIBE id:sub-0 destination:/topic/price
<<< MESSAGE destination:/topic/price content-type:application/json;charset=UTF-8 subscription:sub-0 message-id:_pv5roji-93 content-length:2但是,在我的当前项目中缺少这个。我不确定为什么在这种情况下没有订阅。
- Tiger

0
这个问题已经解决了。我在context.xml中添加了几行代码。
<websocket:message-broker application-destination-prefix="/app">
    <websocket:stomp-endpoint path="/ws">
        <websocket:handshake-interceptors>
            <bean class="org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor" />
        </websocket:handshake-interceptors>
        <websocket:sockjs session-cookie-needed="true" />
    </websocket:stomp-endpoint>
    <websocket:simple-broker prefix="/topic, /queue" />

    <websocket:client-inbound-channel>
        <websocket:executor core-pool-size="50"
                max-pool-size="100" keep-alive-seconds="60" />
    </websocket:client-inbound-channel>
    <websocket:client-outbound-channel>
        <websocket:executor core-pool-size="50"
                max-pool-size="100" keep-alive-seconds="60" />
    </websocket:client-outbound-channel>
    <websocket:broker-channel>
        <websocket:executor core-pool-size="50"
                max-pool-size="100" keep-alive-seconds="60" />
    </websocket:broker-channel>
</websocket:message-broker>

并使用

@Autowired private SimpMessageSendingOperations template; 

在我的控制器中。我还使用了
template.convertAndSendToUser(authentication.getName(), "/queue/price", noteList);

发送通知给特定用户。
最后,在客户端。
user = frame.headers['user-name'];
stompClient.connect(user, user, connectCallback, errorCallback);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接