先放上flatbuffer的github链接flatbuffer,里面可以直接下载针对模板文件生成代码exe程序和所有支持语言的库代码。
之前写的一套系统是http的,里面也用到了websocket,但是服务器都是放在国外的,国内的电信运营商对国外域名的支持很蛋疼,所以就用socket重新写了一套,针对这种复杂业务使用socket,就要自定义一套报文格式,所以这里使用了flatbuffer,但是flatbuffer在网上的资料很少,而且资料都比较浅显,所以才写这篇文章。
首先,线从github上下载exe文件和对应语言的库,或者从文章最下面下载我打包好的,分多的可以支持下~。ios比较特殊,官方没有给出库文件,后面我会单独写一篇文章。
首先将对应语言的库放到项目内,java需要将库的文件原封不动的放到项目内,包括包名也不能动,自己建一样的包路径放进去就可以,因为exe生成的类文件里面继承的库文件包名都是写死的,随便改可能会出问题而且麻烦。
下面开始创建模板文件,先新建文本文档,将名字改为demo.fbs,编辑打开,写入模板,下面是实例:

//包名
namespace com.demo;
//类名
table FbDemo {
	//名字
	name:string;
	//年龄
	age:int;
	//钱
	money:double;
	times:[string];
	test:[byte];
}
//写根类的名字
root_type FbDemo;

然后将fbs文件和exe文件放在同一目录下,执行下面命令,第一条是生成java的类,第二个是生成.net的类,自行根据语言选择。

flatc-1.11.0.exe -j ./demo.fbs
flatc-1.11.0.exe -n ./demo.fbs

执行完毕,目录会出现一个文件夹,是根据namespace定义的包的路径生成的,在最后会看到生成的FbDemo.java文件,将新生成的文件放到项目内,下面是封装的实例。

//封装
		FlatBufferBuilder fb = new FlatBufferBuilder(0);
		//string封装方法
		int nameInt = fb.createString("力口月巴犭苗");
		//一般数组类型封装方式
		int[] times = new int[3];
		for (int i = 0; i < 3; i++) {
			times[i] = fb.createString(String.valueOf(i));
		}
		int timesInt = fb.createVectorOfTables(times);
		//byte数组有单独的封装方式
		int testInt = fb.createByteVector(new byte[] {1,2,5,10});
		//封装类
		int fbDemoInt = FbDemo.createFbDemo(fb, nameInt, 1, 10D, timesInt, testInt);
		fb.finish(fbDemoInt);
		//获取封装类的字节数组
		byte[] demoArr = fb.sizedByteArray();
		
		//根据字节数组封装成类
		FbDemo demo = FbDemo.getRootAsFbDemo(ByteBuffer.wrap(demoArr));
		System.out.println(demo.name());
		System.out.println(demo.times(1) + "----" + demo.timesLength());
		System.out.println(demo.test(3)  + "----" + demo.testLength());

上面是简单的类定义和基本类型的数组封装,下面写一个自定义类的数组如何封装和定义。
下面是模板:

namespace com.demo;
table FbDemo {
	//名字
	name:string;
	//年龄
	age:int;
	//钱
	money:double;
	times:[string];
	test:[byte];
}

table FbDemoList {
	//K线数组
	DataList:[FbDemo];
}

root_type FbDemoList;

下面是代码:

int[] fbDemosInt = new int[2];
		//封装
		FlatBufferBuilder fb = new FlatBufferBuilder(0);
		//string封装方法
		int nameInt = fb.createString("力口月巴犭苗1");
		//一般数组类型封装方式
		int[] times = new int[3];
		for (int i = 0; i < 3; i++) {
			int a = i + 3;
			times[i] = fb.createString(String.valueOf(a));
		}
		int timesInt = fb.createVectorOfTables(times);
		//byte数组有单独的封装方式
		int testInt = fb.createByteVector(new byte[] {6,7,11});
		//封装类
		fbDemosInt[0] = FbDemo.createFbDemo(fb, nameInt, 2, 10.5D, timesInt, testInt);
		//string封装方法
		nameInt = fb.createString("力口月巴犭苗2");
		//一般数组类型封装方式
		times = new int[3];
		for (int i = 0; i < 3; i++) {
			times[i] = fb.createString(String.valueOf(i));
		}
		timesInt = fb.createVectorOfTables(times);
		//byte数组有单独的封装方式
		testInt = fb.createByteVector(new byte[] {1,2,5,10});
		//封装类
		fbDemosInt[1] = FbDemo.createFbDemo(fb, nameInt, 1, 10D, timesInt, testInt);
		int fbDemoListInt = FbDemoList.createFbDemoList(fb, fb.createVectorOfTables(fbDemosInt));
		
		fb.finish(fbDemoListInt);
		
		//获取封装类的字节数组
		byte[] demoArr = fb.sizedByteArray();
		//根据字节数组封装成类
		FbDemoList demoList = FbDemoList.getRootAsFbDemoList(ByteBuffer.wrap(demoArr));
		FbDemo demo = null;
		for (int i = 0; i < demoList.DataListLength(); i++) {
			demo = demoList.DataList(i);
			System.out.println(demo.name());
			System.out.println(demo.times(1) + "----" + demo.timesLength());
			System.out.println(demo.test(3)  + "----" + demo.testLength());
		}

以上就是flatbuffer的基础使用,下一篇讲解如何从FbDemoList数组中提取其中一个类的字节数组,还有根据字节封装类的另一种写法。