当websocket的连接由于一些错误事件的发生 (例如无法发送一些数据) 而被关闭时,一个error事件将被引发。
<table class="properties"> <tbody> <tr> <th scope="row">Bubbles</th> <td>No</td> </tr> <tr> <th scope="row">Cancelable</th> <td>No</td> </tr> <tr> <th scope="row">Interface</th> <td>Event</td> </tr> <tr> <th scope="row">Event handler property</th> <td>WebSocket.onerror</td> </tr> </tbody> </table>
示例
// Create WebSocket connection
// 创建一个 WebSocket 连接
const socket = new WebSocket("ws://localhost:8080");
// Listen for possible errors
// 监听可能发生的错误
socket.addEventListener("error", function (event) {
console.log("WebSocket error: ", event);
});
规范
浏览器兼容性
查看更多
- WebSocket: close event
- WebSocket: message event
- WebSocket: open event
- Writing WebSocket client applications
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
