我的WebSocket有一个奇怪的问题。我只能连接381个插座。这总是381,我不知道这个号码来自哪里。我正在使用 Ubuntu 17.10 。
我正在使用多个浏览器进行测试。来自浏览器的382及更高版本的请求没有响应。 示例 - > http://prntscr.com/j7blpq
硬件信息:
2xcpu - >英特尔酷睿i7
4GB RAM
20GB HD
我正在使用nginx
来保持连接。
这是我的nginx.conf
:
user www-data;
worker_rlimit_nofile 8192;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
keepalive_requests 100000;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
这是我的PHP WebSocket:
<?php
class ws_server{
var $master;
var $sockets = array(); //create an array of socket objects
var $users = array(); //create an array of users objects to handle discussions with users
var $debug = false;
function ascii_banner() //just for old-skool fun...
{
$banner=" _ ____ _ _ \n";
$banner.=" __ _____| |__/ ___| ___ ___| | _____| |_\n ";
$banner.="\ \ /\ / / _ \ '_ \___ \ / _ \ / __| |/ / _ \ __|\n";
$banner.=" \ V V / __/ |_) |__) | (_) | (__| < __/ |_ \n";
$banner.=" \_/\_/ \___|_.__/____/ \___/ \___|_|\_\___|\__|\n";
return $banner;
}
function __construct($address,$port){
$chatHandler = new ChatHandler();
$null = NULL;
error_reporting(E_ALL);
set_time_limit(0);
ob_implicit_flush();
$this->master=socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("socket_create() failed");
socket_set_option($this->master, SOL_SOCKET, SO_REUSEADDR, 1) or die("socket_option() failed");
socket_bind($this->master, $address, $port) or die("socket_bind() failed");
socket_listen($this->master,44444) or die("socket_listen() failed");
$this->sockets[] = $this->master;
$chatHandler->say($this->ascii_banner() );
$chatHandler->say("PHP WebSocket Server running....");
$chatHandler->say("Server Started : ".date('Y-m-d H:i:s'));
$chatHandler->say("Listening on : ".$address." port ".$port);
$chatHandler->say("Master socket : ".$this->master."\n");
$chatHandler->say(".... awaiting connections ...");
$clientSocketArray = array($this->master);
while (true) {
echo 'log-1';
$newSocketArray = $clientSocketArray;
echo 'count socket new array'.count($newSocketArray);
#socket_select($newSocketArray, $null, $null, null);
if (false === socket_select($newSocketArray, $null, $null, null)) {
echo "socket_select() failed, reason: " .
socket_strerror(socket_last_error()) . "\n";
}
echo 'log 0';
if (in_array($this->master, $newSocketArray)) {
echo 'log1';
$newSocket = socket_accept($this->master);
echo 'log2';
$clientSocketArray[] = $newSocket;
$chatHandler->connect($newSocket);
echo 'log3';
$user = $chatHandler->getuserbysocket($newSocket);
echo 'log4';
$bytes = @socket_recv($newSocket,$buffer,4096,0);
echo 'log5';
$chatHandler->doHandshake($user,$buffer,$address,$port);
echo 'log6';
$newSocketIndex = array_search($this->master, $newSocketArray);
unset($newSocketArray[$newSocketIndex]);
echo 'log7';
}
foreach ($newSocketArray as $newSocketArrayResource) {
echo 'log 7-8';
$bytes = @socket_recv($newSocketArrayResource,$buffer,4096,0);
echo 'log8';
if($bytes==0){
echo 'log9';
$chatHandler->disconnect($newSocketArrayResource);
echo 'log10';
$newSocketIndex = array_search($newSocketArrayResource, $clientSocketArray);
echo 'log11';
unset($clientSocketArray[$newSocketIndex]);
} else {
$chatHandler->process($user,$chatHandler->frame_decode($buffer) );
echo 'log12';
}
}
}
echo 'log13';
socket_close($this->master);
}
}
我创建了日志以查看这是一个连接问题还是PHP,但一切看起来都很好,socket_select()
正在侦听,没有发生错误但用户计数停止再次连接的381个用户。每个用户循环达到log-7
并在log-1
之前socket_select()
结束,这意味着套接字正在侦听。
部分相关服务器设置:
file-max
= 396850; ulimit
= 4096; net.ipv4.netfilter.ip_conntrack_max
= 10000; 我遵循以下指示:Increasing the maximum number of tcp/ip connections in linux
答案 0 :(得分:1)
在您的nginx.conf
worker_connections 768;
中,如果您只通过websocket连接到没有反向代理的服务器,那么这将允许768个连接。
如果您将ngnix配置为反向代理,则只能有384个打开的ws连接,因为 768/2 = 384 。
可能会有一些连接来自您检查ws的本地计算机,因此会增加一两个连接。
参考: https://serverfault.com/questions/787919/optimal-value-for-nginx-worker-connections