What is the significance of the WebSocket close codes?
ws.onclose = (event) => {
switch(event.code) {
case 1000:
console.log('Normal closure');
break;
case 1001:
console.log('Going Away');
break;
case 1006:
console.log('Abnormal closure');
break;
case 1015:
console.log('TLS handshake failure');
break;
default:
console.log(`Unknown close code: ${event.code}`);
}
};
WebSocket close codes provide crucial information about why a connection was terminated: 1) 1000: Normal closure (clean shutdown), 2) 1001: Endpoint 'going away' (server shutdown/browser page close), 3) 1002-1015: Various protocol and application-level errors, 4) 4000-4999: Reserved for application use. Understanding these codes is essential for proper error handling, debugging, and implementing appropriate reconnection strategies.