Pomelo的Protobuf

时间:2023-02-13 18:36:44

  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
});
});