Firefox中的服务器发送事件不使用AngularJS

时间:2014-09-20 11:13:45

标签: javascript php angularjs firefox

目前,我正在学习服务器发送事件。如果我制作一个简单的HTML应用程序,一切都很好。但是当我变成AngularJS时,问题出现了。 Firefox中不会连续调用Server Sent Events。它只工作一次。这个问题在Chrome和Opera中都不会发生。

这是关于Firefox中AngularJS性能的一些问题吗?或者我的代码有什么问题吗?任何答案都将非常感谢!谢谢.. :))

的index.php

<body ng-app='' ng-controller="myCtrl">
    <table class="table table-bordered table-hover table-responsive table-striped">
        <thead>
            <tr>
                <th>No.</th>
                <th>Id</th>
                <th>Name</th>
                <th>Level</th>
                <th>Hapus</th>
            </tr>
        </thead>
        <tr ng-repeat="x in admin">
            <td>
                {{$index + 1}}.
            </td>
            <td>
                <input type="text" value="{{x.Id}}" class="init" readonly>  
            </td>
            <td>
                <textarea ng-keyup="myFunc($event, x.Id, 'Name')" class="init" ng-focus="edit($event)" ng-blur="noedit($event, x.Id, 'Name')">{{x.Name}}</textarea>
            </td>
            <td>
                <textarea ng-keyup="myFunc($event, x.Id, 'Level')" class="init" ng-focus="edit($event)" ng-blur="noedit($event, x.Id, 'Level')">{{x.Level}}</textarea>
            </td>
            <td> 
                <button ng-click="sendDelete(x.Id)" class="btn btn-warning">Hapus</button>
            </td>
        </tr>
    </table>
    <div id="result"></div>
    <script src="js/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="js/angular-1.2.25.min.js" type="text/javascript"></script>
    <script type="text/javascript">
                                function myCtrl($scope, $http, $window) {
                                    if (typeof (EventSource) !== "undefined") {
                                        // Yes! Server-sent events support!
                                        var source = new EventSource("sse.php");

                                        source.onmessage = function(event) {
                                            msg = JSON.parse(event.data);
                                            $scope.admin = msg;
                                            $scope.$apply();
                                            document.getElementById("result").innerHTML += msg.Id + ". " + msg.Name + ". " + msg.Level + "<br>";
                                            console.log($scope.admin);
                                        };
                                    } else {
                                        // Sorry! No server-sent events support..
                                        alert('SSE not supported by browser.');
                                    }
                                };
        </script>
</body>

sse.php

<?php

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

  require_once 'koneksi.php';
  if (isset($_GET['id'])) {
     $id = $_GET['id'];
     $query = "SELECT * FROM `admin` WHERE id='" . $id . "'";
  } else {
     $query = "SELECT * FROM `admin`";
  }
  $stmnt = $dbh->prepare($query);
  $stmnt->execute();

  $output = "[";
  while ($row = $stmnt->fetch()) {
     if ($output != "[") {
        $output .= ",";
     }
  $output .= '{"Id":"' . $row["id"] . '",';
  $output .= '"Name":"' . $row["name"] . '",';
  $output .= '"Level":"' . $row["level"] . '"}';
  }
  $output .= "]";

  echo 'data: ' . ($output) . "\n\n";

 ob_flush();
 flush();
 sleep(1);

 ?>

0 个答案:

没有答案