我如何把它放在实时上?我已经放了(异步:真)但它不起作用
我终于让它与 AJAX 一起工作,但问题是,每当我更改 phpMyAdmin 中的一些数据时,它都不是实时的,我需要在网站上刷新它.
这是我的代码:ajax.js
$(document).ready(function() {$.ajax({url: "http://localhost/projectZeus/private/data.php",方法:获取",异步:真,成功:函数(数据){var 能量 = [];for(var i in data) {能量.push(数据[i].energyPercent);}var 图表数据 = {标签:[一月",二月",三月",四月",五月"],数据集:[{标签:收获",线张力:0.3,背景颜色:rgba(2,117,216,0.2)",边框颜色:rgba(2,117,216,1)",点半径:6,pointBackgroundColor: "rgba(2,117,216,1)",pointBorderColor: "rgba(255,255,255,0.8)",点悬停半径:8,pointHoverBackgroundColor: "rgba(2,117,216,1)",点命中半径:20,点边框宽度:2,数据:能源}]};var ctx = $("#AreaChart");var lineChart = new Chart(ctx, {类型:'线',数据:图表数据});},错误:函数(数据){}});});
这是我在 data.php
?>
如何在不刷新页面的情况下使其实时?请帮忙,谢谢!
您可以使用 server-sent-events 代替轮询,这不会像仅在有新事件发生时才发送数据那样对服务器造成太大压力发生了(就像一个新行).更多信息可以在这里找到:
chart.html
<html><头><meta charset="UTF-8"><title>服务器发送事件演示</title><script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script></头><身体><div id="chart_div"></div><button>关闭连接</button><脚本>//谷歌图表函数功能图(chart_data){google.charts.load('current', { packages: ['corechart', 'line'] });google.charts.setOnLoadCallback(drawBasic);函数drawBasic(){var data = new google.visualization.DataTable();data.addColumn('number', 'X');data.addColumn('number', 'Dogs');data.addRows(chart_data);变量选项 = {h轴:{作品名称:《时间》},垂直轴:{标题:人气"}};var chart = new google.visualization.LineChart(document.getElementById('chart_div'));chart.draw(数据,选项);}}//停止按钮var button = document.querySelector('button');//剩下的就是 EventSource,simplez..var evtSource = new EventSource('sse.php', { withCredentials: true });evtSource.onopen = 函数() {图表([])}evtSource.onmessage = 函数(e){图表(JSON.parse(e.data))}evtSource.onerror = function() {console.log("EventSource 失败.");}button.onclick = function() {console.log('连接关闭');evtSource.close();}/*** 或者您可以使用 addEventListener 来监听特定事件,例如 event: chartdata(或者如果您想在同一个流中发送多个事件)*///evtSource.addEventListener("ping", function(e) {////用 JSON.parse(e.data) 做一些事情// }, 错误的);//evtSource.addEventListener("message", function(e) {////用 JSON.parse(e.data) 做一些事情// }, 错误的);</脚本></身体></html>
然后是事件循环,注意这不是一个无限循环,也不需要维护它,它会在客户端连接时创建并在客户端断开连接时退出.
sse.php
希望它有所帮助,避免在 2018 年投票!
I finally made it working with AJAX but the problem is, it's not real-time whenever I change some data in phpMyAdmin, I need to refresh it on the website.
Here's my code: ajax.js
$(document).ready(function() {
$.ajax({
url: "http://localhost/projectZeus/private/data.php",
method: "GET",
async: true,
success: function(data) {
var energy = [];
for(var i in data) {
energy.push(data[i].energyPercent);
}
var chartdata = {
labels: ["Jan", "Feb", "Mar", "Apr", "May"],
datasets: [{
label: "Harvested",
lineTension: 0.3,
backgroundColor: "rgba(2,117,216,0.2)",
borderColor: "rgba(2,117,216,1)",
pointRadius: 6,
pointBackgroundColor: "rgba(2,117,216,1)",
pointBorderColor: "rgba(255,255,255,0.8)",
pointHoverRadius: 8,
pointHoverBackgroundColor: "rgba(2,117,216,1)",
pointHitRadius: 20,
pointBorderWidth: 2,
data: energy
}]
};
var ctx = $("#AreaChart");
var lineChart = new Chart(ctx, {
type: 'line',
data: chartdata
});
},
error: function(data) {
}
});
});
Here's my code in data.php
<?php
require_once('initialize.php');
header('Content-Type: application/json');
global $db;
$sql = "SELECT energyPercent FROM energy";
$result = mysqli_query($db, $sql);
$data = array();
foreach($result as $row) {
$data[] = $row;
}
mysqli_free_result($result);
echo json_encode($data);
?>
How can I get it to real-time without refreshing the page? Please help, thanks!
Instead of polling, you can use server-sent-events, which does not put as much strain on the server as data is only sent if a new event has happened (like a new row). More can be found out about them here: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
Here is an example, as the one in the link is not that good.
The result will look like the following gif:
chart.html
<html>
<head>
<meta charset="UTF-8">
<title>Server-sent events demo</title>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<div id="chart_div"></div>
<button>Close the connection</button>
<script>
// google chart function
function chart(chart_data) {
google.charts.load('current', { packages: ['corechart', 'line'] });
google.charts.setOnLoadCallback(drawBasic);
function drawBasic() {
var data = new google.visualization.DataTable();
data.addColumn('number', 'X');
data.addColumn('number', 'Dogs');
data.addRows(chart_data);
var options = {
hAxis: {
title: 'Time'
},
vAxis: {
title: 'Popularity'
}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
}
// stop button
var button = document.querySelector('button');
// the rest is the EventSource, simplez..
var evtSource = new EventSource('sse.php', { withCredentials: true });
evtSource.onopen = function() {
chart([])
}
evtSource.onmessage = function(e) {
chart(JSON.parse(e.data))
}
evtSource.onerror = function() {
console.log("EventSource failed.");
}
button.onclick = function() {
console.log('Connection closed');
evtSource.close();
}
/**
* or you could use addEventListener's to listen to specific events, like event: chartdata (or incase you wanted to send multiple events in the same stream)
*/
// evtSource.addEventListener("ping", function(e) {
// // do somthing with JSON.parse(e.data)
// }, false);
// evtSource.addEventListener("message", function(e) {
// // do somthing with JSON.parse(e.data)
// }, false);
</script>
</body>
</html>
Then the event loop, note that this is not an infinite loop nor do you need to maintain it, it will get created once a client connects and exit once the client disconnects.
sse.php
<?php
// no normal requests
if ($_SERVER['HTTP_ACCEPT'] !== 'text/event-stream') {
exit();
}
// make session read-only
session_start();
session_write_close();
// disable default disconnect checks
ignore_user_abort(true);
// set headers for stream
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Access-Control-Allow-Origin: *");
// a new stream or an existing one
$lastEventId = intval(isset($_SERVER["HTTP_LAST_EVENT_ID"]) ? $_SERVER["HTTP_LAST_EVENT_ID"] : 0);
if ($lastEventId === 0) {
// resume from a previous event
$lastEventId = intval(isset($_GET["lastEventId"]) ? $_GET["lastEventId"] : 0);
}
echo ":".str_repeat(" ", 2048)."
"; // Padding for IE
echo "retry: 2000
";
// query initial data, or select by event id
$data = [
[0, 0],
[1, 5],
[2, 15],
[3, 45],
[4, 34],
[5, 21],
];
// mock we at event 6
$lastEventId = 6;
// start stream
while (true) {
// user disconnected, kill process
if (connection_aborted()) {
exit();
} else {
// force an update, normally you would assign ids to your events/data
$latestEventId = $lastEventId+1;
//
if ($lastEventId < $latestEventId) {
// generate some data, use array_shift() before to limit array leght whilst rolling
$data[] = [$latestEventId, rand(0, 100)];
echo "id: " . $latestEventId . "
";
echo "event: message
";
echo "data: ".json_encode($data)."
";
$lastEventId = $latestEventId;
} else {
echo "event: ping
";
}
}
// flush buffer
ob_flush();
flush();
// 2 second sleep
sleep(2);
}
Hope it helps, avoid polling its 2018!
相关文章