WebSocket握手期间出错:意外的响应代码:302

8

当将基于REACT的WebSocket客户端连接到基于Java Jetty的WebSocket服务器时,我遇到了下面的错误 -

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

使用Chrome的智能Web Socket客户端连接时,不会出现此错误。

我正在尝试开发基于REACT的Web Socket客户端。客户端代码如下:

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
2个回答

7

ws://localhost:2319/ws 不是有效的端点URL/URI,它想要重定向到正确的URL以匹配你声明的contextPath/ws

这就是302重定向告诉你的信息...

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

假设你的WebsocketListener被声明为@ServerEndpoint("/ws"),并且我们使用你的ServletContext作为contextPath,值为"/ws",那么你访问该websocket端点的URL/URI应该是...

ws://localhost:2319/ws/ws

或者用不同的方式表达...

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


-1

我最近遇到了同样的问题,这是我解决的方法。

我将我的WebSocket服务器端口号从8080更改为8335,这与我的Apache服务器使用的端口号相同。

<?php
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;

require dirname(__DIR__) . '/vendor/autoload.php';

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Chat()
        )
    ),
    8335
);

$server->run();

同时,我也在我的 JavaScript 代码中进行了相同的更改以实际连接

let conn = new WebSocket('ws://localhost:8335');
conn.onopen = function(e) {
    console.log("Connection established!");

    conn.send('Hello Everyone');
};

conn.onmessage = function(e) {
    console.log(e.data);
    $("#msg").append('<br>'+e.data);
};

您还可以通过以下图片在Apache控制器中查找空闲端口。谢谢

点击Xampp控制器上的Netstart

在此处输入图片描述

单击刷新以查看活动套接字、新套接字和旧套接字

选择任何位于酒红色部分的端口

谢谢


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接