Skip to content

Instantly share code, notes, and snippets.

@ibeeger
Last active May 25, 2018 09:04
Show Gist options
  • Save ibeeger/87cc4ccc1722a64bc7825c12278098e3 to your computer and use it in GitHub Desktop.
Save ibeeger/87cc4ccc1722a64bc7825c12278098e3 to your computer and use it in GitHub Desktop.

msgpack5

  • nodeserver 部分

安装: npm install msgpack5

  • server代码 基于express 4.x

var msgpack = require("msgpack5")(); //设置类型
res.set("Content-Type", "application/msgpack"); //数据格式化 var _data = msgpack.encode({ a:1, b:2, c:3 }); res.send(_data);

server全部相关代码

res.set("Content-Type", "application/msgpack");
 var m = msg.encode({
	a: 1,
	c: 2,
	d: 5
 });
var lth = req.get("Content-Length");
console.log(lth);
req.on("data",function(e){
	console.log(msg.decode(e)) ;
})
res.send(m);
  • client端

首先引用 msgpack.js 地址 https://github.com/msgpack

客户端全部相关代码

var oReq = new XMLHttpRequest();	
    oReq.open("POST", "http://localhost:9090", true);    
    oReq.responseType = "arraybuffer";    
var msg = msgpack5();    
    oReq.onload = function (oEvent) {    
var arrayBuffer = oReq.response;     
 	 if (arrayBuffer) {    
 		   var byteArray = new Uint8Array(arrayBuffer);    
 		  	var s  = msg.decode(byteArray);    
 		   	console.log(s);    
 		 }    
 	};   
var data =  a.encode(["a","b","c","e"]);   
 	 oReq.send(data);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment