When using Pomelo for server development, no matter what the client, as long as the server can follow the online protocol, you can establish communication with the server. Pomelo built-in Sioconnector and hybridconnector all define their own protocol format, Where Sioconnector is used for socket.io communication, Hybridconnector is used to handle connection traffic between WebSocket and TCP. To facilitate client development, Pomelo provides a subset of the platform's client SDKs, including the SDK for Web-side JavaScript, and the use of Libpomelo based on the C + + language development.
"Supplemental" hybrid means hybridization, in which Hybridconnector refers to advanced connector. We know that connector is responsible for the network communication task with the client in the Pomelo architecture. To understand the word hybrid, look at a small story: a car with a hybrid logo, which means a hybrid car, a vehicle with a hybrid of an internal combustion engine and electric power, which is obviously a high-level type in a similar vehicle. The new browser will support HTML5 format Web design, natural hybridconnector to the application to handle WebSocket (next generation) and TCP type of connection communication.
Web-side JavaScript Development Library
for browsers, HTML5 already supports websocket, so using a browser that supports websocket can establish communication directly with the Hybridconnector on the server. For older browsers, there is no support for websocket, and you can use the Socket.io-based approach to establish a connection to the server. Therefore, for the web side, Pomelo provides two sets of development libraries, respectively for WebSocket-enabled browsers and browsers that do not support websocket, and the links to the two development libraries are as follows. Pomelo-jsclient-socket.io for Socket.io and Pomelo-jsclient-websocket for WebSocket.
Introduction to Web-side APIs
Both Socket.io and WebSocket provide a unified API that provides a brief introduction to these APIs.
Pomelo.init (params, CB)
This is often the first call of the client, the params should indicate the server to be connected to the IP and port number, CB will be successful after the connection callback;
Pomelo.request (Route, MSG, CB)
The request service, route for the server-side routing, the format of "..", msg for the requested content, CB will respond back to the callback;
Pomelo.notify (route, MSG)
Send notify, do not need the server back response, so there is no callback for the response, other parameters meaning the same as request;
Pomelo.on (route, CB)
This is a method inherited from Eventemmiter, which responds to the push of the server. Route will be user-defined, the format is generally "OnXXX";
Pomelo.disconnect ()
This is the method of pomelo active disconnection.
Chatofpomelo-websocket instance client-related code
NetEase's chatofpomelo-websocket example shows a simple client browser-based chat network program, but it is important to understand the pomelo architecture. Below, we simply enumerate the API-related code in the client-side key script client.js, corresponding to the above.
function queryentry (uid, callback) { var route = ' Gate.gateHandler.queryEntry '; pomelo.init ({ host: window.location.hostname, port: 3014, log: true }, function () { Pomelo.request (route, { uid: uid }, function (data) { pomelo.disconnect (); if (data.code === 500) { showerror (Login_error); return; } callback ( Data.host, data.port); }); });
Pomelo.on (' Onchat ', function (data) {AddMessage (Data.from, Data.target, data.msg); $ ("#chatHistory"). Show (); if (data.from!== username) tip (' message ', data.from); }); We do not introduce the above code in detail here. However, to provide an understanding of the above code you must have a little bit of jquery-based front-end Web development experience.
Summary
From NetEase's chatofpomelo-websocket example, NetEase pomelo provides a web-based front API that is highly encapsulated and easy to use. It's just pathetic. I'm not open yet. Choose to use Web-based online web development. However, we are more interested in the use of Libpomelo based on C + + language development, and in later articles I will choose the opportunity to introduce this case article.
This article is from the "Green Peak" blog, please make sure to keep this source http://zhuxianzhong.blog.51cto.com/157061/1615109
Introduction to the Web Client development API in Pomelo development