Skip to content

为什么sock.send方法没写到原型里面,如果有5000个连接,那就创建了5000个send方法? #1

@qifanyang

Description

@qifanyang

//定义发送消息方法,接收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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions