diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/websocket | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/websocket')
-rw-r--r-- | files/zh-cn/web/api/websocket/bufferedamount/index.html | 49 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/close/index.html | 58 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/error_event/index.html | 71 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/extensions/index.html | 43 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/index.html | 157 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/message_event/index.html | 76 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/onclose/index.html | 37 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/onerror/index.html | 45 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/onmessage/index.html | 49 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/onopen/index.html | 41 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/protocol/index.html | 41 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/readystate/index.html | 60 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/send/index.html | 78 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/url/index.html | 51 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/websocket/index.html | 50 | ||||
-rw-r--r-- | files/zh-cn/web/api/websocket/二进制类型/index.html | 56 |
16 files changed, 962 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/websocket/bufferedamount/index.html b/files/zh-cn/web/api/websocket/bufferedamount/index.html new file mode 100644 index 0000000000..4162fa92f2 --- /dev/null +++ b/files/zh-cn/web/api/websocket/bufferedamount/index.html @@ -0,0 +1,49 @@ +--- +title: WebSocket.bufferedAmount +slug: Web/API/WebSocket/bufferedAmount +tags: + - API + - Property + - Reference + - Web API + - WebSocket +translation_of: Web/API/WebSocket/bufferedAmount +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<p><strong><code>WebSocket.bufferedAmount</code>是一个只读属性,用于返回已经被</strong>{{manch("send")}}方法放入队列中但还没有被发送到网络中的数据的字节数。一旦队列中的所有数据被发送至网络,则该属性值将被重置为0。但是,若在发送过程中连接被关闭,则属性值不会重置为0。如果你不断地调用{{manch("send")}},则该属性值会持续增长</p> + +<p>The <strong><code>WebSocket.bufferedAmount</code></strong> read-only property returns the number of bytes of data that have been queued using calls to {{manch("send")}} but not yet transmitted to the network. This value resets to zero once all queued data has been sent. This value does not reset to zero when the connection is closed; if you keep calling {{manch("send")}}, this will continue to climb.</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox"><em>var bufferedAmount</em> = aWebSocket.bufferedAmount;</pre> + +<h2 id="Value">Value</h2> + +<p>An <code>unsigned long</code>.</p> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#dom-websocket-bufferedamount', 'WebSocket: bufferedAmount')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.WebSocket.bufferedAmount")}}</p> diff --git a/files/zh-cn/web/api/websocket/close/index.html b/files/zh-cn/web/api/websocket/close/index.html new file mode 100644 index 0000000000..046dd5c079 --- /dev/null +++ b/files/zh-cn/web/api/websocket/close/index.html @@ -0,0 +1,58 @@ +--- +title: WebSocket.close() +slug: Web/API/WebSocket/close +translation_of: Web/API/WebSocket/close +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<p><strong><code>WebSocket.close()</code></strong> 方法关闭 {{domxref("WebSocket")}} 连接或连接尝试(如果有的话)。 如果连接已经关闭,则此方法不执行任何操作。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">WebSocket.close();</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>code</code> {{optional_inline}}</dt> + <dd>一个数字状态码,它解释了连接关闭的原因。如果没有传这个参数,默认使用1005。{{domxref("CloseEvent")}}的允许的状态码见<a href="https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes">状态码列表</a> 。</dd> + <dt><code>reason</code> {{optional_inline}}</dt> + <dd>一个人类可读的字符串,它解释了连接关闭的原因。这个UTF-8编码的字符串不能超过123个字节。</dd> +</dl> + +<h3 id="抛出的异常">抛出的异常</h3> + +<dl> + <dt><code>INVALID_ACCESS_ERR</code></dt> + <dd>一个无效的<code>code</code></dd> + <dt><code>SYNTAX_ERR</code></dt> + <dd><code>reason</code> 字符串太长(超过123字节)</dd> +</dl> + +<div class="note"> +<p><strong>注意:</strong> 在Gecko 8.0 {{geckoRelease("8.0")}}之前版本的Gecko里,这个方法不支持传参数。</p> +</div> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th>Specification</th> + <th>Status</th> + <th>Comment</th> + </tr> + <tr> + <td><a class="external external-icon" href="https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket-close" hreflang="en" lang="en">HTML Living Standard<br> + <small lang="en-US">The definition of 'WebSocket.close()' in that specification.</small></a></td> + <td><span class="spec-Living">Living Standard</span></td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.WebSocket.close")}}</p> diff --git a/files/zh-cn/web/api/websocket/error_event/index.html b/files/zh-cn/web/api/websocket/error_event/index.html new file mode 100644 index 0000000000..8544e2f399 --- /dev/null +++ b/files/zh-cn/web/api/websocket/error_event/index.html @@ -0,0 +1,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> diff --git a/files/zh-cn/web/api/websocket/extensions/index.html b/files/zh-cn/web/api/websocket/extensions/index.html new file mode 100644 index 0000000000..27a66590cc --- /dev/null +++ b/files/zh-cn/web/api/websocket/extensions/index.html @@ -0,0 +1,43 @@ +--- +title: WebSocket.extensions +slug: Web/API/WebSocket/extensions +translation_of: Web/API/WebSocket/extensions +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<p><strong><code>WebSocket.extensions</code></strong>是只读属性,返回服务器已选择的扩展值。目前,链接可以协定的扩展值只有空字符串或者一个扩展列表。</p> + +<p>The <strong><code>WebSocket.extensions</code></strong> read-only property returns the extensions selected by the server. This is currently only the empty string or a list of extensions as negotiated by the connection.</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>var extensions</em> = aWebSocket.extensions;</pre> + +<h2 id="返回值">返回值</h2> + +<p>A {{domxref("DOMString")}}.</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#dom-websocket-extensions', 'WebSocket: extensions')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.WebSocket.extensions")}}</p> diff --git a/files/zh-cn/web/api/websocket/index.html b/files/zh-cn/web/api/websocket/index.html new file mode 100644 index 0000000000..dc534d928a --- /dev/null +++ b/files/zh-cn/web/api/websocket/index.html @@ -0,0 +1,157 @@ +--- +title: WebSocket +slug: Web/API/WebSocket +tags: + - API + - WebSocket + - WebSockets +translation_of: Web/API/WebSocket +--- +<div>{{APIRef("Web Sockets API")}}</div> + +<p><code>WebSocket</code> 对象提供了用于创建和管理 <a href="/zh-CN/docs/Web/API/WebSockets_API" title="zh-CN/WebSockets">WebSocket</a> 连接,以及可以通过该连接发送和接收数据的 API。</p> + +<p>使用<code><a href="/en-US/docs/Web/API/WebSocket/WebSocket"> WebSocket()</a></code><a href="/en-US/docs/Web/API/WebSocket/WebSocket"> </a>构造函数来构造一个<code> WebSocket</code> 。</p> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{domxref("WebSocket.WebSocket", "WebSocket(url[, protocols])")}}</dt> + <dd>返回一个 <code>WebSocket</code> 对象。</dd> +</dl> + +<h2 id="常量">常量</h2> + +<table class="standard-table"> + <tbody> + <tr> + <td><strong>Constant</strong></td> + <td><strong>Value</strong></td> + </tr> + <tr> + <td><code>WebSocket.CONNECTING</code></td> + <td><code>0</code></td> + </tr> + <tr> + <td><code>WebSocket.OPEN</code></td> + <td><code>1</code></td> + </tr> + <tr> + <td><code>WebSocket.CLOSING</code></td> + <td><code>2</code></td> + </tr> + <tr> + <td><code>WebSocket.CLOSED</code></td> + <td><code>3</code></td> + </tr> + </tbody> +</table> + +<h2 id="属性">属性</h2> + +<dl> + <dt>{{domxref("WebSocket.binaryType")}}</dt> + <dd>使用二进制的数据类型连接。</dd> + <dt>{{domxref("WebSocket.bufferedAmount")}} {{readonlyinline}}</dt> + <dd>未发送至服务器的字节数。</dd> +</dl> + +<dl> + <dt>{{domxref("WebSocket.extensions")}} {{readonlyinline}}</dt> + <dd>服务器选择的扩展。</dd> +</dl> + +<dl> + <dt>{{domxref("WebSocket.onclose")}}</dt> + <dd>用于指定连接关闭后的回调函数。</dd> + <dt>{{domxref("WebSocket.onerror")}}</dt> + <dd>用于指定连接失败后的回调函数。</dd> + <dt>{{domxref("WebSocket.onmessage")}}</dt> + <dd>用于指定当从服务器接受到信息时的回调函数。</dd> + <dt>{{domxref("WebSocket.onopen")}}</dt> + <dd>用于指定连接成功后的回调函数。</dd> + <dt>{{domxref("WebSocket.protocol")}} {{readonlyinline}}</dt> + <dd>服务器选择的下属协议。</dd> + <dt>{{domxref("WebSocket.readyState")}} {{readonlyinline}}</dt> + <dd>当前的链接状态。</dd> + <dt>{{domxref("WebSocket.url")}} {{readonlyinline}}</dt> + <dd>WebSocket 的绝对路径。</dd> +</dl> + +<h2 id="Method_overview" name="Method_overview">方法</h2> + +<dl> + <dt>{{domxref("WebSocket.close", "WebSocket.close([code[, reason]])")}}</dt> + <dd>关闭当前链接。</dd> + <dt>{{domxref("WebSocket.send", "WebSocket.send(data)")}}</dt> + <dd><span>对要传输的数据进行排队</span>。</dd> +</dl> + +<h2 id="事件">事件</h2> + +<p>使用 <code>addEventListener()</code> 或将一个事件监听器赋值给本接口的 <code>on<em>eventname</em></code> 属性,来监听下面的事件。</p> + +<dl> + <dt>{{domxref("WebSocket/close_event", "close")}}</dt> + <dd>当一个 <code>WebSocket</code> 连接被关闭时触发。<br> + 也可以通过 {{domxref("WebSocket/onclose", "onclose")}} 属性来设置。</dd> + <dt>{{domxref("WebSocket/error_event", "error")}}</dt> + <dd>当一个 <code>WebSocket</code> 连接因错误而关闭时触发,例如无法发送数据时。<br> + 也可以通过 {{domxref("WebSocket/onerror", "onerror")}} 属性来设置.</dd> + <dt>{{domxref("WebSocket/message_event", "message")}}</dt> + <dd>当通过 <code>WebSocket</code> 收到数据时触发。<br> + 也可以通过 {{domxref("WebSocket/onmessage", "onmessage")}} 属性来设置。</dd> + <dt>{{domxref("WebSocket/open_event", "open")}}</dt> + <dd>当一个 <code>WebSocket</code> 连接成功时触发。<br> + 也可以通过 {{domxref("WebSocket/onopen", "onopen")}} 属性来设置。</dd> +</dl> + +<h2 id="Attributes" name="Attributes">示例</h2> + +<pre class="brush: js line-numbers language-js">// Create WebSocket connection. +const socket = new WebSocket('ws://localhost:8080'); + +// Connection opened +socket.addEventListener('open', function (event) { + socket.send('Hello Server!'); +}); + +// Listen for messages +socket.addEventListener('message', function (event) { + console.log('Message from server ', event.data); +}); +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <td>规范</td> + <td>状态</td> + <td>注释</td> + </tr> + <tr> + <td>{{SpecName("HTML WHATWG", "web-sockets.html#the-websocket-interface", "WebSocket")}}</td> + <td>{{Spec2("HTML WHATWG")}}</td> + <td>初始定义</td> + </tr> + </tbody> +</table> + +<ul> +</ul> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.WebSocket")}}</p> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li><a href="/zh-CN/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications" title="/zh-CN/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications">Writing WebSocket client applications</a></li> +</ul> + +<p> + <audio style="display: none;"></audio> +</p> diff --git a/files/zh-cn/web/api/websocket/message_event/index.html b/files/zh-cn/web/api/websocket/message_event/index.html new file mode 100644 index 0000000000..350aac9424 --- /dev/null +++ b/files/zh-cn/web/api/websocket/message_event/index.html @@ -0,0 +1,76 @@ +--- +title: 'WebSocket: message event' +slug: Web/API/WebSocket/message_event +translation_of: Web/API/WebSocket/message_event +--- +<div> +<p>{{APIRef}}</p> + +<p><code>message</code> 事件会在 <code>WebSocket</code> 接收到新消息时被触发。</p> + +<table> + <tbody> + <tr> + <th scope="row">起泡(Bubbles)</th> + <td>不</td> + </tr> + <tr> + <th scope="row">可取消</th> + <td>不</td> + </tr> + <tr> + <th scope="row">接口</th> + <td>{{domxref("MessageEvent")}}</td> + </tr> + <tr> + <th scope="row">事件处理程序属性</th> + <td>{{ domxref("WebSocket.onmessage","onmessage")}}</td> + </tr> + </tbody> +</table> + +<h2 id="例子">例子</h2> + +<pre>// 创建一个 WebSocket 连接 +const socket = new WebSocket('ws://localhost:8080'); + +// 监听消息 +socket.addEventListener('message', function (event) { + console.log('Message from server ', event.data); +});</pre> + +<h2 id="规范">规范</h2> + +<table> + <thead> + <tr> + <th>规范</th> + <th>状态</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName("HTML WHATWG", "web-sockets.html#event-message", "WebSocket message")}}</td> + <td>{{Spec2("HTML WHATWG")}}</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.WebSocket.message_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/error_event">WebSocket: error 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> + <li></li> +</ul> +</div> + +<ul> + <li></li> +</ul> diff --git a/files/zh-cn/web/api/websocket/onclose/index.html b/files/zh-cn/web/api/websocket/onclose/index.html new file mode 100644 index 0000000000..0ed1b7ffe1 --- /dev/null +++ b/files/zh-cn/web/api/websocket/onclose/index.html @@ -0,0 +1,37 @@ +--- +title: WebSocket.onclose +slug: Web/API/WebSocket/onclose +translation_of: Web/API/WebSocket/onclose +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<p><strong><code>WebSocket.onclose</code></strong> 属性返回一个事件监听器,这个事件监听器将在 WebSocket 连接的{{domxref("WebSocket.readyState","readyState")}} 变为 <code>CLOSED</code>时被调用,它接收一个名字为“close”的 {{domxref("CloseEvent")}} 事件。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>WebSocket</em>.onclose = function(event) { + console.log("WebSocket is closed now."); +};</pre> + +<h2 id="值">值</h2> + +<p>一个{{domxref("EventListener")}}.</p> + +<h2 id="文档">文档</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#handler-websocket-onclose', 'WebSocket: onclose')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> diff --git a/files/zh-cn/web/api/websocket/onerror/index.html b/files/zh-cn/web/api/websocket/onerror/index.html new file mode 100644 index 0000000000..996ad55472 --- /dev/null +++ b/files/zh-cn/web/api/websocket/onerror/index.html @@ -0,0 +1,45 @@ +--- +title: WebSocket.onerror +slug: Web/API/WebSocket/onerror +translation_of: Web/API/WebSocket/onerror +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<p>The <strong><code>WebSocket.onerror</code></strong> property returns the event listener to be called when an error occurs. This is a simple event named "error".</p> + +<p>在 <strong><code>WebSocket.onerror </code></strong>属性中,你可以定义一个发生错误时执行的回调函数,此事件的事件名为"error"</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>WebSocket</em>.onerror = function(event) { + console.error("WebSocket error observed:", event); +};</pre> + +<h2 id="值">值</h2> + +<p>An {{domxref("EventListener")}}.</p> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#handler-websocket-onerror', 'WebSocket: onerror')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.WebSocket.onerror")}}</p> diff --git a/files/zh-cn/web/api/websocket/onmessage/index.html b/files/zh-cn/web/api/websocket/onmessage/index.html new file mode 100644 index 0000000000..27bcce6cf2 --- /dev/null +++ b/files/zh-cn/web/api/websocket/onmessage/index.html @@ -0,0 +1,49 @@ +--- +title: WebSocket.onmessage +slug: Web/API/WebSocket/onmessage +tags: + - API + - Web API + - WebSocket + - 参考 + - 属性 +translation_of: Web/API/WebSocket/onmessage +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<p><code><strong>WebSocket.onmessage</strong></code> 属性是一个当收到来自服务器的消息时被调用的 {{domxref("EventHandler")}}。它由一个{{domxref("MessageEvent")}}调用。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>aWebSocket</em>.onmessage = function(event) { + console.debug("WebSocket message received:", event); +};</pre> + +<h2 id="值">值</h2> + +<p>一个 {{domxref("EventListener")}}.</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#handler-websocket-onmessage', 'WebSocket: onmessage')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>初始定义</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.WebSocket.onmessage")}}</p> diff --git a/files/zh-cn/web/api/websocket/onopen/index.html b/files/zh-cn/web/api/websocket/onopen/index.html new file mode 100644 index 0000000000..067c9057bc --- /dev/null +++ b/files/zh-cn/web/api/websocket/onopen/index.html @@ -0,0 +1,41 @@ +--- +title: WebSocket.onopen +slug: Web/API/WebSocket/onopen +translation_of: Web/API/WebSocket/onopen +--- +<p><strong><code>WebSocket.onopen</code></strong>属性定义一个事件处理程序,当{{domxref("WebSocket")}} 的连接状态{{domxref("WebSocket.readyState","readyState")}} 变为{{domxref("WebSocket.readyState","1")}}时调用;这意味着当前连接已经准备好发送和接受数据。这个事件处理程序通过 {{domxref("事件")}}(建立连接时)触发。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate"><em>aWebSocket</em>.onopen = function(event) { + console.log("WebSocket is open now."); +};</pre> + +<h2 id="返回值">返回值</h2> + +<p>An {{domxref("EventListener")}}.</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#handler-websocket-onopen', 'WebSocket: onopen')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.WebSocket.onopen")}}</p> diff --git a/files/zh-cn/web/api/websocket/protocol/index.html b/files/zh-cn/web/api/websocket/protocol/index.html new file mode 100644 index 0000000000..8a575f1180 --- /dev/null +++ b/files/zh-cn/web/api/websocket/protocol/index.html @@ -0,0 +1,41 @@ +--- +title: WebSocket.protocol +slug: Web/API/WebSocket/protocol +translation_of: Web/API/WebSocket/protocol +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<p><strong><code>WebSocket.protocol</code></strong> 是个只读属性,用于返回服务器端选中的子协议的名字;这是一个在创建{{domxref("WebSocket")}} 对象时,在参数{{domxref("<code>protocols</code>")}}中指定的字符串,当没有已建立的链接时为空串。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">var protocol = <em>aWebSocket</em>.protocol;</pre> + +<h2 id="返回值">返回值</h2> + +<p><a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMString" title="DOMString is a UTF-16 String. As JavaScript already uses such strings, DOMString is mapped directly to a String."><code>DOMString</code></a>.</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#dom-websocket-protocol', 'WebSocket: protocol')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.WebSocket.protocol")}}</p> diff --git a/files/zh-cn/web/api/websocket/readystate/index.html b/files/zh-cn/web/api/websocket/readystate/index.html new file mode 100644 index 0000000000..e4d7bd23fb --- /dev/null +++ b/files/zh-cn/web/api/websocket/readystate/index.html @@ -0,0 +1,60 @@ +--- +title: WebSocket.readyState +slug: Web/API/WebSocket/readyState +tags: + - Web API + - WebSocket +translation_of: Web/API/WebSocket/readyState +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<h2 id="概要">概要</h2> + +<p>返回当前 {{domxref("WebSocket")}} 的链接状态,只读。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox notranslate">var readyState = <em>WebSocket</em>.readyState;</pre> + +<h2 id="值">值</h2> + +<p>以下其中之一</p> + +<dl> + <dt>0 (<code>WebSocket.CONNECTING</code>)</dt> + <dd>正在链接中</dd> + <dt>1 (<code>WebSocket.OPEN</code>)</dt> + <dd>已经链接并且可以通讯</dd> + <dt>2 (<code>WebSocket.CLOSING</code>)</dt> + <dd>连接正在关闭</dd> + <dt>3 (<code>WebSocket.CLOSED</code>)</dt> + <dd>连接已关闭或者没有链接成功</dd> +</dl> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th>Specification</th> + <th>Status</th> + <th>Comment</th> + </tr> + <tr> + <td><a class="external external-icon" href="https://html.spec.whatwg.org/multipage/web-sockets.html#dom-websocket-readystate" hreflang="en" lang="en">HTML Living Standard<br> + <small lang="en-US">The definition of 'WebSocket.readyState' in that specification.</small></a></td> + <td><span class="spec-Living">Living Standard</span></td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + + + +<p>{{Compat("api.WebSocket.readyState")}}</p> + +<p> + <audio style="display: none;"></audio> +</p> diff --git a/files/zh-cn/web/api/websocket/send/index.html b/files/zh-cn/web/api/websocket/send/index.html new file mode 100644 index 0000000000..495da7a3e8 --- /dev/null +++ b/files/zh-cn/web/api/websocket/send/index.html @@ -0,0 +1,78 @@ +--- +title: WebSocket.send() +slug: Web/API/WebSocket/send +tags: + - API + - Web API + - WebSocket + - 引用 + - 方法 +translation_of: Web/API/WebSocket/send +--- +<p>{{APIRef("Web Sockets API")}}</p> + + + +<p> <strong><code>WebSocket.send()</code></strong> 方法将需要通过 WebSocket 链接传输至服务器的数据排入队列,并根据所需要传输的data bytes的大小来增加 <code>bufferedAmount</code>的值 。若数据无法传输(例如数据需要缓存而缓冲区已满)时,套接字会自行关闭。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">WebSocket.send("Hello server!");</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>data</code></dt> + <dd>用于传输至服务器的数据。它必须是以下类型之一: + <dl> + <dt>{{domxref("USVString")}}</dt> + <dd>文本字符串。字符串将以 UTF-8 格式添加到缓冲区,并且 <code>bufferedAmount</code> 将加上该字符串以 UTF-8 格式编码时的字节数的值。</dd> + <dt>{{domxref("ArrayBuffer")}}</dt> + <dd>您可以使用一有类型的数组对象发送底层二进制数据;其二进制数据内存将被缓存于缓冲区,<code>bufferedAmount</code> 将加上所需字节数的值。</dd> + <dt>{{domxref("Blob")}}</dt> + <dd><code>Blob</code> 类型将队列 blob 中的原始数据以二进制中传输。 <code>bufferedAmount</code> 将加上原始数据的字节数的值。</dd> + <dt>{{domxref("ArrayBufferView")}}</dt> + <dd>您可以以二进制帧的形式发送任何 <a href="/zh-CN/docs/Web/JavaScript/Typed_arrays">JavaScript 类数组对象</a> ;其二进制数据内容将被队列于缓冲区中。值 <code>bufferedAmount</code> 将加上必要字节数的值。</dd> + </dl> + </dd> +</dl> + +<h3 id="异常">异常</h3> + +<dl> + <dt><code>INVALID_STATE_ERR</code></dt> + <dd>当前连接未处于 <code>OPEN</code> 状态。</dd> + <dt><code>SYNTAX_ERR</code></dt> + <dd>数据是一个包含未配对代理(unpaired surrogates)的字符串。</dd> +</dl> + +<div class="note"> +<p><strong>Note:</strong> Gecko在{{Gecko("6.0")}}中对 <code>send()</code> 方法的实现与规范有些不一致; Gecko 返回一个<code>boolean</code> 来告知当前连接是否依旧处于<code>OPEN </code>状态 (同时也可以使用该返回值来判定数据是否已经被完全缓存或者传输); 这个问题在 {{Gecko("8.0")}}中被修正.</p> + +<p>而 {{Gecko("11.0")}}, 支持 {{jsxref("ArrayBuffer")}} 类型但不支持 {{domxref("Blob")}}类型.</p> +</div> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">注释</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#dom-websocket-send', 'WebSocket: send')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>初始定义</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.WebSocket.send")}}</p> diff --git a/files/zh-cn/web/api/websocket/url/index.html b/files/zh-cn/web/api/websocket/url/index.html new file mode 100644 index 0000000000..e28d09a725 --- /dev/null +++ b/files/zh-cn/web/api/websocket/url/index.html @@ -0,0 +1,51 @@ +--- +title: WebSocket.url +slug: Web/API/WebSocket/url +tags: + - API + - Property + - Refrence + - Web API + - WebSocket +translation_of: Web/API/WebSocket/url +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<p>The <strong><code>WebSocket.url</code></strong> read-only property returns the absolute URL of the {{domxref("WebSocket")}} as resolved by the constructor.</p> + +<p><strong><code>WebSocket.url</code></strong>是一个只读属性,返回值为当构造函数创建{{domxref("WebSocket")}}实例对象时URL的绝对路径。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>var url</em> = aWebSocket.url;</pre> + +<p> </p> + +<h2 id="返回值">返回值</h2> + +<p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMString" title="DOMString is a UTF-16 String. As JavaScript already uses such strings, DOMString is mapped directly to a String."><code>DOMString</code></a>.</p> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#dom-websocket-url', 'WebSocket: url')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.WebSocket.url")}}</p> diff --git a/files/zh-cn/web/api/websocket/websocket/index.html b/files/zh-cn/web/api/websocket/websocket/index.html new file mode 100644 index 0000000000..20977c22ec --- /dev/null +++ b/files/zh-cn/web/api/websocket/websocket/index.html @@ -0,0 +1,50 @@ +--- +title: WebSocket() +slug: Web/API/WebSocket/WebSocket +translation_of: Web/API/WebSocket/WebSocket +--- +<p>{{APIRef("Web Sockets API")}}<br> + <code><strong>WebSocket()</strong></code>构造函器会返回一个 {{domxref("WebSocket")}} 对象。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>aWebSocket</em> = new WebSocket(<em>url</em> [, protocols]);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>url</code></dt> + <dd>要连接的URL;这应该是WebSocket服务器将响应的URL。</dd> + <dt><code>protocols</code> {{optional_inline}}</dt> + <dd>一个协议字符串或者一个包含协议字符串的数组。这些字符串用于指定子协议,这样单个服务器可以实现多个WebSocket子协议(例如,您可能希望一台服务器能够根据指定的协议(<code>protocol</code>)处理不同类型的交互)。如果不指定协议字符串,则假定为空字符串。</dd> +</dl> + +<h3 id="抛出异常">抛出异常</h3> + +<dl> + <dt><code>SECURITY_ERR</code></dt> + <dd>正在尝试连接的端口被阻止。</dd> +</dl> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th><font><font>规范</font></font></th> + <th><font><font>状态</font></font></th> + <th><font><font>注释</font></font></th> + </tr> + <tr> + <td>{{SpecName('HTML WHATWG', '#dom-websocket', 'the WebSocket constructor')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.WebSocket.WebSocket")}}</p> diff --git a/files/zh-cn/web/api/websocket/二进制类型/index.html b/files/zh-cn/web/api/websocket/二进制类型/index.html new file mode 100644 index 0000000000..aad8e48fc2 --- /dev/null +++ b/files/zh-cn/web/api/websocket/二进制类型/index.html @@ -0,0 +1,56 @@ +--- +title: WebSocket.binaryType +slug: Web/API/WebSocket/二进制类型 +tags: + - 参考 + - 属性 + - 应用编程接口 + - 网页套接字 + - 网页接口 +translation_of: Web/API/WebSocket/binaryType +--- +<p>{{APIRef("Web Sockets API")}}</p> + +<p><strong><code>WebSocket.binaryType</code></strong> 返回websocket连接所传输二进制数据的类型。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox"><em>var binaryType</em> = aWebSocket.binaryType;</pre> + +<h2 id="返回值">返回值</h2> + +<p> 一条{{DOMXref("DOMString")}}:</p> + +<dl> + <dt><code>"blob"</code></dt> + <dd>如果传输的是 {{domxref("Blob")}} 类型的数据。</dd> + <dt><code>"arraybuffer"</code></dt> + <dd>如果传输的是 {{jsxref("ArrayBuffer")}} 类型的数据。 + <p> </p> + </dd> +</dl> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('HTML WHATWG', '#dom-websocket-binarytype', 'WebSocket: binaryType')}}</td> + <td>{{Spec2('HTML WHATWG')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.WebSocket.binaryType")}}</p> |