2017-01-20 6 views
2

現在、クライアントソリューションをSSEに更新しています。これはCodeigniterとクライアントに基づいており、JavaScriptを使用しています。私はSSEが初めてです。私はRest APIコントローラーで以下のコードを実行しました。クライアントでサーバー送信イベントJsonおよび更新クライアントを読み取る

function server_sent_events_get() 
    { 
     while (true) { 
      header('Content-Type: text/event-stream'); 
      header('Cache-Control: no-cache'); 
      $mac = $this->get('macaddress'); 
      $scheduleid = $this->get('scheduleid'); 
      $modifiedon = urldecode($this->get('modifiedon')); 

      $device_result = $this->read_devices_xml_json($mac); 
      $schedule_result = $this->read_schedules_xml_json($scheduleid, $modifiedon); 

      echo "event: schedule_status\n"; 
      echo "data: " . json_encode($schedule_result) . "\n\n"; 
      echo "event: device_status\n"; 
      echo "data: " . json_encode($device_result) . "\n\n"; 
      sleep(2); 
     } 
    } 

私は、コンソールにはエラーはありませんジャバスクリプト

if(typeof(EventSource) !== "undefined") { 
    var source = new EventSource("../admin/server/macaddress/000000000002/scheduleid/111/modifiedon/0000-00-00%2000:00:00/format/json"); 
    source.onopen = function(event){ 
     document.getElementById("result").innerHTML += "opened<br>"; 
     // console.log(event); 
    }; 
    source.onmessage = function(event) { 
     document.getElementById("result").innerHTML += event.data + "<br>"; 
     // console.log(event); 
    }; 
    source.onerror = function(event){ 
     document.getElementById("result").innerHTML += "error<br>"; 
     // console.log(event); 
    }; 
    source.addEventListener('device_status', function(e) { 
    var data = JSON.parse(e.data); 
    console.log("device_status"); 
    console.log(data); 
    }, false); 
    source.addEventListener('schedule_status', function(e) { 
    var data = JSON.parse(e.data); 
    console.log("schedule_status"); 
    console.log(data); 
    }, false); 
} else { 
    document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events..."; 
} 

の下に持っています。しかし、私は私が必要なものを得ていない。私は1秒ごとにその戻り値のdevice_statusを見ました。どうして?私の添付ファイルを確認してください。

Console log 基本的に2つのXMLファイルがあります。 1つはデバイス用で、もう1つはスケジュール用です。プライベート関数では、XMLファイルを読んでクライアントに値をプッシュしています。どこで逃したの?私にお知らせください。

答えて

0

私は多くの記事の助けを借りてGoogleの壁に私の脳を壊した後。私は以下のように私の問題を修正しました。これが誰かに役立つことを願っています。

サーバサイドスクリプト

function server_sent_events_get() 
    { 
     ob_start();//must put ob_start if you're using framework. Otherwise there will be a security notice error 
     header('Content-Type: text/event-stream'); 
     header('Cache-Control: no-cache'); 
     while (true) { 
      $array = array('updates' => intval(false)); 
      echo "id:" . uniqid() . PHP_EOL; 
      $mac = $this->get('macaddress'); 
      $scheduleid = $this->get('scheduleid'); 
      $modifiedon = urldecode($this->get('modifiedon')); 
      $device_result = $this->read_devices_xml_json($mac); 
      $schedule_result = $this->read_schedules_xml_json($scheduleid, $modifiedon); 
      if ($device_result['device_status']) { 
       echo "event: device_status\n"; 
       $this->curl_generates_function('device'); 
       $array = array('updates' => intval(true), 'trigger' => 'device_status', 'event_status' => intval(true)); 
      } elseif ($schedule_result['schedule_status']) { 
       echo "event: schedule_status\n"; 
       $this->curl_generates_function('schedule'); 
       $array = array('updates' => intval(true), 'trigger' => 'schedule_status', 'event_status' => intval(true)); 
      } 
      echo "data:" . json_encode($array) . "\n\n"; 
      ob_flush(); 
      flush(); 
      sleep(1); 
     } 
    } 

クライアントサイドスクリプト

function checkingUpdates() { 
    console.log("checkingUpdates :: start"); 
    if (typeof(EventSource) !== "undefined") { 
     var source = new EventSource("../admin/server/macaddress/000000000002/scheduleid/111/modifiedon/0000-00-00%2000:00:00/format/json"); 
     source.onopen = function (event) { 
      console.log("checkingUpdates :: EventSource - Open Connection"); 
     }; 
     source.addEventListener('device_status', function (event) { 
      source.close(); 
      console.log("checkingUpdates :: EventSource device_status - Close Connection"); 
      parent.location.reload(); 
     }, false); 

     source.addEventListener('schedule_status', function (event) { 
      source.close(); 
      console.log("checkingUpdates :: EventSource schedule_status - Close Connection"); 
      top.DataManager.getScheduleByGroupId(); 
     }, false); 

     source.onerror = function (event) { 
      console.log("checkingUpdates :: EventSource - Error Connection"); 
     }; 
    } else { 
     console.log("checkingUpdates :: EventSource - Not Working"); 
     top.SCHEDULE_TICKER = setInterval(function() { 
      top.DataManager.checkScheduleUpdates(); 
      console.log("checkScheduleUpdates :: Mannual Checking Checked"); 
     }, top.INTERVAL_UPDATE); 
     top.DEVICE_TICKER = setInterval(function() { 
      top.DataManager.checkDeviceStatus(); 
      console.log("checkDeviceStatus :: Mannual Checking Checked"); 
     }, top.INTERVAL_UPDATE); 
    } 
    console.log("checkingUpdates :: end"); 
} 

この答えは適切なものでない場合は、コメント/答えを追加することを躊躇しないでください。

ありがとうございました!ハッピーコーディング!

関連する問題