简体   繁体   中英

Make persistent connection in php socket to avoid too many apache instances

I have a page that contains a picture. The picture should refresh every second. I have a socket.php file that creates a link to a c++ program and asks for a picture and then put it as output. I have a js code that asks socket.php for an image every second. So every second my js code in clients browser, asks socket.php on my server to send the user a new picture and socket.php asks my c++ code for a picture, receive the picture and pass it to the client browser. every thing is ok. but when I change the interval from 1 second to 50 miliseconds, the number of "apache2" processes on my server goes really up. I mean about 200 apache2 processes and this uses too much ram memory on my server.

My question is: what should I do to make a have a persistent connection between php and c++ , so for every query from user, a new connection doesn't create? Does a persistent connection help to avoid this number of apache processes?

This is my socket.php file:

if(isset($_GET['message']))
        $message = $_GET['message'];
    else $message = "-1";
    $host    = "127.0.0.1";
    $port    = 12345;
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
    $result = socket_connect($socket, $host, $port) or die("Could not connect to server\n");    
    socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");
    $b= '';
    $buf = '';
    while(true)
    {
        $bytes = socket_recv($socket, $buf, 2048, 0);
        if($bytes==0) break;
        $b .= $buf;
    }
    $im = imagecreatefromstring($b);
    header('Content-Type: image/jpeg');
    imagejpeg($im);
    imagedestroy($im);

This is my js code:

function updateImage()
{
if(!isPaused)
{
    if(newImage.complete) {
    document.getElementById("myimg").src = newImage.src;
    newImage = new Image();
    newImage.src = "socket.php?message=0&image" + count++ + ".jpg";
    }
    setTimeout(updateImage, 50);        
}
}

60 calls in a min it's nothing and 200 too. I think problem is you don't close socket.

the best approach is make you c++ update mysql DB every second and page should ask for updated image from DB, like this you will gain flexibility. At that point you also can do cashing of the image Also you can attach as much image users as you want without opening the new sockets and without C++ application calling.

As far as I can tell, one really cannot persist a connection from an Apache instance of PHP (eg mod_php) to another service, except for database connections (eg all of those supported by PDO). @volkinc's link to how PHP executes is a pretty good example which illustrates where such a persistent link would have to cached/stored by PHP, but is not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM