protobuf实现js与java间的http通信

前端js与后端java之间的http通信我们一般采用json这种文本格式,实现非常简单,也非常容易被人抓包。protobuf协议起源于google,采用类似TLV(tag,length,value)的编码方式,输出一段字节数组,是一种二进制格式的协议。相比json格式,protobuf编码出来的内容更少,效率更高,传输过程中因为是二进制被人抓包也就更难。protobuf官方给出了很多后端语言(java,C++,python)基于protobuf的交互。前端js与后端的交互例子比较少,因为本身js在处理http请求方面也比较弱。下面提供简单demo:

proto协议文件:

message Req {
    required string text = 1;
}
message Resp {
    required string code = 1;
}

前端代码:

<!DOCTYPE html>
<html>
<head>
<script src="./long.js"></script>   
<script src="./bytebuffer.js"></script>
<script src="./protobuf.js"></script>
<script>
if (typeof dcodeIO === 'undefined' || !dcodeIO.ProtoBuf) {
    throw(new Error("ProtoBuf.js is not present. Please see www/index.html for manual setup instructions."));
}
var ProtoBuf = dcodeIO.ProtoBuf;
var proto = ProtoBuf.loadProtoFile("./example.proto");
var Req   = proto.build("Req");
var Resp  = proto.build("Resp");

var req = new Req();
req.text="1";
var  body = new Uint8Array(req.toArrayBuffer());

var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost:8089/xxxxx_getProtoBufReq.action', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
	if (this.status == 200) {
		var resp=Resp.decode(this.response);
		alert(resp.code);
	}
};
xhr.send(body);
</script>
</head>
<body>
</body>
</html>
上面的html需要引入long.js,bytebuffer.js,protobuf.js三个文件,都可以在https://github.com/dcodeIO 的网站上找到。

参考资料:

http://mozilla.com.cn/thread-34886-1-1.html

http://web.jobbole.com/83701/


后端java代码:

	public String getProtoBuf() {
		return "protoBuf";
	}
	
	public void getProtoBufReq()  {
		HttpServletRequest request = ServletActionContext.getRequest();
		InputStream input=null;
		ByteArrayOutputStream out=null;
		ServletOutputStream output=null;
		try {
			input = request.getInputStream();
			out = new ByteArrayOutputStream();
			byte by[] = new byte[1024];
			int len = 0;
			while ((len = input.read(by)) != -1) {
				out.write(by, 0, len);
			}
			Example.Req req=Example.Req.parseFrom(out.toByteArray());
			System.out.println(req.getText());
			
			Example.Resp.Builder resp=Example.Resp.newBuilder();
			resp.setCode("300");
			HttpServletResponse response = ServletActionContext.getResponse();
			output=response.getOutputStream();
			output.write(resp.build().toByteArray());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			IOUtils.closeQuietly(output);
			IOUtils.closeQuietly(out);
			IOUtils.closeQuietly(input);
		}
	}

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值