Websocket Connection Failed: Error During Websocket Handshake: Unexpected Response Code: 400

WebSocket connection failed: Error during WebSocket handshake: Unexpected response code: 400

Problem solved! I just figured out how to solve the issue, but I would still like to know if this is normal behavior or not.

It seems that even though the Websocket connection establishes correctly (indicated by the 101 Switching Protocols request), it still defaults to long-polling. The fix was as simple as adding this option to the Socket.io connection function:

{transports: ['websocket']}

So the code finally looks like this:

const app = express();
const server = http.createServer(app);
var io = require('socket.io')(server);

io.on('connection', function(socket) {
console.log('connected socket!');

socket.on('greet', function(data) {
console.log(data);
socket.emit('respond', { hello: 'Hey, Mr.Client!' });
});
socket.on('disconnect', function() {
console.log('Socket disconnected');
});
});

and on the client:

var socket = io('ws://localhost:3000', {transports: ['websocket']});
socket.on('connect', function () {
console.log('connected!');
socket.emit('greet', { message: 'Hello Mr.Server!' });
});

socket.on('respond', function (data) {
console.log(data);
});

And the messages now appear as frames:

working websockets

This Github issue pointed me in the right direction. Thanks to everyone who helped out!

"Error during WebSocket handshake: Unexpected response code: 400" while recording application using JMeter

As of current JMeter version 5.2.1 JMeter cannot either capture or properly proxy the WebSocket traffic so I would recommend splitting the recording in 2 parts:

  1. Before the login (including the login)
  2. After the login (to wit: login without proxy, then turn on proxy, then continue recording)

In any case all the WebSocket calls needs to be captured separately using a 3rd-party sniffer tool like Fiddler or Wireshark and then properly simulated using JMeter WebSocket Samplers plugin



Related Topics



Leave a reply



Submit