1

So I've been able to use AJAX to repeatedly refresh a div on my website, using the following code:

var $container = $("#content");
    var refreshId = setInterval(function()
    {
        $container.load('toad.php').fadeOut("slow").load('response.php').fadeIn("slow"); ;
    }, 2000);

This works fine, toad.php gets executed every 2 seconds and the contents update.

toad.php contains the following code currently:

<?php 
if ( !($sock = socket_create(AF_INET, SOCK_STREAM, 0)) ) {
        $errorcode = socket_last_error();
        $errormsg = socket_strerror($errorcode);

        die("Couldn't create socket: [$errorcode] $errormsg \n");
    }

    echo "Socket created \n";

    if (!socket_connect($sock, '127.0.0.1', 45000)) {
        //$errorcode = socket_last_error();
        //$errormsg = socket_strerror($errorcode);
        echo "No sensor available to connect to.\n";
        //die("Could not connect: [$errorcode] $errormsg \n");
    }

    echo "Connection established \n";

    //Now receive reply from server
    if (false !== ($bytes = socket_recv($sock, $buf, 1200, MSG_WAITALL))) {
        echo "Read $bytes bytes from socket_recv(). Closing socket...";
    }           
    else {
         echo "socket_recv() failed; reason: " . socket_strerror(socket_last_error($sock)) . "\n";
    }

    socket_close($sock);

    echo $buf . "\n";
    echo "OK.\n"; 
?>   

The issue with this is that the socket is created and connected to every single time the div is refreshed.

If I don't have the socket_create and socket_connect functions in toad.php then the socket_recv function doesn't know about the socket and doesn't work.

My question is how can I get the socket receive code to loop and update the contents of the div without having to re create and connect to the socket every time?

Thanks!

  • Sounds like you want a closure. – Useless Intern Apr 24 '13 at 13:15
  • Try this solution. Working with persistent connections in PHP is pretty tricky: http://stackoverflow.com/questions/8595686/persistent-local-domain-socket-in-php – silkfire Apr 24 '13 at 13:19
  • the obvious answer would be to use a standard http requests and use sessions or cookies to maintain state. Or pass the data back and forth in the query. – Spudley Apr 24 '13 at 13:23
  • Hmm ok, maybe I should have asked if there was a better way of having a div that was constantly updating with data received every second or so from a TCP connection. I just defaulted to using php sockets because it seemed simplest to me but perhaps there's a more elegant solution to my problem. – user2315642 Apr 24 '13 at 13:44

1 Answers1

0

If you can rewrite the server your connecting to to use websocket, you could just connect directly from the browser using socket.io.

http://socket.io/

As others have said, maintaining a persistent PHP connection can be tricky, but if running a NodeJS (or similar) script to handle client connections isn't possible you could serve the websockets from that same PHP script.

Try this approach:

http://devzone.zend.com/209/writing-socket-servers-in-php/

Your toad.php script would run on the command line, listening to your existing socket server, then serving the data to it's own socket server which the browser connects to directly with websocket.

watermanio
  • 235
  • 1
  • 11