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 ):
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.
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.
Csdn lidp http://blog.csdn.net/perfectpdl