Websocket 突破最大长连接

为了测试机器能够最大的长连接个数,故写了一个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

个人联系方式QQ:944484545,欢迎大家的加入,分享学习是一件开心事

猜你喜欢

转载自www.cnblogs.com/hy-xiaobin/p/12085209.html