-
Notifications
You must be signed in to change notification settings - Fork 202
Open
Description
//定义发送消息方法,接收json数据对象,方法中会完成编码
//在这里会为每个sock创建一个send方法,为什么不写到原型里面
sock.send=function(msg){
msg = JSON.stringify(msg);
var len = Buffer.byteLength(msg);//buffer是全局的不需要导入,process也是全局的
//写入4个字节表示本次包长
var headBuf = new Buffer(4);
headBuf.writeUInt32LE(len, 0);
var bodyBuf = new Buffer(len+headBuf.length);
headBuf.copy(bodyBuf,0,0,headBuf.length);
bodyBuf.write(msg,headBuf.length);
if(sock.writable)
{
sock.write(bodyBuf);
}
else
{
global.err("socket write err,writable :" + sock.writable);
sock.emit("c_close");
}
}Metadata
Metadata
Assignees
Labels
No labels