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

问题描述 投票:0回答:1

大家好我有这些错误

  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配置

 @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);
    }

任何想法如何修复这个?我是弹簧websocket的新手

java spring websocket glassfish sockjs
1个回答
0
投票

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

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

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

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

这里还删除了contextPath vix

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

在这里再次相同

 stompClient.send("/app/hello", {}, JSON.stringify({ 'name': name }));

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

© www.soinside.com 2019 - 2024. All rights reserved.