我有一个使用谷歌平面缓冲区的 nodejs 应用程序。
饮料模式的 fbs 文件:
namespace MyAlcoholist;
table Drink {
drink_type_name: string;
drink_company_name: string;
drink_brand_name: string;
drink_flavor_type_name : string;
liquid_color_type_name : string;
liquid_color_is_transparent : bool;
alcohol_vol : float;
calories_for_100g : uint;
global_image_id: uint;
drink_flavor_id: uint;
}
table Drinks { drinks:[Drink]; }
root_type Drinks;
编译它flatc -s drinks.fbs
,它生成了js文件drinks_generated.js。到目前为止,一切都很好。
我在我的 nodejs 服务器中使用以下代码从数组中准备和创建平面缓冲区:
flatBuffersUtil.js
var flatbuffers = require('../js/flatbuffers').flatbuffers;
var builder = new flatbuffers.Builder();
var drinks = require('../fbs/drinks_generated').MyAlcoholist; // Generated by `flatc`.
function drinkArrayToBuffer(drinkArray) {
var drinksVectArray = [];
drinkArray.forEach(function (element, index, array) {
var drinkObj = element;
var drinkBrandName = builder.createString(drinkObj.drink_brand_name);
var drinkCompanyName = builder.createString(drinkObj.drink_company_name);
var drinkflavorTypeName = builder.createString(drinkObj.drink_flavor_type_name);
var drinkTypeName = builder.createString(drinkObj.drink_type_name);
var liquidColorTypeName = builder.createString(drinkObj.liquid_color_type_name);
drinks.Drink.startDrink(builder);
drinks.Drink.addAlcoholVol(builder, drinkObj.alcohol_vol);
drinks.Drink.addCaloriesFor100g(builder, drinkObj.calories_for_100g);
drinks.Drink.addDrinkBrandName(builder, drinkBrandName);
drinks.Drink.addDrinkCompanyName(builder, drinkCompanyName);
drinks.Drink.addDrinkFlavorId(builder, drinkObj.drink_flavor_id);
drinks.Drink.addDrinkFlavorTypeName(builder, drinkflavorTypeName);
drinks.Drink.addDrinkTypeName(builder, drinkTypeName);
drinks.Drink.addGlobalImageId(builder, drinkObj.global_image_id);
drinks.Drink.addLiquidColorIsTransparent(builder, drinkObj.is_transparent);
drinks.Drink.addLiquidColorTypeName(builder, liquidColorTypeName);
var drink = drinks.Drink.endDrink(builder);
drinksVectArray.push(drink);
})
var drinksVect = drinks.Drinks.createDrinksVector(builder, drinksVectArray);
builder.finish(drinksVect);
var buf = builder.dataBuffer();
var drinksArray = drinks.Drinks.getRootAsDrinks(buf);
return buf;
}
module.exports.drinkArrayToBuffer = drinkArrayToBuffer;
flatBuffersUtil.js 的用法:
...
var data = flatBuffersUtil.drinkArrayToBuffer(result);
res.send(data);
当我收到缓冲区时,客户端使用 angularjs 1.5.0 完成,我尝试使用以下代码从中创建一个对象:
在主索引文件中:
<script type="text/javascript" src="js/flatbuffers.js"></script>
<script type="text/javascript" src="js/fbs/drinks_generated.js"></script>
然后在角度控制器中:
$http({
data: data,
method: 'POST',
url: 'https://myalcoholist.com:8888/drink/get_list/all_drinks',
}).then(function successCallback(response) {
var buffer = response.data;
var drinks = MyAlcoholist.Drinks.getRootAsDrinks(buffer);
deferred.resolve(drinks);
// this callback will be called asynchronously
// when the response is available
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
问题是函数MyAlcoholist.Drinks.getRootAsDrinks(buffer);
失败并出现以下错误:
TypeError: bb.position is not a function
at Function.MyAlcoholist.Drinks.getRootAsDrinks (drinks_generated.js:256)
at successCallback (admin-drinks-controller.js:22)
at angular.js:15552
at m.$eval (angular.js:16820)
at m.$digest (angular.js:16636)
at m.$apply (angular.js:16928)
at g (angular.js:11266)
at t (angular.js:11464)
at XMLHttpRequest.u.onload (angular.js:11405)
现在.. 生成的drinks_generated.js 文件中的函数代码如下:
MyAlcoholist.Drinks.getRootAsDrinks = function(bb, obj) {
return (obj || new MyAlcoholist.Drinks).__init(bb.readInt32(bb.position()) + bb.position(), bb);
};
所以 bb 应该是一个带有函数 position() 的字节缓冲区类型,但我收到的是一个字节对象。我想我需要先或以某种方式对其进行类型转换......但这只是一个猜测。
有任何想法吗?
更新
好的,首先我将drinksArrayToBuffer 函数修改为以下代码:
function drinkArrayToBuffer(drinkArray) {
var drinksVectArray = [];
drinkArray.forEach(function (element, index, array) {
var drinkObj = element;
var drinkBrandName = builder.createString(drinkObj.drink_brand_name);
var drinkCompanyName = builder.createString(drinkObj.drink_company_name);
var drinkflavorTypeName = builder.createString(drinkObj.drink_flavor_type_name);
var drinkTypeName = builder.createString(drinkObj.drink_type_name);
var liquidColorTypeName = builder.createString(drinkObj.liquid_color_type_name);
drinks.Drink.startDrink(builder);
drinks.Drink.addAlcoholVol(builder, drinkObj.alcohol_vol);
drinks.Drink.addCaloriesFor100g(builder, drinkObj.calories_for_100g);
drinks.Drink.addDrinkBrandName(builder, drinkBrandName);
drinks.Drink.addDrinkCompanyName(builder, drinkCompanyName);
drinks.Drink.addDrinkFlavorId(builder, drinkObj.drink_flavor_id);
drinks.Drink.addDrinkFlavorTypeName(builder, drinkflavorTypeName);
drinks.Drink.addDrinkTypeName(builder, drinkTypeName);
drinks.Drink.addGlobalImageId(builder, drinkObj.global_image_id);
drinks.Drink.addLiquidColorIsTransparent(builder, drinkObj.is_transparent);
drinks.Drink.addLiquidColorTypeName(builder, liquidColorTypeName);
var drink = drinks.Drink.endDrink(builder);
drinksVectArray.push(drink);
})
var drinksVect = drinks.Drinks.createDrinksVector(builder, drinksVectArray);
drinks.Drinks.startDrinks(builder);
drinks.Drinks.addDrinks(builder, drinksVect);
var endDrinksOffset = drinks.Drinks.endDrinks(builder);
drinks.Drinks.finishDrinksBuffer(builder, endDrinksOffset);
var buf = builder.dataBuffer();
return buf;
}
现在在服务器端,我能够反序列化缓冲区并正确使用数据。问题在于客户端的反序列化。
在我发送缓冲区后,客户端收到以下对象:
Object
bytes_: Object
position_: 7148
__proto__: Object
现在.. 我创建了以下函数来将缓冲区转换为客户端的 json 对象:
function drinksByteArrayToArray(buffer) {
var res = [];
var byteBuffer =new flatbuffers.ByteBuffer(buffer);
var drinks = MyAlcoholist.Drinks.getRootAsDrinks(byteBuffer);
var drinksLength = drinks.drinksLength();
for (var i=0;i<drinksLength;i++) {
var drink = drinks.drinks(i);
var drinkObj = {
drink_flavor_id: drink.drinkFlavorId(),
drink_type_name: drink.drinkTypeName(),
drink_company_name: drink.drinkCompanyName(),
drink_brand_name: drink.drinkBrandName(),
drink_flavor_type_name: drink.drinkFlavorTypeName(),
liquid_color_type_name: drink.liquidColorTypeName(),
is_transparent: drink.liquidColorIsTransparent(),
alcohol_vol: drink.alcoholVol(),
calories_for_100g: drink.caloriesFor100g(),
global_image_id: drink.globalImageId
}
res.push(drinkObj);
}
return res;
}
当我执行此功能时,饮料的长度为零。所以缓冲区似乎是空的。
现在我认为问题出在以下代码上:
var byteBuffer =new flatbuffers.ByteBuffer(buffer);
我想我需要以其他方式将数据转换为字节缓冲区。
我知道我需要提供flatbuffers.ByteBuffer
一个字节数组,所以我尝试做以下事情:
var byteBuffer =new flatbuffers.ByteBuffer(buffer.bytes_);
但结果是一样的。
有任何想法吗?