2015-06-29 21:22 阅读 85


I am trying to implement Server-sent-events communication between my client and my server.

I have implemented it and it works perfectly except when the server return exceptions.

I am not sure why the exception message is not being returned to the client after the exception.

Here is how my listener is written

    var evtSource = new EventSource('poll.php');

    evtSource.addEventListener("getMessagingQueue", function(e) {
        var data = JSON.parse(;

    }, false);

Below is my PHP code. (ie. poll.php) file

ini_set('display_errors', 0);

header("Content-Type: text/event-stream" . PHP_EOL);
header("Cache-Control: no-cache" . PHP_EOL);

try {

    $sleepTime = 1;
    $url = '';
    $loggedIn = false;

        throw new exception('Missing Credentials');
    } else {

        $scheme = 'http';
            $scheme = 'https';

        $url = sprintf('%s://%s:%s@%s:%s', $scheme, ICWS_USERNAME, ICWS_PASSWORD, ICWS_SERVER, ICWS_PORT);

        //configure the connection
        $conf = new ICWS\Config\Config($url, ICWS_STATION_NAME);    

        //create a new instance of icws

        $icws = new ICWS\Connection($conf); 
        $messaging = new ICWS\Messaging($icws);

        $loggedIn = $icws->isLogged();


        throw new exception('Something Went Wrong when trying to login');



        $result = array_merge( (array) $messaging->getCallsQueue(), (array) $messaging->getCurrentUserStatusQueue()) ;




} catch(Exception $e){

    $result = array('userStatus' => array('statusId' => 'You are not logged in!',
                                       'isLoggedIn' => false,
                                       'icwsDescription' => $e



function displayResults($result){

    echo 'event: getMessagingQueue' . PHP_EOL;
    echo 'data: ' . json_encode(  $result ) . PHP_EOL . PHP_EOL;

function isDefinedConstents(){

    $args = func_get_args();

    foreach($args as $v){
        $value = constant($v);
        if( !defined($v) || empty($value) ){
            return false;

    return true;

How do I get the listener to get the message even if the there is exceptions?

EDITED When I execute this code after the addEventListner line

evtSource.onerror = function(e) {

This is what I get

error { target: EventSource, isTrusted: true, currentTarget: EventSource, eventPhase: 2, bubbles: false, cancelable: false, defaultPrevented: false, timeStamp: 1435614201283000, originalTarget: EventSource, explicitOriginalTarget: EventSource, NONE: 0 }

It seems that the Exception thrown by PHP server cause the entire call to fail.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • douluo1330 douluo1330 2015-06-29 21:26

    Most likely, your Javascript code is having trouble JSON.parseing the PHP exception returned because it is not valid JSON. This will cause an uncaught exception in your Javascript code, ending the execution.

    The way to get around this is to throw the exception in a way the Javascript can handle. Instead of throwing the exception, try something like die("data: ". json_encode(array("exception" => "Missing Credentials.")))

    Also, try surrounding your JSON.parse() with a try catch block, and print the exception to the console.

    There may also be an uncaught exception being thrown in your processData function, but we have no way of knowing without seeing the code.

    点赞 评论 复制链接分享
  • du4373 du4373 2015-06-29 22:32

    I solved the problem by adding ob_end_clean(); below the two header(.... lines

    点赞 评论 复制链接分享