Websocket is an important feature of HTML5 and provides two-way communication between the client and server. It is the first choice for many webapps for network communication, but it is not supported in Android WebKit, the socket. i/O popularity. Of course, this feature cannot be missed in our cloud OS. My task this week is to support websocket in the cloud OS WebKit so that webapps can directly call the websocket API.
WebKit (such as Android) on some platforms does not support websocket, but does not support WebKit. However, this feature requires porting at the specific network layer of each platform. Therefore, you only need to complete the read and write operations at the network layer. First look at the source code WebCore/modules/websockets, and websocket-relatedCodeIn this folder, the structure is as follows:
Through websocket. CPP, websocket. H and websocket. IDL packages websocket JS APIs, so in JS, new websocket ("... ",... ");, It is reflected that WebKit is:
Websocket. cpp-> websocketchannel. cpp-> websocketframe. cpp, websockethandshake. cpp-> socketstreamhandle
Specifically, websocket. cpp is the js api packaging, websocketchannel. cpp is the channel packaging, websocketframe. cpp and websockethandshake. cpp process the websocket upper layer protocol, and socketstreamhandle processes the network stream. After socketstreamhandle reads data, it passes the following command:
Socketstreamhandle-> socketstreamhandleclient-> websocketchannelclient
This path sends the message to the onmessage callback in JS, where websocketchannel. CPP is the implementation of socketstreamhandleclient, websocket. CPP is the implementation of websocketchannelclient, so it seems that the structure and information loop of the entire lower layer of websocket are clear.
After analyzing the WebKit code, we will find that WebKit has completed the websocket shelf and protocol, but lacks the Data Reading and Writing in socketstreamhandle, and confirms my initial judgment. Check socketstreamhandlebase. H and socketstreamhandlebase. CPP will find that you only need to inherit socketstreamhandlebase and implement the Virtual Methods platformsend and platformclose. In addition, after reading the data, call socketstreamhandleclient. didreceivesocketstreamdata sends data to the upper layer, so that the entire websocket is complete.
It is not difficult to implement these methods. You need to pay attention to the need for Asynchronous Network-based read/write. You can use epoll or select directly. If you are a newbie or have cross-platform requirements, you can use libevent, I just used this library, and the specific code will not be pasted. The standard libevent is sufficient for reading and writing.
After completing the websocket, you can not only provide websocket support in JS APIs, but also provide websocket client capabilities on native. Unlike JS, native does not have a JS execution environment, websocket is not required either. CPP packaging, so it should be directly from websocketchannel. CPP starts to use, for example:
Refptr <websocketchannel> wschannel = websocketchannel: Create (page-> mainframe ()-> document (), Channel); kurl url = kurl (), string ("WS: // 127.0.0.1: 9223 "); wschannel-> connect (URL, string (" WebKit "));
The channel here is an implementation of websocketchannelclient. In the JS environment, this implementation is websocket. CPP, which needs to be implemented independently on the native end. The important thing is to implement the didreceivemessage method. This is the message callback after websocket protocol parsing, which is equivalent to using the onmessage method in Js.
Other websocket parts will not be mentioned. If you are interested in the protocol, you can focus on websocketframe. CPP and websockethandshake. CPP. For more information about WebKit IDL, see websocket. CPP, websocket. H and websocket. IDL. In short, the small websocket still has a lot of dry goods.