﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-Error-随笔分类-websocket</title><link>http://www.cppblog.com/Error/category/21182.html</link><description /><language>zh-cn</language><lastBuildDate>Mon, 22 Feb 2016 09:56:34 GMT</lastBuildDate><pubDate>Mon, 22 Feb 2016 09:56:34 GMT</pubDate><ttl>60</ttl><item><title>websocket 简介</title><link>http://www.cppblog.com/Error/archive/2016/02/22/212860.html</link><dc:creator>Enic</dc:creator><author>Enic</author><pubDate>Mon, 22 Feb 2016 09:02:00 GMT</pubDate><guid>http://www.cppblog.com/Error/archive/2016/02/22/212860.html</guid><wfw:comment>http://www.cppblog.com/Error/comments/212860.html</wfw:comment><comments>http://www.cppblog.com/Error/archive/2016/02/22/212860.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/Error/comments/commentRss/212860.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/Error/services/trackbacks/212860.html</trackback:ping><description><![CDATA[<p>WebSocket is an underlying network protocol that enables you to build other standard<br />protocols on top of it.</p>
<p>The WebSocket Protocol enables full duplex communication between a client and<br />a remote server over the Web, and supports transmission of binary data and text strings.<br />The protocol consists of an opening handshake followed by basic message framing, and is<br />layered over TCP.</p>
<p><br />WebSocket programming follows an asynchronous programming model, which<br />means that as long as a WebSocket connection is open, your application simply listens<br />for events. Your client does not need to actively poll the server for more information. To<br />start listening for the events, you simply add callback functions to the WebSocket object.<br />Alternatively, you can use the addEventListener() DOM method to add event listeners<br />to your WebSocket objects.<br />A WebSocket object dispatches four different events:<br />&#183; Open<br />&#183; Message<br />&#183; Error<br />&#183; Close<br />As with all web APIs, you can listen for these events using on&lt;eventname&gt; handler<br />properties, as well as using the addEventListener(); method.</p>
<p>ws.onopen<br />ws.onmessage<br />ws.onerror<br />ws.onclose</p>
<p>e.data instanceof Blob<br />ws.binaryType = "blob"</p>
<p>WebSocket objects have two methods: send() and close()</p>
<p><br />Listing 2-12. Waiting for the Open Event Before Sending a Message<br />// Wait until the open event before calling send().<br />var ws = new WebSocket("ws://echo.websocket.org")<br />ws.onopen = function(e) {<br />ws.send("Initial data");<br />}</p>
<p>Listing 2-13. Checking the readyState Property for an Open WebSocket<br />// Handle outgoing data. Send on a WebSocket if that socket is open.<br />function myEventHandler(data) {<br />if (ws.readyState === WebSocket.OPEN) {<br />// The socket is open, so it is ok to send the data.<br />ws.send(data);<br />} else {<br />// Do something else in this case.<br />//Possibly ignore the data or enqueue it.<br />}<br />}</p>
<p><br />// Send a Blob<br />var blob = new Blob("blob contents");<br />ws.send(blob);<br />// Send an ArrayBuffer<br />var a = new Uint8Array([8,6,7,5,3,0,9]);<br />ws.send(a.buffer);</p>
<p><br />Listing 2-15. Calling the close() Method<br />// Close the WebSocket connection<br />ws.close();<br />You can optionally pass two arguments to the close() method: code (a numerical<br />status code) and reason (a text string). Passing these arguments transmits information<br />to the server about why the client closed the connection. We will discuss the status<br />codes and reasons in greater detail in Chapter 3, when we cover the WebSocket closing<br />handshake. Listing 2-16 shows an example of calling the close() method with an<br />argument.<br />Listing 2-16. Calling the close() Method with a Reason<br />// Close the WebSocket connection because the session has ended successfully<br />ws.close(1000, "Closing normally");<br />Listing 2-16 uses code 1000, which means, as it states in the code, that the<br />connection is closing normally.</p>
<p><br />WebSocket Object Attributes<br />There are several WebSocket Object attributes you can use to provide more information<br />about the WebSocket object: readyState, bufferedAmount, and protocol.</p>
<p>Table 2-1. readyState Attributes, Values, and Status Descriptions<br />Attribute Constant&nbsp;&nbsp;&nbsp;&nbsp; Value&nbsp;&nbsp;&nbsp;&nbsp; Status<br />WebSocket.CONNECTING&nbsp;&nbsp; 0&nbsp;&nbsp; The connection is in progress but has not been established.<br />WebSocket.OPEN&nbsp;&nbsp; 1&nbsp;&nbsp; The connection has been established. Messages can flow between the client and server.<br />WebSocket.CLOSING&nbsp;&nbsp; 2&nbsp;&nbsp; The connection is going through the closing handshake.<br />WebSocket.CLOSED&nbsp;&nbsp; 3&nbsp;&nbsp; The connection has been closed or could not be opened.</p>
<p>WebSocket Object Attribute: protocol<br />The protocol attribute is the empty string before the<br />opening handshake completes and remains an empty string if the server does not choose<br />one of the protocols offered by the client.</p>
<p>&nbsp;</p><img src ="http://www.cppblog.com/Error/aggbug/212860.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/Error/" target="_blank">Enic</a> 2016-02-22 17:02 <a href="http://www.cppblog.com/Error/archive/2016/02/22/212860.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>