Nodejs net 接受包 并解码,第一次使用了 protobuf

5 篇文章 0 订阅
3 篇文章 0 订阅

第一次使用 net 模块的 buffer 类型

对 buffer.copy 开始不了解 走了弯路,调用的对象是 sourece 一直以为是 dest

对包进行分割 包的结构为 :包内容长度[byte0 byte1] 包内容(protobuf arry)[byte2,byte3 ...],客户端使用 libevent 做

// 将net模块 引入进来
var net = require("net");
var path = require("path");
var protoBufJs=require("protobufjs");
var pbRoot = protoBufJs.loadSync(path.join(__dirname,"./pb/elabdata.proto"));
var eLabMessageType = pbRoot.lookupType("ELabMessage");

//创建一个cli 用于输出
var cli={};

//解码接收到的消息
function DecodeNetMsg(sock,msg)
{
	if(msg.netcmd==0)
	{
		if(cli[msg.account]!=undefined)
		{
			cli[msg.account].write("GameOver");
			cli[msg.account].end();
			delete cli[msg.account];
		}
		cli[msg.account]=sock;
		sock.room=msg.account;
		console.log(msg.account+"is login!");
	}
}

// 创建一个net.Server用来监听,当连接进来的时候,就会调用我们的函数
// client_sock,就是我们的与客户端通讯建立连接配对的socket
// client_sock 就是与客户端通讯的net.Socket
var server = net.createServer(function(client_sock) { 
	console.log("client comming", client_sock.remoteAddress, client_sock.remotePort);
	// 设置你接受的格式, 
	// client_sock.setEncoding("utf8");
	// client_sock.setEncoding("hex"); // 转成二进制的文本编码
	// 
	// 客户端断开连接的时候处理,用户断线离开了
	client_sock.on("close", function() {
		console.log("close socket");
	});
	// 接收到客户端的数据,调用这个函数
	// data 默认是Buffer对象,如果你强制设置为utf8,那么底层会先转换成utf8的字符串,传给你
	// hex 底层会把这个Buffer对象转成二进制字符串传给你
	// 如果你没有设置任何编码 <Buffer 48 65 6c 6c 6f 57 6f 72 6c 64 21>
	// utf8 --> HelloWorld!!!   hex--> "48656c6c6f576f726c6421"
	client_sock.on("data", function(data) {
		var readpos=0;
		if(client_sock.recbuf==undefined)
		{	
			client_sock.recbuf=Buffer.alloc(2048,0);
			client_sock.recsize=0;
		}
		do{
			//包分割
			var readsize;
			readsize=data.length-readpos;
			if(readsize>client_sock.recbuf.length-client_sock.recbuf.recsize)	readsize=client_sock.recbuf.length-client_sock.recbuf.recsize;
			data.copy(client_sock.recbuf,client_sock.recpos,readpos,readpos+readsize);
			readpos+=readsize;
			client_sock.recsize+=readsize;
			do{
				var len=client_sock.recbuf[0]+(client_sock.recbuf[1]<<8);
				if(len+2<=client_sock.recsize)
				{
					var tmp=Buffer.alloc(len);
					client_sock.recbuf.copy(tmp,0,2,2+len);
					var msg=eLabMessageType.decode(tmp);
					DecodeNetMsg(client_sock,msg);
					client_sock.recbuf.copy(client_sock.recbuf,0,len+2,client_sock.recsize);
					client_sock.recsize-=len+2;
				}
				else break;
			}while(1);
		}while(data.length!=readpos);
	});
	
	client_sock.on("error", function(err) {
		console.log("error", err);
	});
});
 
// 当我开始监听的时候就会调用这个回掉函数
server.on("listening", function() {
	console.log("start listening...");
});
 
 
// 监听发生错误的时候调用
server.on("error", function() {
	console.log("listen error");
});
 
server.on("close", function() {
	console.log("server stop listener");
});
/*
server.on("connection", function(client_sock) {
	console.log("client comming 22222");
});
*/
// 编写代码,指示这个server监听到哪个端口上面。
// 127.0.0.1: 6080
// node就会来监听我们的server,等待连接接入
server.listen({
	port: 1920,
    //host: "127.0.0.1",
    host: "192.168.183.1",
	exclusive: true,
});
 
// 停止node对server的监听事件处理,那么node就没有其他的事件要处理,所以就退出了。
// server.unref(); // 取消node,对server的事件的监听;
// server.close(); // 主动的掉这个server.close才会触发这个net.Server的close事件
module.exports=cli;

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值