問題発生時の体系的調査方法
1. Layer 7(アプリケーション層)の問題
const debugWebSocketFrame = (event) => {
console.log('Layer 7 Debug:');
console.log('- Frame type:', event.type);
console.log('- Payload size:', event.data.length);
console.log('- Content:', event.data);
if (typeof event.data === 'string') {
try {
const parsed = JSON.parse(event.data);
console.log('- Valid JSON:', parsed);
} catch (e) {
console.log('- Invalid JSON format');
}
}
};
2. Layer 5(セッション層)の問題
const debugWebSocketSession = (ws) => {
console.log('Layer 5 Debug:');
console.log('- Ready State:', ws.readyState);
console.log('- URL:', ws.url);
console.log('- Protocol:', ws.protocol);
console.log('- Extensions:', ws.extensions);
const states = {
0: 'CONNECTING',
1: 'OPEN',
2: 'CLOSING',
3: 'CLOSED'
};
console.log('- State Name:', states[ws.readyState]);
};
3. Layer 4(トランスポート層)の問題
const analyzeNetworkLayer = () => {
console.log('Layer 4 Debug (Check in Network tab):');
console.log('- TCP connection establishment time');
console.log('- TCP congestion window size');
console.log('- Retransmission count');
console.log('- Connection timeout values');
};
4. 総合的な診断ツール
class WebSocketLayerDiagnostics {
constructor(url) {
this.url = url;
this.diagnostics = {
layer7: { errors: [], warnings: [] },
layer5: { errors: [], warnings: [] },
layer4: { errors: [], warnings: [] }
};
}
diagnose() {
const ws = new WebSocket(this.url);
ws.onopen = () => {
this.diagnostics.layer5.warnings.push('Connection established');
this.performLayer7Tests(ws);
};
ws.onerror = (error) => {
this.diagnostics.layer5.errors.push(`Connection error: ${error}`);
this.analyzeConnectionFailure();
};
ws.onclose = (event) => {
this.diagnostics.layer5.warnings.push(
`Connection closed: Code ${event.code}, Reason: ${event.reason}`
);
};
return this.diagnostics;
}
performLayer7Tests(ws) {
try {
ws.send('{"test": "json"}');
this.diagnostics.layer7.warnings.push('JSON format test passed');
} catch (e) {
this.diagnostics.layer7.errors.push('JSON format test failed');
}
try {
ws.send(new ArrayBuffer(8));
this.diagnostics.layer7.warnings.push('Binary data test passed');
} catch (e) {
this.diagnostics.layer7.errors.push('Binary data test failed');
}
}
}