我一直致力于开发基于 LAMP 的社交网络应用程序。我将网站放在免费的网络托管解决方案上。但访问它有时会将我带到 Google Chrome 的“Aw Snap!出了点问题”页面。但是,它在所有其他浏览器上都可以正常工作。该错误在访问网站时经常出现,有时在通过 localhost 访问时出现。这可能是什么原因?
我怀疑这是由于同时在后台运行两个 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>
在线学生.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);
?>
头文件.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();
?>