mozilla

版本 238849 / WebSockets

  • 版本网址缩略名: WebSockets
  • 版本标题: WebSockets
  • 版本 id: 238849
  • 创建于:
  • 创建者: karsa.si
  • 是否是当前版本?
  • 评论 12 words added, 211 words removed

修订内容

WebSockets is a technology that makes it possible to open an interactive communication session between the user's browser and a server.

WebSocket 技术使得浏览器直接与服务器端的程序通过socket通讯成为可能。

Using a WebSocket connection, Web applications can perform real-time communication instead of having to poll for changes back and forth.

使用WebSocket链接,Web应用程序可以实时的推送或者获取信息。

Availability of WebSockets

目前,WebSockets还在完善之中,浏览器支持还未普及,甚至浏览器的不同版本支持情况也不同。(请使用最新的firefox浏览器,翻译这篇文章的时候是13.0)

 

Creating a WebSocket object 建立一个WebSocket对象

In order to communicate using the WebSocket protocol, you need to create a WebSocket object; this will automatically attempt to open the connection to the server.

使用websocket的第一步是建立一个WebSocket 对象。

WebSocket WebSocket(
  in DOMString url,
  in optional DOMString protocols
);

WebSocket WebSocket(
  in DOMString url,
  in optional DOMString[] protocols
);
url
The URL to which to connect; this should be the URL to which the WebSocket server will respond.
protocols Optional
Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols, so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to be able to handle different types of interactions depending on the specified protocol). If you don't specify a protocol string, an empty string is assumed.

The constructor can throw exceptions:

SECURITY_ERR
The port to which the connection is being attempted is being blocked.

Connection errors

Edit section

If an error occurs while attempting to connect, first a simple event with the name "error" is sent to the WebSocket object (thereby invoking its onerror handler), and then the CloseEvent is sent to the WebSocket object (thereby invoking its onclose handler) to indicate the reason for the connection's closing.

As of Firefox 11 however, it is typical to receive a descriptive error message in the console from the Mozilla platform, and a closing code as defined in RFC 6455, Section 7.4 through the CloseEvent.

ExamplesEdit section

这例子建立一个链接往www.example.com的链接。

var exampleSocket = new WebSocket("ws://www.example.com/socketserver", "protocolOne");  

if(exampleSocket.readyState == OPEN)

Sending data to the server

Edit section

Once you've opened your connection, you can begin transmitting data to the server. To do this, simply call the WebSocket object's send() method for each message you want to send:

  1. exampleSocket.send("Here's some text that the server is urgently awaiting!");  

You can send data as a string, Blob , or ArrayBuffer.

Note: Prior to version 11, Firefox only supported sending data as a string.

As establishing a connection is asynchronous and prone to failure there is no guarantee that calling the send() method immediately after creating a WebSocket object will be successful. We can at least be sure that attempting to send data only takes place once a connection is established by defining an onopen handler to do the work:

  1. exampleSocket.onopen = function (event) {  
  2.   exampleSocket.send("Here's some text that the server is urgently awaiting!");   
  3. };  

Using JSON to transmit objects

Edit section

One handy thing you can do is use JSON to send reasonably complex data to the server. For example, a chat program can interact with a server using a protocol implemented using packets of JSON-encapsulated data:

  1. // Send text to all users through the server  
  2. function sendText() {  
  3.   // Construct a msg object containing the data the server needs to process the message from the chat client.  
  4.   var msg = {  
  5.     type: "message",  
  6.     text: document.getElementById("text").value,  
  7.     id:   clientID,  
  8.     date: Date.now()  
  9.   };  
  10.   
  11.   // Send the msg object as a JSON-formatted string.  
  12.   exampleSocket.send(JSON.stringify(msg));  
  13.     
  14.   // Blank the text input element, ready to receive the next line of text from the user.  
  15.   document.getElementById("text").value = "";  
  16. }  

Receiving messages from the server

Edit section

WebSockets is an event-driven API; when messages are received, a "message" event is delivered to the onmessage function. To begin listening for incoming data, you can do something like this:

  1. exampleSocket.onmessage = function (event) {  
  2.   console.log(event.data);  
  3. }  

Receiving and interpreting JSON objects

Edit section

Let's consider the chat client application first alluded to in Using JSON to transmit objects. There are assorted types of data packets the client might receive, such as:

  • Login handshake
  • Message text
  • User list updates

The code that interprets these incoming messages might look like this:

  1. exampleSocket.onmessage = function(event) {  
  2.   var f = document.getElementById("chatbox").contentDocument;  
  3.   var text = "";  
  4.   var msg = JSON.parse(event.data);  
  5.   var time = new Date(msg.date);  
  6.   var timeStr = time.toLocaleTimeString();  
  7.     
  8.   switch(msg.type) {  
  9.     case "id":  
  10.       clientID = msg.id;  
  11.       setUsername();  
  12.       break;  
  13.     case "username":  
  14.       text = "<b>User <em>" + msg.name + "</em> signed in at " + timeStr + "</b><br>";  
  15.       break;  
  16.     case "message":  
  17.       text = "(" + timeStr + ") <b>" + msg.name + "</b>: " + msg.text + "<br>";  
  18.       break;  
  19.     case "rejectusername":  
  20.       text = "<b>Your username has been set to <em>" + msg.name + "</em> because the name you chose is in use.</b><br>"  
  21.       break;  
  22.     case "userlist":  
  23.       var ul = "";  
  24.       for (i=0; i < msg.users.length; i++) {  
  25.         ul += msg.users[i] + "<br>";  
  26.       }  
  27.       document.getElementById("userlistbox").innerHTML = ul;  
  28.       break;  
  29.   }  
  30.     
  31.   if (text.length) {  
  32.     f.write(text);  
  33.     document.getElementById("chatbox").contentWindow.scrollByPages(1);  
  34.   }  
  35. };  

Here we use JSON.parse() to convert the JSON object back into the original object, then examine and act upon its contents.

Text data format

Edit section

Text received over a WebSocket connection is in UTF-8 format.

Prior to Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6) , certain non-characters in otherwise valid UTF-8 text would cause the connection to be terminated. Now Gecko permits these values.

Closing the connection

Edit section

When you've finished using the WebSocket connection, call the WebSocket method close():

  1. exampleSocket.close();  

It may be helpful to examine the socket's bufferedAmount attribute before attempting to close the connection to determine if any data has yet to be transmitted on the network.

修订版来源

<p>WebSockets is a technology that makes it possible to open an interactive communication session between the user's browser and a server.</p>
<p>WebSocket 技术使得浏览器直接与服务器端的程序通过socket通讯成为可能。</p>
<p>Using a WebSocket connection, Web applications can perform real-time communication instead of having to poll for changes back and forth.</p>
<p>使用WebSocket链接,Web应用程序可以实时的推送或者获取信息。</p>
<div id="section_1"> <h2 class="editable"><span>Availability of WebSockets</span></h2> <p>目前,WebSockets还在完善之中,浏览器支持还未普及,甚至浏览器的不同版本支持情况也不同。(请使用最新的firefox浏览器,翻译这篇文章的时候是13.0)</p> <p> </p>
</div>
<div id="section_2"> <h2 class="editable"><span>Creating a WebSocket object 建立一个WebSocket对象</span></h2> <p>In order to communicate using the WebSocket protocol, you need to create a <a href="/en/WebSockets/WebSockets_reference/WebSocket" rel="internal" title="en/WebSockets/WebSockets reference/WebSocket"><code>WebSocket</code></a> object; this will automatically attempt to open the connection to the server.</p> <p>使用websocket的第一步是建立一个WebSocket 对象。</p> <pre>WebSocket WebSocket(
  in DOMString url,
  in optional DOMString protocols
);

WebSocket WebSocket(
  in DOMString url,
  in optional DOMString[] protocols
);
</pre> <dl> <dt><code>url</code></dt> <dd>The URL to which to connect; this should be the URL to which the WebSocket server will respond.</dd> <dt><code>protocols</code> <span style="border: 1px solid #9ED2A4; background-color: #C0FFC7; font-size: x-small; white-space: nowrap; padding: 2px;" title="">Optional</span></dt> <dd>Either a single protocol string or an array of protocol strings. These strings are used to indicate sub-protocols, so that a single server can implement multiple WebSocket sub-protocols (for example, you might want one server to be able to handle different types of interactions depending on the specified <code>protocol</code>). If you don't specify a protocol string, an empty string is assumed.</dd> </dl> <p>The constructor can throw exceptions:</p> <dl> <dt><code>SECURITY_ERR</code></dt> <dd>The port to which the connection is being attempted is being blocked.</dd> </dl> <div id="section_3"> <h3 class="editable"><span>Connection errors</span></h3> <div class="editIcon" style="visibility: hidden;"> <h3 class="editable"><a href="/en/WebSockets/Writing_WebSocket_client_applications?action=edit&amp;sectionId=3" title="Edit section"><span class="icon"><img alt="Edit section" class="sectionedit" src="/skins/common/icons/icon-trans.gif"></span></a></h3> </div> <p>If an error occurs while attempting to connect, first a simple event with the name "error" is sent to the <a href="/en/WebSockets/WebSockets_reference/WebSocket" rel="internal" title="WebSocket"><code>WebSocket</code></a> object (thereby invoking its <code>onerror</code> handler), and then the <a href="/en/WebSockets/WebSockets_reference/CloseEvent" rel="internal" title="CloseEvent"><code>CloseEvent</code></a> is sent to the <a href="/en/WebSockets/WebSockets_reference/WebSocket" rel="internal" title="WebSocket"><code>WebSocket</code></a> object (thereby invoking its <code>onclose</code> handler) to indicate the reason for the connection's closing.</p> <p>As of Firefox 11 however, it is typical to receive a descriptive error message in the console from the Mozilla platform, and a closing code as defined in <a class="external" href="http://tools.ietf.org/html/rfc6455#section-7.4" rel="external" target="_blank" title="RFC 6455 Section 7.4">RFC 6455, Section 7.4</a> through the <a href="/en/WebSockets/WebSockets_reference/CloseEvent" rel="internal" title="CloseEvent"><code>CloseEvent</code></a>.</p> </div> <div id="section_4"> <h3 class="editable"><span>Examples</span><a href="/en/WebSockets/Writing_WebSocket_client_applications?action=edit&amp;sectionId=4" title="Edit section"><span class="icon"><img alt="Edit section" class="sectionedit" src="/skins/common/icons/icon-trans.gif"></span></a></h3> <p>这例子建立一个链接往<code>www.example.com的链接。</code></p> <p>var exampleSocket = new WebSocket("<a class=" external" href="ws://www.example.com/socketserver" rel="freelink">ws://www.example.com/socketserver</a>", "protocolOne");  <br> <br> if(exampleSocket.readyState == OPEN)</p> </div>
</div>
<div id="section_5"> <h2 class="editable"><span>Sending data to the server</span></h2> <div class="editIcon" style="visibility: hidden;"> <h2 class="editable"><a href="/en/WebSockets/Writing_WebSocket_client_applications?action=edit&amp;sectionId=5" title="Edit section"><span class="icon"><img alt="Edit section" class="sectionedit" src="/skins/common/icons/icon-trans.gif"></span></a></h2> </div> <p>Once you've opened your connection, you can begin transmitting data to the server. To do this, simply call the <code>WebSocket</code> object's <a href="/en/WebSockets/WebSockets_reference/WebSocket#send%28%29" rel="internal" title="en/WebSockets/WebSockets reference/WebSocket#send()"><code>send()</code></a> method for each message you want to send:</p> <div class="dp-highlighter"> <div class="bar"> <div class="tools"><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">view plain</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">print</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">?</a></div> </div> <ol class="dp-c" start="1"> <li class="alt"><span><span>exampleSocket.send(</span><span class="string">"Here's some text that the server is urgently awaiting!"</span><span>);  </span></span></li> </ol> </div> <p>You can send data as a string, <code><a href="/en/DOM/Blob" rel="custom">Blob</a></code> , or <a href="/en/JavaScript_typed_arrays/ArrayBuffer" rel="internal" title="en/JavaScript typed arrays/ArrayBuffer"><code>ArrayBuffer</code></a>.</p> <div class="note"><strong>Note:</strong> Prior to version 11, Firefox only supported sending data as a string.</div> <p>As establishing a connection is asynchronous and prone to failure there is no guarantee that calling the <code>send()</code> method immediately after creating a WebSocket object will be successful. We can at least be sure that attempting to send data only takes place once a connection is established by defining an <code>onopen</code> handler to do the work:</p> <div class="dp-highlighter"> <div class="bar"> <div class="tools"><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">view plain</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">print</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">?</a></div> </div> <ol class="dp-c" start="1"> <li class="alt"><span><span>exampleSocket.onopen = </span><span class="keyword">function</span><span> (event) {  </span></span></li> <li><span>  exampleSocket.send(<span class="string">"Here's some text that the server is urgently awaiting!"</span><span>);   </span></span></li> <li class="alt"><span>};  </span></li> </ol> </div> <div id="section_6"> <h3 class="editable"><span>Using JSON to transmit objects</span></h3> <div class="editIcon"> <h3 class="editable"><a href="/en/WebSockets/Writing_WebSocket_client_applications?action=edit&amp;sectionId=6" title="Edit section"><span class="icon"><img alt="Edit section" class="sectionedit" src="/skins/common/icons/icon-trans.gif"></span></a></h3> </div> <p>One handy thing you can do is use <a href="/en/JSON" rel="internal" title="en/JSON">JSON</a> to send reasonably complex data to the server. For example, a chat program can interact with a server using a protocol implemented using packets of JSON-encapsulated data:</p> <div class="dp-highlighter"> <div class="bar"> <div class="tools"><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">view plain</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">print</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">?</a></div> </div> <ol class="dp-c" start="1"> <li class="alt"><span><span class="comment">// Send text to all users through the server</span><span>  </span></span></li> <li><span><span class="keyword">function</span><span> sendText() {  </span></span></li> <li class="alt"><span>  <span class="comment">// Construct a msg object containing the data the server needs to process the message from the chat client.</span><span>  </span></span></li> <li><span>  <span class="keyword">var</span><span> msg = {  </span></span></li> <li class="alt"><span>    type: <span class="string">"message"</span><span>,  </span></span></li> <li><span>    text: document.getElementById(<span class="string">"text"</span><span>).value,  </span></span></li> <li class="alt"><span>    id:   clientID,  </span></li> <li><span>    date: Date.now()  </span></li> <li class="alt"><span>  };  </span></li> <li><span>  </span></li> <li class="alt"><span>  <span class="comment">// Send the msg object as a JSON-formatted string.</span><span>  </span></span></li> <li><span>  exampleSocket.send(JSON.stringify(msg));  </span></li> <li class="alt"><span>    </span></li> <li><span>  <span class="comment">// Blank the text input element, ready to receive the next line of text from the user.</span><span>  </span></span></li> <li class="alt"><span>  document.getElementById(<span class="string">"text"</span><span>).value = </span><span class="string">""</span><span>;  </span></span></li> <li><span>}  </span></li> </ol> </div> </div>
</div>
<div id="section_7"> <h2 class="editable"><span>Receiving messages from the server</span></h2> <div class="editIcon" style="visibility: hidden;"> <h2 class="editable"><a href="/en/WebSockets/Writing_WebSocket_client_applications?action=edit&amp;sectionId=7" title="Edit section"><span class="icon"><img alt="Edit section" class="sectionedit" src="/skins/common/icons/icon-trans.gif"></span></a></h2> </div> <p>WebSockets is an event-driven API; when messages are received, a "message" event is delivered to the <code>onmessage</code> function. To begin listening for incoming data, you can do something like this:</p> <div class="dp-highlighter"> <div class="bar"> <div class="tools"><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">view plain</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">print</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">?</a></div> </div> <ol class="dp-c" start="1"> <li class="alt"><span><span>exampleSocket.onmessage = </span><span class="keyword">function</span><span> (event) {  </span></span></li> <li><span>  console.log(event.data);  </span></li> <li class="alt"><span>}  </span></li> </ol> </div> <div id="section_8"> <h3 class="editable"><span>Receiving and interpreting JSON objects</span></h3> <div class="editIcon"> <h3 class="editable"><a href="/en/WebSockets/Writing_WebSocket_client_applications?action=edit&amp;sectionId=8" title="Edit section"><span class="icon"><img alt="Edit section" class="sectionedit" src="/skins/common/icons/icon-trans.gif"></span></a></h3> </div> <p>Let's consider the chat client application first alluded to in <a href="/en/WebSockets/Writing_WebSocket_client_applications#Using_JSON_to_transmit_objects" rel="custom">Using JSON to transmit objects</a>. There are assorted types of data packets the client might receive, such as:</p> <ul> <li>Login handshake</li> <li>Message text</li> <li>User list updates</li> </ul> <p>The code that interprets these incoming messages might look like this:</p> <div class="dp-highlighter"> <div class="bar"> <div class="tools"><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">view plain</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">print</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">?</a></div> </div> <ol class="dp-c" start="1"> <li class="alt"><span><span>exampleSocket.onmessage = </span><span class="keyword">function</span><span>(event) {  </span></span></li> <li><span>  <span class="keyword">var</span><span> f = document.getElementById(</span><span class="string">"chatbox"</span><span>).contentDocument;  </span></span></li> <li class="alt"><span>  <span class="keyword">var</span><span> text = </span><span class="string">""</span><span>;  </span></span></li> <li><span>  <span class="keyword">var</span><span> msg = JSON.parse(event.data);  </span></span></li> <li class="alt"><span>  <span class="keyword">var</span><span> time = </span><span class="keyword">new</span><span> Date(msg.date);  </span></span></li> <li><span>  <span class="keyword">var</span><span> timeStr = time.toLocaleTimeString();  </span></span></li> <li class="alt"><span>    </span></li> <li><span>  <span class="keyword">switch</span><span>(msg.type) {  </span></span></li> <li class="alt"><span>    <span class="keyword">case</span><span> </span><span class="string">"id"</span><span>:  </span></span></li> <li><span>      clientID = msg.id;  </span></li> <li class="alt"><span>      setUsername();  </span></li> <li><span>      <span class="keyword">break</span><span>;  </span></span></li> <li class="alt"><span>    <span class="keyword">case</span><span> </span><span class="string">"username"</span><span>:  </span></span></li> <li><span>      text = <span class="string">"&lt;b&gt;User &lt;em&gt;"</span><span> + msg.name + </span><span class="string">"&lt;/em&gt; signed in at "</span><span> + timeStr + </span><span class="string">"&lt;/b&gt;&lt;br&gt;"</span><span>;  </span></span></li> <li class="alt"><span>      <span class="keyword">break</span><span>;  </span></span></li> <li><span>    <span class="keyword">case</span><span> </span><span class="string">"message"</span><span>:  </span></span></li> <li class="alt"><span>      text = <span class="string">"("</span><span> + timeStr + </span><span class="string">") &lt;b&gt;"</span><span> + msg.name + </span><span class="string">"&lt;/b&gt;: "</span><span> + msg.text + </span><span class="string">"&lt;br&gt;"</span><span>;  </span></span></li> <li><span>      <span class="keyword">break</span><span>;  </span></span></li> <li class="alt"><span>    <span class="keyword">case</span><span> </span><span class="string">"rejectusername"</span><span>:  </span></span></li> <li><span>      text = <span class="string">"&lt;b&gt;Your username has been set to &lt;em&gt;"</span><span> + msg.name + </span><span class="string">"&lt;/em&gt; because the name you chose is in use.&lt;/b&gt;&lt;br&gt;"</span><span>  </span></span></li> <li class="alt"><span>      <span class="keyword">break</span><span>;  </span></span></li> <li><span>    <span class="keyword">case</span><span> </span><span class="string">"userlist"</span><span>:  </span></span></li> <li class="alt"><span>      <span class="keyword">var</span><span> ul = </span><span class="string">""</span><span>;  </span></span></li> <li><span>      <span class="keyword">for</span><span> (i=0; i &lt; msg.users.length; i++) {  </span></span></li> <li class="alt"><span>        ul += msg.users[i] + <span class="string">"&lt;br&gt;"</span><span>;  </span></span></li> <li><span>      }  </span></li> <li class="alt"><span>      document.getElementById(<span class="string">"userlistbox"</span><span>).innerHTML = ul;  </span></span></li> <li><span>      <span class="keyword">break</span><span>;  </span></span></li> <li class="alt"><span>  }  </span></li> <li><span>    </span></li> <li class="alt"><span>  <span class="keyword">if</span><span> (text.length) {  </span></span></li> <li><span>    f.write(text);  </span></li> <li class="alt"><span>    document.getElementById(<span class="string">"chatbox"</span><span>).contentWindow.scrollByPages(1);  </span></span></li> <li><span>  }  </span></li> <li class="alt"><span>};  </span></li> </ol> </div> <p>Here we use <a href="/en/JavaScript/Reference/Global_Objects/JSON/parse" rel="internal" title="en/JavaScript/Reference/Global Objects/JSON/parse"><code>JSON.parse()</code></a> to convert the JSON object back into the original object, then examine and act upon its contents.</p> </div> <div id="section_9"> <h3 class="editable"><span>Text data format</span></h3> <div class="editIcon"> <h3 class="editable"><a href="/en/WebSockets/Writing_WebSocket_client_applications?action=edit&amp;sectionId=9" title="Edit section"><span class="icon"><img alt="Edit section" class="sectionedit" src="/skins/common/icons/icon-trans.gif"></span></a></h3> </div> <p>Text received over a WebSocket connection is in UTF-8 format.</p> <p>Prior to Gecko 9.0 (Firefox 9.0 / Thunderbird 9.0 / SeaMonkey 2.6) , certain non-characters in otherwise valid UTF-8 text would cause the connection to be terminated. Now Gecko permits these values.</p> </div>
</div>
<div id="section_10"> <h2 class="editable"><span>Closing the connection</span></h2> <div class="editIcon"> <h2 class="editable"><a href="/en/WebSockets/Writing_WebSocket_client_applications?action=edit&amp;sectionId=10" title="Edit section"><span class="icon"><img alt="Edit section" class="sectionedit" src="/skins/common/icons/icon-trans.gif"></span></a></h2> </div> <p>When you've finished using the WebSocket connection, call the WebSocket method <a href="/en/WebSockets/WebSockets_reference/WebSocket#close%28%29" rel="internal" title="en/WebSockets/WebSockets reference/WebSocket#close()"><code>close()</code></a>:</p> <div class="dp-highlighter"> <div class="bar"> <div class="tools"><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">view plain</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">print</a><a href="/en/WebSockets/Writing_WebSocket_client_applications#" title="en/WebSockets/Writing_WebSocket_client_applications#">?</a></div> </div> <ol class="dp-c" start="1"> <li class="alt"><span><span>exampleSocket.close();  </span></span></li> </ol> </div> <p>It may be helpful to examine the socket's <code>bufferedAmount</code> attribute before attempting to close the connection to determine if any data has yet to be transmitted on the network.</p>
</div>
恢复到这个版本