Error during WebSocket handshake: Unexpected response code: 302 Error during WebSocket handshake: Unexpected response code: 302 json json

Error during WebSocket handshake: Unexpected response code: 302


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>


I recently faced the same issue and this is how I was able to resolve mine.

I changed the port number for my websocket server from 8080 to 8335 which was the same is the same port for my Apache server.

<?phpuse 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();

And also made the same changes in my javascript code for the actual connection

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

You can also lookup for free ports in the Apache controller by following the images below. Thanks

click on Netstart on the Xampp Controller

enter image description here

Click to refresh to view Active Socket, New Sockets and Old Sockets

Select any of the ports that falls in the burgundy section

Thank you