0

I'm trying to implement Comet-like behaviour on my site to simulate PUSH events.

I'm using jQuery and $.ajax request. The problem with my principle is that I make re-request when I get success event... But... I get success immediately when the request is sent, not when the script is done with the execution...

You might see the problem I now have. The re-request happens even when there is no data from the script - very bad behaviour because it calls my script > 50 times per second...

Is there a way to know when the script has done its part (real success event)?

Br, Stjepan.

EDIT: Code

/*

    IPR Comet implementation

*/

// Define singleton
IPRComet = new function()
{
    // Variables
    this.m_bStopExecution = false;
    this.m_iTmp = 1;

    // Init IPR Comet implementation
    this.Init = function()
    {
        // Start poll worker thread
        this.m_bStopExecution = false;
        this.PollThread();
    };

    // Main poll thred
    this.PollThread = function()
    {
        if(this.m_bStopExecution)
            return;

        this.m_iTmp += 1;

        $.ajax(
        {
            url: BASE_IPRBASE_URL + "/AJAX/Comet.php",
            success: function(pCometResponse) { IPRComet.OnResponse(pCometResponse); },
            error: function() { IPRComet.OnSystemError(); },
            complete: function(jqXHR, sStatus)
            {
                $("#Sandbox-Output2").text(jqXHR.readyState + ": " + IPRComet.m_iTmp);
                if(sStatus == "success" || sStatus == "timeout")
                    IPRComet.PollThread();
            },
            dataType: "json",
            timeout: 30000
        });
    };

    // System error handler
    this.OnSystemError = function()
    {
        // Stop further execution
        this.m_bStopExecution = false;

        IPRClientHooks.CallHook("Comet::OnSystemError");
    };

    // Response error handler
    this.OnResponseError = function(pCometResponse)
    {
        // Stop further execution
        this.m_bStopExecution = false;

        IPRClientHooks.CallHook("Comet::OnResponseError", pCometResponse);
    };

    // Response handler
    this.OnResponse = function(pCometResponse)
    {
        // Error case       
        if(pCometResponse.Type == "Error")
        {
            IPRComet.OnResponseError(pCometResponse);
            return;
        }

        IPRClientHooks.CallHook("Comet::OnResponse", pCometResponse);
    };

};

// Start Comet when document is ready
$(document).ready(function() { IPRComet.Init(); });
Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
StjepanV
  • 167
  • 2
  • 14
  • have you tried using `success` and `error` callbacks to call `IPRComet.PollThread();` instead of `complete` callback? SHould also look at AJAX examples in `$.when` http://api.jquery.com/jQuery.when/ – charlietfl Jan 27 '13 at 21:22
  • Yes, I have. complete is triggered for success and error at the same time. Success is complete event but filtered to status message "success". :( – StjepanV Jan 27 '13 at 21:24
  • success is only called after the server-side code completes, so I agree with @charlifi's comment. Perhaps you need to set the async=false property. – ron tornambe Jan 27 '13 at 21:27
  • async=false defeats the purpose. I'm trying to implement PUSH like behaviour using AJAX. async set to false would block my other script execution... I need PUSH like functionality for real-time user notification. (Like Facebook) – StjepanV Jan 27 '13 at 21:30
  • Your client-side behaviour is correct. The challenge is server-side, to achieve a mechanism to make a response only when there's some change of state to report. – Beetroot-Beetroot Jan 27 '13 at 23:10
  • Server-side modifications (through PHP/Apche modules) are locked to me. I'm using shared hosting :( – StjepanV Jan 28 '13 at 05:03

1 Answers1

0

Had to remove jQuery from the picture... It does not function as it should...

Had to use XMLHTTPRequest object and onreadystatechange event.

Anyway, here is the working code:

/*

    IPR Comet implementation

*/

// Define singleton
IPRComet = new function()
{
    // Variables
    this.XHR = null;
    this.m_bStopExecution = false;

    // Init IPR Comet implementation
    this.Init = function()
    {
        this.m_bStopExecution = false;

        this.XHR = new $.ajaxSettings.xhr();
        this.XHR.onreadystatechange = function()
        {
            if(this.readyState == 4 && this.status == 200)
                IPRComet.OnResponse(this.responseText);
        };
        // Start poll worker thread
        this.PollThread();
    };

    // Main poll thred
    this.PollThread = function()
    {
        if(this.m_bStopExecution)
            return;

        try
        {
            this.XHR.open("GET", BASE_IPRBASE_URL + "/AJAX/Comet.php", true);
            this.XHR.send();
        }
        catch(pException)
        {
            this.OnSystemError();
        }
    };

    // System error handler
    this.OnSystemError = function()
    {
        // Stop further execution
        this.m_bStopExecution = false;

        IPRClientHooks.CallHook("Comet::OnSystemError");
    };

    // Response error handler
    this.OnResponseError = function(pCometResponse)
    {
        // Stop further execution
        this.m_bStopExecution = false;

        IPRClientHooks.CallHook("Comet::OnResponseError", pCometResponse);
    };

    // Response handler
    this.OnResponse = function(sCometResponse)
    {
        var pCometResponse = JSON.parse(sCometResponse);

        // Error case       
        if(pCometResponse.Type == "Error")
        {
            IPRComet.OnResponseError(pCometResponse);
            return;
        }

        IPRClientHooks.CallHook("Comet::OnResponse", pCometResponse);

        this.PollThread();
    };

};

// Start Comet when document is ready
$(document).ready(function() { IPRComet.Init(); });
StjepanV
  • 167
  • 2
  • 14