php - MySQL - 服务器在长时间轮询时消失了

标签 php mysql sql

我有一个基于长轮询的聊天应用程序。服务器看起来像这样:

<?php
header('Content-Type: application/json');

if (!isset($_GET['lastId'])) {
    exit(json_encode(array('status' => 'error_lastIdNotSet')));
}

//DIBI LIBRARY
//insert dibi lib code here, no space for it
//more about it here: http://www.dibiphp.com/cs/quick-start

$lastDownloadedId = intval($_GET['lastId']);

$counter = 290;
while ($counter > 0) {
    if ($lastDownloadedId !== intval(file_get_contents('./lastMessageId.txt'))) {

        $arr_output = array(
            'status' => 'new',
            'messages' => array()
        );

        if ($_SERVER['REMOTE_ADDR'] == '127.0.0.1') {
            dibi::connect(array(
                'driver'   => 'mysql',
                'host'     => 'localhost',
                'username' => 'root',
                'password' => '',
                'database' => 'chat',
                'charset'  => 'utf8',
            ));
        } else {
            dibi::connect(array(
                'driver'   => 'mysql',
                'host'     => 'mysql.moxo.cz',
                'username' => 'u570204589_blog',
                'password' => '*************',
                'database' => 'u570204589_blog',
                'charset'  => 'utf8',
            ));
        }
        dibi::query('SET NAMES utf8');

        foreach(dibi::query('SELECT `id`, `time`, `unsafe_from`, `unsafe_messageText` FROM `messages` WHERE id>' . $lastDownloadedId)->fetchAll() as $row) {
            $arr_output['messages'][] = array(
                'id' => $row->id,
                'time' => $row->time->format('U'),
                'from' => $row->unsafe_from,
                'messageText' => $row->unsafe_messageText
            );
        }
        exit(json_encode($arr_output));
    }
    $counter--;
    usleep(100000);
}

echo json_encode(array('status' => 'timeout'));

如果在打开连接后大约 10 秒内首先发送新消息,则一切正常,但稍后发送时,Mysql 服务器已经消失,而是触发错误。

脚本当前位于 http://anagmate.moxo.cz/projectsDir/chat/longPollServer.php .为了使其工作,必须附加 ?lastId= 和最后一条消息 ID(可以在 http://anagmate.moxo.cz/projectsDir/chat/lastMessageId.txt 找到)。然后你可以尝试写一些消息到http://anagmate.moxo.cz/projectsDir/chat并回顾响应。

问题出在哪里?

错误看起来像这样:

Fatal error: Uncaught exception 'DibiDriverException' with message 'MySQL server has gone away' in /home/u570204589/public_html/projectsDir/chat/longPollServer.php:9
Stack trace:
#0 /home/u570204589/public_html/projectsDir/chat/longPollServer.php(9): DibiMySqlDriver->query('SELECT `id`, `t...')
#1 /home/u570204589/public_html/projectsDir/chat/longPollServer.php(9): DibiConnection->nativeQuery('SELECT `id`, `t...')
#2 /home/u570204589/public_html/projectsDir/chat/longPollServer.php(9): DibiConnection->query(Array)
#3 /home/u570204589/public_html/projectsDir/chat/longPollServer.php(42): dibi::query('SELECT `id`, `t...')
#4 {main}
SQL: SELECT `id`, `time`, `unsafe_from`, `unsafe_messageText` FROM `messages` WHERE id>55
  thrown in /home/u570204589/public_html/projectsDir/chat/longPollServer.php on line 9

最佳答案

foreach(dibi::query('SELECT `id`, `time`, `unsafe_from`, `unsafe_messageText` FROM `messages` WHERE id>' . $lastDownloadedId)->fetchAll() as $row) {
        $arr_output['messages'][] = array(
            'id' => $row->id,
            'time' => $row->time->format('U'),
            'from' => $row->unsafe_from,
            'messageText' => $row->unsafe_messageText
        );
    }

嗯,我理解的对吗 dibi::query('SELECT id, time, unsafe_from, unsafe_messageText FROM 消息 WHERE id>' . $lastDownloadedId)->fetchAll() 意思是你在每次迭代时都向数据库发出新请求?

可以分享一下你的滴滴课吗?

尝试使用:

$result = dibi::query('SELECT `id`, `time`, `unsafe_from`, `unsafe_messageText` FROM `messages` WHERE id>' . $lastDownloadedId)->fetchAll();
foreach($result as $row) {
            $arr_output['messages'][] = array(
                'id' => $row->id,
                'time' => $row->time->format('U'),
                'from' => $row->unsafe_from,
                'messageText' => $row->unsafe_messageText
            );
        }

关于php - MySQL - 服务器在长时间轮询时消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23090708/

相关文章:

php - Laravel 和 WordPress 前端

php - 如何在 Yii2 中使用 kartik-Select2 小部件项目将所选项目保存到数据库?

javascript - 如何使用站点 B 中的 iframe 调用站点 A 上的 jQuery 脚本?

mysql - 在mysql中创建带有gmt时区时间戳的数据字段?

php - TYPO3安装后"The requested URL was not found on this server"

php - 如何在 PHP 中使用可变长度参数列表准备 MySQL 查询

php - 可以在同一个 git 存储库中存储两种不同类型的代码吗?

mysql - 如何从模型中的不同区域设置获取 ID 号

sql - 使用异常更新复杂函数中的表

mysql - 如何确保多个mysql查询成功执行