Spring和SockJs的控制器组件问题

时间:2016-04-06 08:37:01

标签: spring spring-mvc websocket spring-websocket sockjs

使用Spring和SockJs配置WebSocket时遇到问题。

我已将所有应用程序配置为Spring Documentation,连接似乎没问题,但是当我发送消息时,Controller从不参与,但它不起作用。

这是配置组件:

@Configuration
@EnableWebMvc
@EnableWebSocketMessageBroker
public class MyWebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer implements WebSocketConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/mySocket").withSockJS().setInterceptors(new MySocketHandshakeInterceptor());;
    }

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

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {

    }

}

这是HandshakeInterceptor实现:

public class MySocketHandshakeInterceptor implements HandshakeInterceptor {

    @Override
    public void afterHandshake(ServerHttpRequest paramServerHttpRequest, ServerHttpResponse paramServerHttpResponse, WebSocketHandler paramWebSocketHandler, Exception paramException) {
        // TODO Auto-generated method stub
        System.out.println("MySocketHandshakeInterceptor afterHandshake");
    }

    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> paramMap)
            throws Exception {
        System.out.println("MySocketHandshakeInterceptor beforeHandshake");
        return true;
    }

}

这是Controller组件:

@Controller
public class MySocketController {

    @MessageMapping("/testsocket")
    @SendTo("/topic/testresponse")
    public MessageOut test(MessageIn message) throws Exception {
        System.out.println("MySocketController start");
        System.out.println("MySocketController test "+ message);
        return new MessageOut("test OK");
    }

}

这些是MessageIn和MessageOut:

public class MessageIn {

    private String test;

    /**
     * @return the test
     */
    public String getTest() {
        return test;
    }

}


public class MessageOut {

    private String result;

    public MessageOut(String result) {
        super();
        this.result = result;
    }

    /**
     * @return the test
     */
    public String getResult() {
        return result;
    }

    /**
     * @param result the result to set
     */
    public void setResult(String result) {
        this.result = result;
    }

}

最后,这是客户端(javascript):

var socketSession = {};

connectToMySocket();

function connectToVideoSocket() {
    socketSession.socket = new SockJS("/mySocket");
    socketSession.socket.stomp = Stomp.over(socketSession.socket);
    socketSession.socket.stomp.debug = null;

    socketSession.socket.stomp.connect({}, function () {
        socketSession.socket.stomp.subscribe("/topic/testresponse", function (data) {
            console.log(data);
        });
    });

}

这是我为测试套接字而启动的命令:

socketSession.socket.stomp.send("app/testsocket", {}, JSON.stringify({'test': 'test'}));

在系统输出控制台中,我只能看到两行:

MySocketHandshakeInterceptor beforeHandshake
MySocketHandshakeInterceptor afterHandshake

拦截器工作正常,但我没有看到Controller组件的任何打印。

怎么了? 感谢。

1 个答案:

答案 0 :(得分:0)

我自己解决了。

这是客户端的问题。

正确的脚本是:

var mySocket = undefined;
var stompClient = undefined;

connectToVideoSocket();

function connectToVideoSocket() {
    mySocket = new SockJS('/mySocket');
    stompClient = Stomp.over(mySocket);
    stompClient.debug = null;

    stompClient.connect({}, function(frame) {
        stompClient.subscribe('/topic/testresponse', function(data){
            console.log(JSON.parse(data.body).result);
        });
    });
}

这样就可以了。