LAMP に基づくソーシャル ネットワーキング アプリケーションの開発に取り組んでいます。無料の Web ホスティング ソリューションに Web サイトを配置しました。しかし、アクセスすると、Google Chrome の「Aw Snap! 何かがおかしくなった」ページに移動することがあります。ただし、他のすべてのブラウザでは問題なく動作します。Web サイトにアクセスしたときにエラーが頻繁に発生し、ローカルホスト経由でアクセスしたときにエラーが発生することもあります。この理由として何が考えられるでしょうか?
バックグラウンドで2つのSSE(サーバー送信イベント)を同時に実行しているためだと思われます。
おそらく、時間の経過とともに SSE が大量のデータを生成するためです (エラーは、SSE がデータの送信を開始してからしばらくしてからリンクをクリックした場合にのみ表示されることが多く、最初の送信では表示されないためです)。
私は Google Chrome の開発者コンソールを監視していました。リンクをクリックすると、「リソース onlineStudents.php の読み込みに失敗しました」および「リソース checkMessages.php の読み込みに失敗しました」というエラーが表示されます。
同じスクリプトは次のとおりです。
チャット.php
<script>
if(typeof(EventSource)!=="undefined")
{
var source2=new EventSource("onlineStudents.php");
source2.onmessage=function(event2)
{
var data=JSON.parse(event2.data);
$("#chat_head_number").html(": "+data['total']);
for(var i=1;i<30;i++)
{
$("#chat_states_number"+i).html(" ");
x=$("#state"+i).text();
for( var j=0;j<data['statesName'].length;j++)
{
if(data['statesName'][j]==x)
{
$("#chat_states_number"+i).html(" : "+data['statesNumber'][j]);
}
}
}
}
}
</script>
onlineStudents.php
<?php session_start();
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
require_once 'myfunctions.php';
$query="Select * from online_students where email<>'$_SESSION[user]'";
$result= queryMysql($query);
$data["total"]=mysql_num_rows($result);
$query="Select distinct state from online_students";
$result=queryMysql($query);
while($row= mysql_fetch_array($result))
{
$query2="select * from online_students where state='$row[state]' and email<>'$_SESSION[user]'";
$result2=queryMysql($query2);
$data["statesName"][]=$row['state'];
$data["statesNumber"][]=mysql_num_rows($result2);
}
$query="Select distinct college from online_students";
$result= queryMysql($query);
while($row= mysql_fetch_array($result))
{
$query2="Select * from online_students where college='$row[college]' and email<>'$_SESSION[user]'";
$result2=queryMysql($query2);
$data["collegesName"][]=$row['college'];
$data["collegesNumber"][]=mysql_num_rows($result2);
}
echo "data:".json_encode($data)."\n\n";
ob_flush();
flush();
sleep(3);
?>
header.php
<script type="text/javascript">
if(typeof(EventSource)!=="undefined")
{
var source=new EventSource("checkMessages.php");
source.onmessage=function(event)
{
$("#new_message").html("Inbox"+event.data);
};
}
else
{
$("#new_message").html("HTML5 not supported");
}
</script>
checkMessages.php
<?php
session_start();
require_once 'myfunctions.php';
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$userid=studentidOf($_SESSION['user']);
$query="Select distinct msgby from messages where msgfor='$userid'";
$result=queryMysql($query);
$k=mysql_num_rows($result);
$query="Select postnumber from notifications where notifor=$userid and postnumber is NOT NULL";
$result=queryMysql($query);
$k+=mysql_num_rows($result);
$collegeid=collegeidOf($_SESSION['user']);
$query="Select collegeid_post from notifications where notifor=$userid and collegeid_post=$collegeid and notiby<>$userid";
$result=queryMysql($query);
$k+=mysql_num_rows($result);
$query="Select collegeid_forum from notifications where notifor=$userid and collegeid_forum=$collegeid and notiby<>$userid";
$result=queryMysql($query);
$k+=mysql_num_rows($result);
$query="Select threadid from notifications where notifor=$userid and threadid is NOT NULL";
$result=queryMysql($query);
$k+=mysql_num_rows($result);
if($k>0)
echo "data:($k)\n\n";
ob_flush();
flush();
?>