使用webscoket+nginx+springboot时遇到了这样的问题!

我通过 websocket 访问我的sprinboot服务,途径 nginx代理,会偶发下面的问题:

// 这是连接的代码
function connect() {
    try {
        if ("WebSocket" in window) {
            var socket = new SockJS(path + "/examSocket");
            stompClient = Stomp.over(socket);
            stompClient.connect({cyryzj: cyryzj, ksid: ksid}, function (frame) {
                //console.log('连接成功: ' + frame);
                stompClient.subscribe('/exam/subscribe/' + cyryzj, function (frame) {
                    //console.log('订阅成功: ' + frame);
                }/*, {'cyryzj': cyryzj}*/);
            }, function errorCallBack(error) {
                console.log('连接失败【' + error + '】');
                reconnect();
            });
        } else {
            alert("您的浏览器不支持Websocket通信协议,请使用Chrome或者Firefox浏览器!");
            return;
        }
    } catch (e) {
        console.log('connect exception', e)
        reconnect();
    }
}

// 连接后 7s中推送一次信息
 if (examtype === '0_1') { // 限时考试才会通过redis去更新时间
                    connect();
                    timeRefreshTTL = setInterval(function () {
                        /*crontabRefreshTTL*/
                        send(ksid);
                    }, parseInt(timeLong));
                }
// java 接受信息代码如下
 @MessageMapping("/crontabRefreshExamTime/{ksid}/{cyryzj}/{time}")  // 获取客户端的推送信息
    public String crontabRefreshExamTime(@DestinationVariable("cyryzj") final String glzj,
                                         @DestinationVariable("ksid") final String ksid,
                                         @DestinationVariable("time") final String time) {
    // ...
}

错误如下

2021-10-15 23:36:31.375 [http-nio-8088-exec-24]  [o.s.w.s.server.support.DefaultHandshakeHandler] 
ERROR: Handshake failed due to invalid Upgrade header: null

2021-10-15 23:37:28.252 [http-nio-8088-exec-19]  [o.s.b.a.w.s.e.ErrorMvcAutoConfiguration$StaticView] 
ERROR: Cannot render error page for request [/sysgl/examSocket/055/fhas3jd4/htmlfile] and exception [你的主机中的软件中止了一个已建立的连接。] as the response has already been committed. As a result, the response may have the wrong status code.

2021-10-15 23:43:41.503 [http-nio-8088-exec-9]  [c.w.h.c.log.error.RisesinRestExceptionTranslator] 
ERROR: 服务器异常
org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.util.LinkedHashMap] with preset Content-Type 'text/event-stream;charset=UTF-8'

nginx 配置如下

        location /sysgl/ {
            rewrite ^/sysgl/(.*)$ /sysgl/$1 break;
            proxy_pass http://proxyserver;
            #proxy_pass   http://xxxx:8088/sysgl/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_buffering off;
            
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection  "upgrade";    
            #proxy_connect_timeout 10; #单位为秒
            #proxy_send_timeout 10;
            #proxy_read_timeout 10;
        }

我怀疑是连接超时造成,请比较了解这块的大佬们,给指点下!研究了好长时间了

阅读 5.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题