WebSocket握手期间出错:意外的响应代码:302 [英] Error during WebSocket handshake: Unexpected response code: 302

查看:4641
本文介绍了WebSocket握手期间出错:意外的响应代码:302的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将基于REACT的WebSocket客户端连接到基于Java Jetty的Web Socket服务器时,出现以下错误-

WebSocket connection to 'ws://localhost:2319/ws' failed: Error during WebSocket handshake: Unexpected response code: 302

通过Chrome的Smart Web Socket客户端连接时不存在此错误.

我正在尝试基于REACT开发Web套接字客户端.客户端代码为-

var connection = new WebSocket('ws://localhost:2319/ws');
     connection.onopen = function () {
     // connection is opened and ready to use
    };

WebSocket服务器已基于Jetty.服务器代码为-

server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(SSConstants.WWBSOCKET_PORT);
    server.addConnector(connector);

    // Setup the basic application "context" for this application at "/"
    // This is also known as the handler tree (in jetty speak)
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/ws"); // Set to "/ws" for future integration with the main jetty server.
    server.setHandler(context);

    try {
        // Initialize javax.websocket layer
        ServerContainer wsContainer = WebSocketServerContainerInitializer.configureContext(context);

        // Add WebSocket endpoint to javax.websocket layer
        wsContainer.addEndpoint(WebsocketListener.class);
        server.start();           
    }
    catch (Throwable t) {
        ssLogger.logInfo("Websocket Server start exp : ");
        t.printStackTrace(System.err);
    }

输出-

WebSocket connection to 'ws://localhost:2319/ws' failed: Error during WebSocket handshake: Unexpected response code: 302

Request URL:ws://localhost:2319/ws
Request Method:GET
Status Code:302 Found

Response Headers
view source
Content-Length:0
Date:Fri, 11 Aug 2017 18:51:42 GMT
Location:http://localhost:2319/ws/
Server:Jetty(9.3.8.v20160314)

Request Headers
view source
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:Upgrade
Host:localhost:2319
Origin:https://localhost:1338
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:2OZooIjOX7G6kgNpPOz9Fw==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
Name

解决方案

ws://localhost:2319/ws不是有效的端点URL/URI,它想要重定向到您声明的/ws 的正确URL/URI >

这就是302重定向告诉您的情况...

Location: http://localhost:2319/ws/

假设您的WebsocketListener被声明为@ServerEndpoint("/ws"),我们在"/ws"contextPath处使用您的ServletContext,这意味着您的URL/URI访问该websocket端点是...

ws://localhost:2319/ws/ws

或者说另一种方式...

ws://<host>:<port>/<contextPath>/<endpointPath>

While connecting to REACT based WebSocket client to Java Jetty based Web Socket server, I am getting the error below -

WebSocket connection to 'ws://localhost:2319/ws' failed: Error during WebSocket handshake: Unexpected response code: 302

This error doesn't exist while connecting through Chrome's Smart Web Socket client.

I am trying to develop Web Socket Client based on REACT. Client code is -

var connection = new WebSocket('ws://localhost:2319/ws');
     connection.onopen = function () {
     // connection is opened and ready to use
    };

WebSocket Server has been based on Jetty. Server code is -

server = new Server();
    ServerConnector connector = new ServerConnector(server);
    connector.setPort(SSConstants.WWBSOCKET_PORT);
    server.addConnector(connector);

    // Setup the basic application "context" for this application at "/"
    // This is also known as the handler tree (in jetty speak)
    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.setContextPath("/ws"); // Set to "/ws" for future integration with the main jetty server.
    server.setHandler(context);

    try {
        // Initialize javax.websocket layer
        ServerContainer wsContainer = WebSocketServerContainerInitializer.configureContext(context);

        // Add WebSocket endpoint to javax.websocket layer
        wsContainer.addEndpoint(WebsocketListener.class);
        server.start();           
    }
    catch (Throwable t) {
        ssLogger.logInfo("Websocket Server start exp : ");
        t.printStackTrace(System.err);
    }

Output -

WebSocket connection to 'ws://localhost:2319/ws' failed: Error during WebSocket handshake: Unexpected response code: 302

Request URL:ws://localhost:2319/ws
Request Method:GET
Status Code:302 Found

Response Headers
view source
Content-Length:0
Date:Fri, 11 Aug 2017 18:51:42 GMT
Location:http://localhost:2319/ws/
Server:Jetty(9.3.8.v20160314)

Request Headers
view source
Accept-Encoding:gzip, deflate, br
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:Upgrade
Host:localhost:2319
Origin:https://localhost:1338
Pragma:no-cache
Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits
Sec-WebSocket-Key:2OZooIjOX7G6kgNpPOz9Fw==
Sec-WebSocket-Version:13
Upgrade:websocket
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36
Name

解决方案

ws://localhost:2319/ws is not the valid endpoint URL/URI, it wants to redirect to the correct one for your declared contextPath of /ws

That's what the 302 redirect is telling you ...

Location: http://localhost:2319/ws/

Lets say your WebsocketListener is declared as @ServerEndpoint("/ws"), and we use your ServletContext at contextPath of "/ws" that would mean your URL/URI to access that websocket endpoint is ...

ws://localhost:2319/ws/ws

Or said in a different way ...

ws://<host>:<port>/<contextPath>/<endpointPath>

这篇关于WebSocket握手期间出错:意外的响应代码:302的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆