Skip to content

Instantly share code, notes, and snippets.

@ennjoy
Created June 7, 2019 13:51
Show Gist options
  • Save ennjoy/60ed883ee42a9e2cf8b6b1c24f494663 to your computer and use it in GitHub Desktop.
Save ennjoy/60ed883ee42a9e2cf8b6b1c24f494663 to your computer and use it in GitHub Desktop.
longpoll.php
<?php
$ts = isset($_REQUEST['ts']) ? intval($_REQUEST['ts']) : false;
$wait = isset($_REQUEST['wait']) ? intval($_REQUEST['wait']) : false;
$access_token = check_token();
$updates = array();
$row_ts = mysqli_fetch_assoc(mysqli_query($sqlConnect, "SELECT time FROM ". T_MESSAGES ." ORDER BY id DESC "));
if (!$ts) {
error(30, 'not found ts');
} else if ($ts) {
if (in_array($wait, range(1,90))) {
if ($row_ts['time'] - 10 < $ts && $ts < $row_ts['time'] + 50) {
while (true) {
$row_messages = mysqli_query($sqlConnect, "SELECT id, from_id, time FROM ". T_MESSAGES ." WHERE time > '$ts' AND to_id = '$access_token[user_id]' AND deleted_one = '0' AND deleted_two = '0' ");
$row_ts_new = mysqli_fetch_assoc(mysqli_query($sqlConnect, "SELECT time FROM ". T_MESSAGES ." ORDER BY id DESC "));
if (mysqli_num_rows($row_messages)) {
while ($row = mysqli_fetch_array($row_messages)) {
$updates[] = array($row['id'], $row['from_id'], $row['time']);
}
$response = array(
'ts' => $row_ts_new['time'],
'updates' => $updates
);
json_encode_cyr($response);
exit();
}
sleep($wait);
}
} else {
$row_ts_new = mysqli_fetch_assoc(mysqli_query($sqlConnect, "SELECT time FROM ". T_MESSAGES ." ORDER BY id DESC "));
$response = array(
'failed' => 31,
'ts' => $row_ts_new['time']
);
json_encode_cyr($response);
}
} else {
error(56, 'wait is incorrect');
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment