Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否要阻止Apache服务器重新连接?_Javascript_Php_Ajax_Html_Apache - Fatal编程技术网

Javascript 是否要阻止Apache服务器重新连接?

Javascript 是否要阻止Apache服务器重新连接?,javascript,php,ajax,html,apache,Javascript,Php,Ajax,Html,Apache,我确信,由于服务器断开和重新连接,我的聊天应用程序正在受到影响。它应该过滤重复的消息,并且在大多数情况下是这样做的。但是,当两个(或更多)互相发送消息的用户同时连接时,他们的消息似乎会重复。如果只有一个用户在查看他们的消息,则不会发生这种情况。应用程序正在使用服务器端事件JavaScript API 不管怎样,有没有办法保持连接?还是解决这个问题的方法?可能是通过服务器端消息过滤 客户端: function chat() { if (document.getElementById('ch

我确信,由于服务器断开和重新连接,我的聊天应用程序正在受到影响。它应该过滤重复的消息,并且在大多数情况下是这样做的。但是,当两个(或更多)互相发送消息的用户同时连接时,他们的消息似乎会重复。如果只有一个用户在查看他们的消息,则不会发生这种情况。应用程序正在使用服务器端事件JavaScript API

不管怎样,有没有办法保持连接?还是解决这个问题的方法?可能是通过服务器端消息过滤

客户端:

function chat() {
    if (document.getElementById('chat').style.display == "none" || document.getElementById('chat').className != "on") {
        if (window.matchMedia("screen and (max-width: 800px)").matches) {
            document.getElementById('content').className = 'off';
            document.getElementById('chat').className = "on";
            if (document.getElementById('coverPhoto')) {document.getElementById('coverPhoto').className = 'off';}
        } else {
            document.getElementById('chat').style.display = 'block';
        }
        document.getElementById('chatBox').contentDocument.designMode = 'On';
        if (typeof(EventSource) !== "undefined") {
            var source = new EventSource('/ajax/gMessages.php'), last_id, used = {};
            source.addEventListener('message', function(event) {
                var now_id = event.lastEventId;
                if (last_id != now_id && document.getElementById('chatMsgs').innerHTML != event.data) {
                    last_id = event.lastEventId;
                    if (!used[event.data]) {
                        document.getElementById('chatMsgs').innerHTML += event.data;
                        used[event.data] = 1;
                        document.getElementById('chat').scrollTop = document.getElementById('chat').scrollHeight; //keep scrollbar at the bottom
                    }
                }   
            }, false);
        } 
    } else if (document.getElementById('chat').style.display == "block" || document.getElementById('chat').className != "off") {
        if (window.matchMedia("screen and (max-width: 800px)").matches) {
            document.getElementById('content').className = 'on';
            document.getElementById('chat').className = 'off';
            if (document.getElementById('coverPhoto')) {document.getElementById('coverPhoto').className = 'on';}
        } else {
            document.getElementById('chat').style.display = "block";
        }
    }
}
服务器端:

<?php
session_start();
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
set_time_limit(0);
include "../includes/config.php"; 
include "../includes/date.php";
function getMessages() {
    $con = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD);   
    $con->exec("SET CHARACTER SET utf8"); 
    $getMsgs = "SELECT * FROM `messages` WHERE `msg_to` = '$_SESSION[username]' AND `type` = '0' ORDER BY `sent` ASC";
    $receivedMsgs = $con->query($getMsgs); 
    foreach ($receivedMsgs->fetchAll() as $msg) { 
        $getUI = "SELECT `profile_pic`, `full_name`, `username` FROM `users` WHERE `username` = '$msg[msg_from]'";
        $uiQuery = $con->query($getUI);
        while ($UI = $uiQuery->fetch(PDO::FETCH_ASSOC)) { 
            echo "id: {$msg['id']}\n"; 
            echo "data: <article>\n"; 
            echo "data: <img src='{$UI[profile_pic]}' alt='{$UI[full_name]}' />\n"; 
            echo "data: <section class='msgCntnt'>\n";
            echo "data: <p class='timeAgo'>". Agotime($msg[sent]) ."</p>\n"; 
            echo "data: <a href='/profile?u={$UI[username]}'>{$UI['full_name']}</a><br />\n"; 
            echo "data: <h4>@{$msg['msg_from']}</h4>\n"; 
            echo "data: <p>{$msg['message']}</p>\n"; 
            echo "data: </section>\n"; 
            echo "data: </article>\n\n"; 
        } 
            ob_flush();
            flush();
            sleep(1);
    }
}
getMessages();
?>

您是使用长轮询还是每五秒钟查询一次?我建议使用WebSocket,但使用PHP是不可能的。但你的问题还是应该在别的地方。因为您不应该只将消息插入数据库,而应该只插入一次。也许给我们看一些代码,告诉我们什么不起作用。@Jompper WebSockets可以使用PHP,但使用.Updated OP开发不是一个很有趣的体验。我正在使用服务器发送事件Javascript API。在正确类型的服务器上,它的工作方式类似于单工WebSocket。在Apache上,它将重新连接,以便在我当前的服务器上进行长时间轮询。无论如何,问题不在于消息被插入了多次。它们被多次输出。只有在我在作品中提到的特定情况下。