Created
June 7, 2019 13:51
-
-
Save ennjoy/60ed883ee42a9e2cf8b6b1c24f494663 to your computer and use it in GitHub Desktop.
longpoll.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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