Csdn lidp http://blog.csdn.net/perfectpdl
The previous article introduced websocket theory. This article analyzes the implementation of the websocket protocol client on the WebKit browser engine.
For more information about websocket APIs, see RFC 6455.
The websocket implementation code on WebKit is under the WebCore/websockets directory.
The following is the definition of WebKit websocket JavaScript IDL Interface (websocket. IDL ):
module websockets { interface [ Conditional=WEB_SOCKETS, CustomConstructor, EventTarget, NoStaticTables ] WebSocket { readonly attribute DOMString URL; // ready state const unsigned short CONNECTING = 0; const unsigned short OPEN = 1; const unsigned short CLOSED = 2; readonly attribute unsigned short readyState; readonly attribute unsigned long bufferedAmount; // networking attribute EventListener onopen; attribute EventListener onmessage; attribute EventListener onclose; [Custom] boolean send(in DOMString data) raises(DOMException); void close(); // EventTarget interface [Custom] void addEventListener(in DOMString type, in EventListener listener, in boolean useCapture); [Custom] void removeEventListener(in DOMString type, in EventListener listener, in boolean useCapture); boolean dispatchEvent(in Event evt) raises(EventException); };}
The URL attribute indicates the network address of the websocket server. The protocol is generally "ws". The send method is to send data to the server, and the close method is to close the connection. In addition to these methods, there are also important events: onopen, onmessage, onerror, and onclose.
The JS interface provided by the browser is used by web developers.
JavaScript code of an instance connected to websocket
var wsServer = 'ws://localhost:8888/Demo'; var websocket = new WebSocket(wsServer); websocket.onopen = function (evt) { onOpen(evt) }; websocket.onclose = function (evt) { onClose(evt) }; websocket.onmessage = function (evt) { onMessage(evt) }; websocket.onerror = function (evt) { onError(evt) }; function onOpen(evt) { console.log("Connected to WebSocket server."); } function onClose(evt) { console.log("Disconnected"); } function onMessage(evt) { console.log('Retrieved data from server: ' + evt.data); } function onError(evt) { console.log('Error occured: ' + evt.data); } The above JavaScript code initiates a request for the client. The first is Create a websocket object and specify the server URL. the TCP request is initiated to the server when it is created. Therefore, the creation process is a blocking action, and WebKit is implemented in v8websocketcustom. in CPP, the constructorcallback function is the constructor entry. After the URL and protocol are parsed, The websocket-class connect function is called to initiate a TCP request. Websocket-> connect (URL, towebcorestring (Protocol), EC ); Void websocket: connect (const string & URL, const vector <string> & protocols, exceptioncode & EC) (websocket. cpp) The function first performs some syntax checks, such as whether the protocol is ws or WSS (secure ws), and then establishes a websocketchannel. We recommend that a websocoket channel represent the connected instance (websocketchannel. cpp ), Last call ~ The connect function provided by websocketchannel first creates a handshake object websockethandshake to save the handshake information. didcreatewebsocket-> didcreatewebsocketimpl initiates a TCP request. The client initiates a connection request when constructing a websocket object, and then specifies the server Event Callback. For example, the onopen event indicates that the connection is established and the onmessage indicates that the server has sent a message. For more information about websocket, visit here.
|