-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Pomelo 0.4 new features
Pomelo 0.4 makes structural changes to support friendly custom communication protocol, and provides custom data transmission strategy for different situations.
Pomelo 0.4 makes a more detailed protocol division to support more flexible custom feature. The main structure is as follow chart:
We add the protocol layer to encode/decode data packages between the upper layer and under layer. Protocol layer is mainly in charge of encoding and decoding transmission data for upper layer, and is associated with specific connector. Hybridconnector is responsible for dealing with Message layer data, and sioconnector is responsible for dealing with data from socket.io layer.
Encoding and decoding algorithm in protocol layer can be custom made by passing it to connector, code example is as follow:
var encode = function(reqId, route, msg) {
// do some customized encode with reqId, route and msg
return result; // return encode result
};
var decode = function(msg) {
// do some customized decode with msg
return result; // return decode result
};
app.configure('production|development', 'connector', function(){
app.set('connectorConfig', {
connector : pomelo.connectors.hybridconnector,
encode: encode,
decode: decode
});
});When data comes to connector, the corresponding encode and decode methods will be invoked.
-
encode(reqId, route, msg)- means encodingreqId,routeandmsg. We can know package type by checkingreqId, ifreqId > 0it represents request message, ifreqId === 0it represents push message. And this method's return value is the result of encoding message, which will be passed to transmission layer to send out. -
decode(msg)- is responsible for decoding messages from under layer.msgis the data that under layer receives, which is in accordance with the data in the same layer of client side. This method's return value is the result of decoding message, which can be handle by upper layer, and it format is : {id: id, route: route, body: body}. In this kind of message, id is the identity of message, request message's id must be positive integer, notify message does not have this field, the field route is string which means message router, the field body is object which means message body.
We can get default encode and decode method by pomelo.connectors.hybridconnector.encode/decode and pomelo.connectors.sioconnector.encode/decode.
Client side is similar to server side, which also provides corresponding custom parameters, code example is as follow:
var encode = function(reqId, route, msg) {
// do some customized encode with reqId, route and msg
return result; // return encode result
};
var decode = function(msg) {
// do some customized decode with msg
return result; // return decode result
};
pomelo.init({
host: host,
port: port,
log: true,
encode: encode,
decode: decode}, function() {
});As we can see from the chart above, there is a scheduler layer between protocol layer and transport layer.
Scheduler layer is deciding the data sending strategy, for example, the priority of sending and batch operations in broadcasting messages.
var SchedulerService = function() {
};
SchedulerService.prototype.schedule = function(route, msg, recvs, opts, cb) {
// do the schedule
};
app.configure('production|development', 'connector', function(){
app.set('schedulerConfig', {
scheduler: new SchedulerService()
});
});schedule(route, msg, recvs, opts, cb) is responsible for implementation of schedule strategy. You can use sessionService.sendMessage to send messages in schedule method.
- route - message router, and in response message this field is null.
- msg - messages after encoding.
- recvs - receivers's session.id list.
- opts - additional parameters. opts.isBroadcast means the message is need to do broadcast operation,opts.isResponse means the message is need to do response operation,opts.isPush means the message is need to do push operation.
Pomelo provides two kinds of scheduler.
-
pomelo.schedulers.direct- Default scheduler,which will send messages directly. -
pomelo.schedulers.buffer- Optional scheduler, which will cache messages and send messages in batch. You can adjust messages sending interval by modifyingflushInterval, which default value is 20ms.
In Pomelo 0.3 hybridconnector will not close the connection after heartbeat timeout. In Pomelo 0.4.2, we add the option disconnectOnTimeout, when the server side detecting heartbeat timeout it will close the client connection, code example is as follow:
app.set('connectorConfig', {
connector : pomelo.connectors.hybridconnector,
heartbeat : 3,
disconnectOnTimeout: true
});