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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java Glassfish Spring Sockjs失败:WebSocket握手期间出错:意外的响应代码:500相关的知识,希望对你有一定的参考价值。

大家好我有这些错误

  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的新手

答案

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

不要使用相同的端点“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 }));

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

以上是关于Java Glassfish Spring Sockjs失败:WebSocket握手期间出错:意外的响应代码:500的主要内容,如果未能解决你的问题,请参考以下文章

GlassFish不起作用。错误:java.BindException

Glassfish 3.1.2.2上的java.lang.NoClassDefFoundError

Glassfish、Java 电子邮件和证书

java.lang.ClassNotFoundException:Glassfish 中的数据源

Glassfish 4.0 加载应用程序时出现异常,java.lang.IllegalStateException

Glassfish - java.lang.NoClassDefFoundError