SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    AJAX and infinite loop script

    Hi,

    Do you know if I can launch a PHP script kept-alive by an infinite loop (it's a socket client script) through AJAX without blocking the other JS commands coming afterwards?

    The idea is that a script is always launched, and sends returns data only when needed, and we execute this returned string little by little as it is returned on the ReadyState=3.
    I am doing this to implement a chat and an alert system using socket connections, in an existing PHP application which works only through AJAX. So there are JS messages all the time.

    I have made quite a lot of tries, but so far, even if the chat server sees the connections, on the client side, the script execution blocks anything which comes after - so the app is blocked.

    I have seen a few posts discussing the problem, but none came with a solution. Some suggested to use a Flash interface, which I'd like to avoid, others say that IE doesn't respond to ReadyState=3.
    Does any of you know more?

    If you wanna have a look at the code, here it is:

    The JS function:
    Code:
    function bbnjs_chat_reception(){
    	var div = myTop.document.getElementById('bbn_chat_text');
    	var myajax = new top.window.bbnjs_ajax_object();
    	var method   = "POST";
    	var filename = "../inx/reception.php";
    	var request  = "user=<?php echo $_SESSION['bbn_user']['bbn_login']; ?>";
    	var str_length = 0; //number of received characters
    	var update = function(){
    		if( myajax.readyState == 3 && myajax.responseText.length > str_length ){
    			str_length = myajax.responseText.length;
    			var response = myajax.responseText;
    			myTop.document.getElementById('bbn_chat_text').innerHTML = response;
    			div.scrollTop = div.scrollHeight;
    		}
    	};
    	setTimeout(update,100);
    	update();
    	myajax.open(method, filename, true);
    	myajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	myajax.send(request);
    }
    And the PHP:
    Code:
    <?php
    set_time_limit(0);
    $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
    if ( @socket_connect($socket,'127.0.0.1',35353) )
    	{
    		$stream = array();
    		$stream['bbn_connect'] = 1;
    		$stream['bbn_user'] = $_SESSION['bbn_user']['id'];
    		$stream = serialize($stream);
    		socket_write($socket,$stream,strlen($stream));
    		while ( true )
    			{
    				if( $input = @socket_read($socket,4096) )
    					{
    						if ( strlen($input) > 0 )
    							{
    								$output = unserialize($input);
    								echo $output;
    								flush();
    							}
    					}
    				else
    					{ echo socket_strerror(socket_last_error()); break; }
    			}
    		socket_close($socket);
    	}
    ?>
    Thanks for your help!

    Thomas

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://msdn.microsoft.com/en-us/libr...61(VS.85).aspx

    Remarks:
    You cannot call responseBody and responseText properties to obtain partial results (readyState = 3). Doing so will return an error, because status and response headers are not fully available. You must wait until all data has been received.

    In comparison, the Microsoft XML (MSXML) version of the IXMLHTTPRequest interface exposes partial results through the responseStream property, which the Windows Internet Explorer native version does not implement. Be aware that this behavior also differs from the IServerXMLHTTPRequest World Wide Web link interface, which provides partial results to responseBody and responseText.
    So if you forced IE7+ to use
    new ActiveXObject("msxml.xmlhttp")
    instead of
    new XMLHTTPRequest

    it might work?

  3. #3
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply Jim, but I have the same problem with Firefox.
    It seems I should be looking at COMET:
    http://en.wikipedia.org/wiki/Comet_(programming)
    http://alex.dojotoolkit.org/?p=545


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •