使用JMeter连接SocketCluster遇异常:WebSocket连接存服务器端问题
Got it, let's break down why your JMeter WebSocket connection returns a 101 "Switching Protocols" response but the server doesn't recognize an active user. The 101 code confirms the standard WebSocket handshake succeeded, but SocketCluster adds an extra protocol layer on top of vanilla WebSocket—you're missing that critical handshake step.
Here's how to fix this:
Send the SocketCluster-specific handshake message
Right after the WebSocket Open Connection Sampler succeeds, add a WebSocket Single Write Sampler to send the initial handshake payload. This is a JSON message that tells the server you’re a valid SocketCluster client. For most recent versions, the handshake looks like this:{"event":"#handshake","data":{"authToken":null,"clientId":"${__UUID()}","engine":"ws"}}Use JMeter's
__UUID()function to generate a uniqueclientIdeach time. If your server requires authentication, replacenullwith a validauthTokenhere.Enable detailed WebSocket logging in JMeter
To debug the exact client-server interaction, edit yourjmeter.propertiesfile to turn on debug logs:log_level.jmeter.protocol.http=DEBUG log_level.jmeter.protocol.websocket=DEBUGRun your test again and check the
jmeter.logfile. Look for confirmation that your handshake message was sent, and if the server responded with a#handshakeacknowledgment (a JSON message confirming success).Validate with the official SocketCluster client
Rule out server-side issues by testing with the official Node.js client. Create a simple script like this:const socketCluster = require('socketcluster-client'); const socket = socketCluster.create({ hostname: 'your-server-address', port: 8000, // Replace with your server port secure: false // Set to true if using wss:// }); socket.on('connect', () => { console.log('Connected successfully—server should show an active user now'); }); socket.on('error', (err) => { console.error('Client error:', err); });If this script connects and the server registers an active user, the problem is definitely that JMeter isn’t mimicking the official client’s handshake flow.
Dig into the server’s full error logs
The partial error code you mentioned (15241...) is almost certainly tied to a timeout waiting for the SocketCluster handshake. Grab the full error message from your server logs—it will likely explicitly state that no handshake was received within the allowed window, confirming our hypothesis.Double-check the WebSocket endpoint URL
Make sure your JMeter Open Connection Sampler points to the correct SocketCluster path. For SocketCluster v16+, the endpoint is usuallyws://your-host:port/socketcluster/(note the trailing slash). Older versions might use just/as the path.
内容的提问来源于stack exchange,提问作者Akandam Dharmateja

