WebSocket
00:00
WebSocket
1. WebSocket 概述
| 特性 | HTTP | WebSocket |
|---|---|---|
| 通信模式 | 请求-响应 | 全双工 |
| 连接 | 短连接 | 持久连接 |
| 服务器推送 |
2. WebSocket API
const ws = new WebSocket('wss://example.com/chat');
ws.onopen = () => {
console.log('连接已建立');
ws.send('Hello!');
};
ws.onmessage = (e) => {
console.log('收到消息:', e.data);
};
ws.onclose = (e) => {
console.log('连接关闭:', e.code);
};
ws.onerror = () => {
console.error('WebSocket 错误');
};
连接状态
| readyState | 常量 | 说明 |
|---|---|---|
| 0 | CONNECTING | 正在连接 |
| 1 | OPEN | 连接已建立 |
| 2 | CLOSING | 正在关闭 |
| 3 | CLOSED | 已关闭 |
发送与关闭
ws.send('文本消息');
ws.send(JSON.stringify({ type: 'chat', content: '你好' }));
ws.send(new ArrayBuffer(4));
ws.close(1000, '正常关闭');
3. 断线重连
class ReconnectingWebSocket {
constructor(url, options = {}) {
this.url = url;
this.retries = 0;
this.options = { reconnectInterval: 1000, ...options };
this.connect();
}
connect() {
this.ws = new WebSocket(this.url);
this.ws.onopen = (e) => {
this.retries = 0;
this.onopen?.(e);
};
this.ws.onmessage = (e) => this.onmessage?.(e);
this.ws.onclose = (e) => {
this.onclose?.(e);
const delay = Math.min(this.options.reconnectInterval * Math.pow(1.5, this.retries), 30000);
this.retries++;
setTimeout(() => this.connect(), delay);
};
}
send(data) {
if (this.ws?.readyState === WebSocket.OPEN) this.ws.send(data);
}
close() {
this.retries = Infinity;
this.ws?.close();
}
}
4. 心跳机制
setInterval(() => {
if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify({ type: 'ping' }));
}, 30000);