Java Glassfish Spring Sockjs失败:WebSocket握手期间出错:意外的响应代码:500

时间:2016-02-03 11:47:59

标签: java spring websocket glassfish sockjs

大家好,我有这些错误

  WebSocket connection to 'ws://localhost:8080/vix/hello/598/rula3cfq/websocket' failed: Error during WebSocket handshake: Unexpected response code: 500

我的玻璃鱼版本是4,春季4.2.4.RELEASE

这是我的控制器

@Controller
public class Test
{

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

这是websocket config

 @Configuration
 @EnableWebSocketMessageBroker
 public class WebsocketConfig extends        
              AbstractWebSocketMessageBrokerConfigurer
{
@Override
public void configureMessageBroker(MessageBrokerRegistry config) {
    config.enableSimpleBroker("/topic");
    config.setApplicationDestinationPrefixes("/app");
}

public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/hello")
            .setAllowedOrigins("*").withSockJS();
}

}

此安全配置

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring().antMatchers("/webjars/**", "/images/**", "/oauth/uncache_approvals", "/oauth/cache_approvals","/topic/**","/app/**",
            "/hello/**", "/calcApp/**");
}

这是我使用stomp和sockjs的前沿

    var stompClient = null;

    function setConnected(connected) {
        document.getElementById('connect').disabled = connected;
        document.getElementById('disconnect').disabled = !connected;
        document.getElementById('conversationDiv').style.visibility = connected ? 'visible' : 'hidden';
        document.getElementById('response').innerHTML = '';
    }

    function connect() {
        var socket = new SockJS('http://localhost:8080/vix/hello');
        stompClient = Stomp.over(socket);
        stompClient.connect({}, function(frame) {
            setConnected(true);
            console.log('Connected: ' + frame);
            stompClient.subscribe('/vix/topic/greetings', function(greeting){
                showGreeting(JSON.parse(greeting.body).content);
            });
        });
    }

    function disconnect() {
        if (stompClient != null) {
            stompClient.disconnect();
        }
        setConnected(false);
        console.log("Disconnected");
    }

    function sendName() {
        var name = document.getElementById('name').value;
        stompClient.send("/vix/app/hello", {}, JSON.stringify({ 'name': name }));
    }

    function showGreeting(message) {
        var response = document.getElementById('response');
        var p = document.createElement('p');
        p.style.wordWrap = 'break-word';
        p.appendChild(document.createTextNode(message));
        response.appendChild(p);
    }

任何想法如何解决这个问题?我是spring websocket的新人

1 个答案:

答案 0 :(得分:0)

在你的连接方法中,你可以离开

不要使用相同的端点“hello”和MessageMapping,将会混淆

Future[Future[SomeObject]]

此处还删除了contextPath vix

public void registerStompEndpoints(StompEndpointRegistry registry) {
    registry.addEndpoint("/ws")
        .setAllowedOrigins("*").withSockJS();
}

var socket = new SockJS('/hello');

这里再次相同

stompClient.subscribe('/topic/greetings', function(greeting){
    showGreeting(JSON.parse(greeting.body).content);
});

基本上,所有问题都是添加上下文,因为在添加第一个“/”时默认添加了上下文