Stomp订阅回调不适用于Spring MVC

时间:2015-12-31 04:50:06

标签: spring stomp

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

控制器     --------------     @Controller     public class MessageController {         // @自动私有SimpMessagingTemplate模板;         @Autowired私人SimpMessageSendingOperations模板;         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 :(得分:0)

请按照Spring WebSocket

进行操作

答案 1 :(得分: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);
相关问题