继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Websocket 突破最大长连接

qq_生活就像二进制_04394092
关注TA
已关注
手记 4
粉丝 0
获赞 0

为了测试机器能够最大的长连接个数,故写了一个js脚本,需要用node进行执行

var WebSocketClient = require('websocket').client; var size = 8000;
var index = 0;

setInterval(function () {
    if (index < size) {
        init();
        index++;
    }
}, 10);
console.log('begin...');
init = function () {
    var client = new WebSocketClient();
    let urlIndex = index;
    // client.connect('ws://192.168.214.191:8899/ws', "", "");
    client.connect('ws://127.0.0.1:21112/OrderWebSocket/'+index, "", "");
    // client.connect('ws://192.168.214.181:30004/Invest/OrderWebSocket/' + index, "", "");

    client.on('connectFailed', function (error) {
        console.log('Connect Error: ' + error.toString());
    });

    client.count = 0;

    client.start = 0;
    client.on('connect', function (connection) {
        var last = (Number)(new Date().getMilliseconds());
        console.log(index + ' Connected\n');
        connection.on('error', function (error) {
            console.log("Connection Error: " + error.toString());
        });
        connection.on('close', function (error) {
            var second = (new Date().getTime() - client.start)/1000 + 1; 
            console.log(error + ';  Connection Closed:second = '+ second +",count:"+client.count +"--timepercount:"+second/client.count);
            
        });
        connection.on('message', function (message) {
            if(client.start == 0){
                client.start = new Date().getTime();   
            }
            
            client.count++;
            // var now = (Number)(new Date().getMilliseconds());
            // // console.log("序号:"+urlIndex+",消息:"+message.utf8Data+"\n"+(now - last));
            // last = now;
        });
        connection.send("hello");
    });

}; 

以上代码就是来连接websocket使用的,发现到了1.3w左右连接,就出现Connection Error,解决方案


编辑/etc/security/limits.conf,添加以下两行代码,注意前面有星号

* soft nofile 1000000
* hard nofile 1000000

file

然后修改临时端口和IP_TABLE最大跟踪的TCP连接数有限制,编辑/etc/sysctl.conf,在文件中添加如下行

net.ipv4.ip_local_port_range = 10000 65535
net.netfilter.nf_conntrack_max = 1000000
net.nf_conntrack_max = 1000000

file

完毕,现在连接可以到4.5w左右差不多,我的机器会报以下错误
file

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP