Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
qifanyang opened this issue Jul 2, 2015 · 7 comments

Comments

@qifanyang
Copy link

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

               }
@albin3
Copy link

albin3 commented Jul 12, 2015

估计是用到了外层的变量吧,直接拉到prototype里面会有难度。

@albin3
Copy link

albin3 commented Jul 12, 2015

@xyfcode 这个项目还会更新么?

@xyfcode
Copy link
Owner

xyfcode commented Jul 16, 2015

会更新的,随着项目的进展和上线运营遇到的问题而更新。

@xyfcode
Copy link
Owner

xyfcode commented Jul 16, 2015

sock是个实例对象,不能够使用prototype

@qifanyang
Copy link
Author

不可以在程序程序启动时 修改net.Socket的原型么?

@xyfcode
Copy link
Owner

xyfcode commented Jul 16, 2015

这样可以的,但方法中的sock参数需要独特设置一下。

@lijiejoy
Copy link

@xyfcode 能留下联系方式吗?我们有一个项目希望可以合作。或者加我微信lijiejoy
谢谢。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants