• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Error

            C++博客 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
              217 Posts :: 61 Stories :: 32 Comments :: 0 Trackbacks

            WebSocket is an underlying network protocol that enables you to build other standard
            protocols on top of it.

            The WebSocket Protocol enables full duplex communication between a client and
            a remote server over the Web, and supports transmission of binary data and text strings.
            The protocol consists of an opening handshake followed by basic message framing, and is
            layered over TCP.


            WebSocket programming follows an asynchronous programming model, which
            means that as long as a WebSocket connection is open, your application simply listens
            for events. Your client does not need to actively poll the server for more information. To
            start listening for the events, you simply add callback functions to the WebSocket object.
            Alternatively, you can use the addEventListener() DOM method to add event listeners
            to your WebSocket objects.
            A WebSocket object dispatches four different events:
            · Open
            · Message
            · Error
            · Close
            As with all web APIs, you can listen for these events using on<eventname> handler
            properties, as well as using the addEventListener(); method.

            ws.onopen
            ws.onmessage
            ws.onerror
            ws.onclose

            e.data instanceof Blob
            ws.binaryType = "blob"

            WebSocket objects have two methods: send() and close()


            Listing 2-12. Waiting for the Open Event Before Sending a Message
            // Wait until the open event before calling send().
            var ws = new WebSocket("ws://echo.websocket.org")
            ws.onopen = function(e) {
            ws.send("Initial data");
            }

            Listing 2-13. Checking the readyState Property for an Open WebSocket
            // Handle outgoing data. Send on a WebSocket if that socket is open.
            function myEventHandler(data) {
            if (ws.readyState === WebSocket.OPEN) {
            // The socket is open, so it is ok to send the data.
            ws.send(data);
            } else {
            // Do something else in this case.
            //Possibly ignore the data or enqueue it.
            }
            }


            // Send a Blob
            var blob = new Blob("blob contents");
            ws.send(blob);
            // Send an ArrayBuffer
            var a = new Uint8Array([8,6,7,5,3,0,9]);
            ws.send(a.buffer);


            Listing 2-15. Calling the close() Method
            // Close the WebSocket connection
            ws.close();
            You can optionally pass two arguments to the close() method: code (a numerical
            status code) and reason (a text string). Passing these arguments transmits information
            to the server about why the client closed the connection. We will discuss the status
            codes and reasons in greater detail in Chapter 3, when we cover the WebSocket closing
            handshake. Listing 2-16 shows an example of calling the close() method with an
            argument.
            Listing 2-16. Calling the close() Method with a Reason
            // Close the WebSocket connection because the session has ended successfully
            ws.close(1000, "Closing normally");
            Listing 2-16 uses code 1000, which means, as it states in the code, that the
            connection is closing normally.


            WebSocket Object Attributes
            There are several WebSocket Object attributes you can use to provide more information
            about the WebSocket object: readyState, bufferedAmount, and protocol.

            Table 2-1. readyState Attributes, Values, and Status Descriptions
            Attribute Constant     Value     Status
            WebSocket.CONNECTING   0   The connection is in progress but has not been established.
            WebSocket.OPEN   1   The connection has been established. Messages can flow between the client and server.
            WebSocket.CLOSING   2   The connection is going through the closing handshake.
            WebSocket.CLOSED   3   The connection has been closed or could not be opened.

            WebSocket Object Attribute: protocol
            The protocol attribute is the empty string before the
            opening handshake completes and remains an empty string if the server does not choose
            one of the protocols offered by the client.

             

            posted on 2016-02-22 17:02 Enic 閱讀(276) 評(píng)論(0)  編輯 收藏 引用 所屬分類: websocket

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 国产精品成人99久久久久| 国产Av激情久久无码天堂| 69久久夜色精品国产69| 久久综合久久综合久久| 无码国内精品久久人妻| 午夜欧美精品久久久久久久| 精品免费久久久久久久| 93精91精品国产综合久久香蕉 | 久久婷婷五月综合国产尤物app| 一本色道久久99一综合| 久久精品99久久香蕉国产色戒| 2021久久精品国产99国产精品| 狠狠色丁香婷婷综合久久来来去| 久久综合鬼色88久久精品综合自在自线噜噜| 久久久久久亚洲精品不卡| 成人午夜精品无码区久久| 国产亚洲欧美成人久久片| 狠狠精品干练久久久无码中文字幕 | 久久99国产精品二区不卡| 国产综合成人久久大片91| 国产成人精品三上悠亚久久| 久久这里只有精品久久| 亚洲午夜久久久久久噜噜噜| 久久精品国产影库免费看 | 97精品伊人久久大香线蕉| 国产亚洲精久久久久久无码| 久久免费观看视频| 51久久夜色精品国产| 国产精品女同久久久久电影院| 伊人久久大香线蕉综合网站 | 青青青伊人色综合久久| 亚洲国产精品无码久久一区二区 | 模特私拍国产精品久久| 久久综合综合久久狠狠狠97色88| 一本色综合网久久| 久久国产欧美日韩精品| 久久人人添人人爽添人人片牛牛 | 18禁黄久久久AAA片| 久久乐国产精品亚洲综合| 久久99精品久久久久久不卡|