pomelo的protobuf實現,借助了javascript的動態性,使得應用程序可以在運行時解析proto文件,不需要進行proto文件的編譯。pomelo的實現中,為了更方便地解析proto文件,使用了json格式,與原生的proto文件語法是相通的,但是是不相同的。用戶定義好客戶端以及服務端的通信所需要的信息格式的proto文件,服務端的proto配置放在config/serverProtos.json中,客戶端的proto配置放在config/clientProtos.json。如果在其配置文件里,配置了所有類型的proto信息,那么在通信過程中,將會全部使用二進制的方式對消息進行編碼; 如果沒有定義某一類消息相應的proto,pomelo還是會使用初始的json格式對消息進行編
// clientProtos.json { "chat.chatHandler.send": { "required string rid": 1, "required string content": 2, "required string from": 3, "required string target": 4 }, "connector.entryHandler.enter": { "required string username": 1, "required string rid": 2 }, "gate.gateHandler.queryEntry": { "required string uid": 1 } } // serverProtos.json { "onChat": { "required string msg": 1, "required string from": 2, "required string target": 3 }, "onLeave": { "required string user": 1 }, "onAdd": { "required string user": 1 } }
在app.js中配置:
app.configure('production|development', 'connector', function() { app.set('connectorConfig', { connector: pomelo.connectors.hybridconnector, heartbeat: 3, useDict: true, useProtobuf: true //enable useProtobuf }); }); app.configure('production|development', 'gate', function(){ app.set('connectorConfig', { connector : pomelo.connectors.hybridconnector, useDict: true, useProtobuf: true //enable useProtobuf }); });