gpt4 book ai didi

javascript - "Long Polling"模拟撑起整个网页

转载 作者:行者123 更新时间:2023-12-02 15:37:10 26 4
gpt4 key购买 nike

简要概述

我有一个基于回合的网络应用程序,每回合限制 60 秒,除非他们在 60 秒之前结束回合,从而下一个用户的回合开始。

它的工作原理是通过 while 语句来阻止 PHP 页面上生成数据,该语句检查新数据,如果存在则生成数据,如果不存在则休眠并重置:

while($olddata === $newdata){
sleep(2);
/* get new data and repeat */
}

我从这些 StackOverflow 问题中得到了这个概念:

How do I implement basic "Long Polling"?

Ajax push system

问题

但是,一旦标注开始,页面就会变得相对无响应;在超时完成或收到新数据之前,执行一些简单的操作(例如刷新页面)将不起作用。

如何配置此代码,以便页面在等待新数据时保持响应?

AJAX/jQuery 代码

function draftPing(){
//This $holder element holds the data including the
//row ID, which is used to determine which turn number
//we are on
var $holder = $("#data-holder");
var currID = $holder.attr("data-currID");

$.ajax({
type: "POST",
url: "../inc/draft-ping.php",
data: { currID : currID },
async: true,
cache: false,
timeout: 60000,
success: function(data) {
var jsonData = $.parseJSON(data);

//Update $holder with new turn ID
$holder.attr("data-currID", jsonData[0]);

/* Do stuff with data */
updateDeadline(jsonData[1]);
updateTeam(jsonData[3]);
updateDraft(jsonData[4]);

/* Begin the next call for new information (wait 1s)*/
setTimeout(
draftPing,
1000
);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("Draft retrieve error: ", textStatus + " (" + errorThrown + ")");
setTimeout(
draftPing, /* Try again after 5s*/
5000);
}
});
}

PHP 代码

<?php

session_start();
require_once("../config.php");
require_once("classes.php");
require_once("functions.php");

$postedID = $_POST["currID"];
$draft = new Draft($user->getLeagueID());
$currID = $draft->getCurrDraftRow();

//This is the "long polling" simulation...
//the AJAX callback won't produce any information
//until the current ID is different to the ID
//on the page with the AJAX post function
while($currID == $postedID){
sleep(2);
$currID = $draft->getCurrDraftRow();
}

/* Get all the data - redacted because it's not important (and it works) */

//Output all the data as one JSON object
exit(json_encode(array($nid, $ndeadline, $nuserid, $fteam, $fdraft)));

最佳答案

如果您使用 session_start() 打开 session ,最好在开始等待之前使用 session_write_close() 关闭它,否则所有其他请求对 session 的访问都将被阻止.

<?php

session_start();
require_once("../config.php");
require_once("classes.php");
require_once("functions.php");

$postedID = $_POST["currID"];
$draft = new Draft($user->getLeagueID());
$currID = $draft->getCurrDraftRow();

//This is the "long polling" simulation...
//the AJAX callback won't produce any information
//until the current ID is different to the ID
//on the page with the AJAX post function
while($currID == $postedID){
session_write_close(); //added
sleep(2);
session_start(); //if needed, doesn't look like it is though
$currID = $draft->getCurrDraftRow();
}

/* Get all the data - redacted because it's not important (and it works) */

//Output all the data as one JSON object
exit(json_encode(array($nid, $ndeadline, $nuserid, $fteam, $fdraft)));

关于javascript - "Long Polling"模拟撑起整个网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32849057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com