如何保持所有JS一起为我的所有通知

时间:2015-03-14 08:19:47

标签: javascript php jquery

在这里,我阅读了许多类型的通知系统,如push,web socket,nodejs等。但那些对我来说都很复杂。所以作为一种简单的方法(在我的初步步骤中)我将使用Below方法发出通知(新朋友,新朋友,新评论/回复,新邮件等)。所以我使用特定的JS和php来获取特定的通知。

现在我想在JS中实现所有这些以最小化我的脚本。在这里我遇到的问题是每个通知都有不同的发送数据和php页面以及不同的结果div。那么如何将它们保持在一起呢?

另外一个问题(我知道stackoverflow规则):它能否使我的服务器连接问题太多?

新邮件通知JS:

function addrep(type, msg){
    // do here with result
}

var name = '<?php echo $username; ?>';
function waitForMail(){
    $.ajax({
        type: "GET",
        url: "/server/mail.php",
        cache: false,
        data: {name : name
    },
        timeout:15000, 
        success: function(data){ 
            addrep("postreply", data);
            setTimeout(waitForMail, 15000 );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            setTimeout(waitForMail, 15000); 
        }
    });
}

$(document).ready(function(){
    waitForMail();
});

新帖子通知JS:

function addpost(type, msg){
    // do here with result
}

var name = '<?php echo $username; ?>';
var profileid = '<?php echo $profileid; ?>';
function waitForPost(){
    $.ajax({
        type: "GET",
        url: "/server/post.php",
        cache: false,
        data: {name : name, profileid : profileid
    },
        timeout:15000, 
        success: function(data){ 
            addpost("postreply", data);
            setTimeout(waitForPost, 15000 );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown){
            setTimeout(waitForPost, 15000); 
        }
    });
}

$(document).ready(function(){
    waitForPost();
});

所有的php文件都像

一样
while (true) {
    if($_GET['username']){
    $res = mysqli_query(// here) or die(mysqli_error($dbh));

    $rows =  mysqli_fetch_assoc($res);
    $id = $rows['id'];
    //etc all
    //do something
    $data['id'] = $id;
        if (!empty($data)) {
        echo json_encode($data);
        flush();
        exit(0);
        }
    }
sleep(5);
}
mysqli_close($dbh);

1 个答案:

答案 0 :(得分:0)

嗯,我不确定您的应用程序设计是否合适。无论如何,似乎很明显,如果你必须以相同的间隔获取通知,最好有一个ajax来检查所有内容。类似的东西:

$.ajax({
    type: "GET",
    url: "/server/notification.php",
    cache: false,
    data: {name : name, profileid : profileid
},
    timeout:15000, 
    success: function(data) {
        /**
        * backend should return now single object for all notification types
        * something like 
        * {
        *     newpost: { ... },
        *     mail: { ... },
        *     somethingelse: { ... }
        * }
        */
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
        setTimeout(waitForPost, 15000); 
    }
});

如果由于某些原因无法执行此操作,则可以将所有URL及其回调保留在对象中,并在数组中创建ajax请求。类似的东西:

// list of ajax request names and their callbacks
var reqs = {
     mail: function(data) { doSomethingWith(data);},
     post: function(data) { doSomethingWith(data);}
};

// this function will send ajax by given type
function fetchNotification(type, callback) {
    $.ajax({
        type: "GET",
        // ajax url, for example /server/mail.php
        url: "/server/" + type + ".php",
        cache: false,
        data: {name : name, profileid : profileid},
        timeout: 15000,
        success: function(data) {
            // run callback. In this example execute doSoemthingWithData
            callback(data);
            // in 15 seconds repeat this reqest
            setTimeout(function() {
                 fetchNotification(type, callback);
            }, 15000)
        },
        error: function(req, status, err){
            // something went wrong, try again in 15 seconds
            setTimeout(function() {
                fetchNotification(type, callback);
            }, 15000); 
        }
    }
}

for (var i in reqs) {
    // init first ajax for each type and its callback from reqs object
    fetchNotification(i, reqs[i]);
}