George_Fal 2016-04-16 06:18 采纳率: 0%
浏览 16

PHP AJAX-聊天功能

I'm building a web application using PHP, JavaScript/jQuery and MySQL. Right now I'm trying to implement a chat feature that allows two user who are both online to chat with each other (e.g. NOT a big chat room, but only private chats between two users). However, I've ran into the following questions during the implementation process:

  1. How can I let one user know whether another user is currently online or not? Now I have a page where a user can see the name of other registered users. I hope to differentiate those who are currently logged in from those that aren't. Currently, when a user is logged in, I store his username in $_SESSION['name']. So how can one user know whether another user's $_SESSION['name'] is also set?

  2. How can I ensure that the conversation is private to two users? I currently have a page called "chat.php", where the chat interface is located in. When one user clicks on the name of another user who's also online, the two will be directed to their own "chat.php". Similarly other users should be unable to view chat that they are not involved in. I'm currently thinking about generating a unique page for the two users, like "chat.php?user1=Tom&user2=John" But how exactly should I achieve this? I'm new to PHP.

  3. To display the new message if the other person has just sent one, can we do this using Ajax in an event-driven way? Or can we only use polling? I'm currently using polling like the following, but I feel that polling isn't every efficient:

    // "logs.php" reads chat message from the database 
    setInterval(function(){
        $.get("logs.php", {}, function(resp) {
            // display the response
        });
    }, 1000);  // poll every second
    

Any input to any of the above questions is appreciated! Thanks.

  • 写回答

1条回答 默认 最新

  • weixin_33743880 2016-04-16 07:56
    关注

    I am also currently working on chat right now, below answer are according to my best of knowledge if you find something wrong please comment it.

    Answer 1// With the use of session you will not be able to get who are logged in and who aren't, to check you have to set flag in database and from there you to fetch logged in user apart from current session user.

    Answer 2// Here you are mixing to different concept of chat i.e. one to one chat(private chate) and another is group chat. What I will prefer is create different chat file for both concept. Consider a scenario where you 100 user chatting in group you will not be able to send 100 user id through ajax.

    Answer 3// Long polling is the method with which you can achieve real time chat update. But still there are so many catch to use long polling with PHP. First and foremost is the main drawback using long polling is it use more resource when user will increase.

    So, in conclusion try to use web socket programming or use different framework like node.js to implement.

    If you have less number of user then definitely you can use long polling with PHP.

    评论

报告相同问题?