aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/websocket/error_event/index.html
blob: 8544e2f399eae1dd9f1243ac88a448eb0fae8a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
---
title: 'WebSocket: 错误事件'
slug: Web/API/WebSocket/error_event
translation_of: Web/API/WebSocket/error_event
---
<div>{{APIRef}}</div>

<p><code>websocket</code>的连接由于一些错误事件的发生 (例如无法发送一些数据)而被关闭时,一个<code>error</code>事件将被引发.</p>

<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>{{domxref("Event")}}</td>
  </tr>
  <tr>
   <th scope="row">Event handler property</th>
   <td>{{ domxref("WebSocket.onerror","onerror")}}</td>
  </tr>
 </tbody>
</table>

<h2 id="示例">示例</h2>

<pre class="brush: js notranslate">// 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);
});</pre>

<h2 id="标准">标准</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th>Specification</th>
   <th>Status</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("HTML WHATWG", "web-sockets.html#event-error", "WebSocket error")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("api.WebSocket.error_event")}}</p>

<h2 id="查看更多">查看更多</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/WebSocket/close_event">WebSocket: close event</a></li>
 <li><a href="/en-US/docs/Web/API/WebSocket/message_event">WebSocket: message event</a></li>
 <li><a href="/en-US/docs/Web/API/WebSocket/open_event">WebSocket: open event</a></li>
 <li><a href="/en-US/docs/WebSockets/Writing_WebSocket_client_applications">Writing WebSocket client applications</a></li>
</ul>