Php chat application questions [closed]

萝らか妹 提交于 2019-12-12 06:46:13

问题


I being new to php/mysql, have decided to implement a simple chat application. Here are some specific questions..


1) In facebook, if i send a chat message, it updates the chat log using javascript but it will also come up with a "failed to deliver" mark after some time if the connection is lost. My question is how to check if connection to the server is established before sending the message using AJAX/jquery so that I too can inform the user that his message wasn't delivered.


2) Consider this simplified scenario.. The chat room has 2 slots, that is, only 2 people can share the chat room and chat with each other at any time.. In case a third user tries to login, he will be informed that chat room is full. I can store the user's login data as session variables or something like that. My question is this... If 2 people are already chatting and one guy just closes the browser without logging out or anything like that... In that case, the session variable is still present and the application still considers 2 people are chatting in the room.. How to delete the session variable if the user is not active in the browser window and make room for the 3rd guy to come in?


3) How do chat applications (like fb chat and google chat) update their chat log? do they request entire chat log (say consisting of 10 messages) or do they just request the current messages using AJAX? And what would be the ideal duration for each requests? I am using 1 second...


回答1:


1) In jquery ajax callback function tells you if error ocurred:

$("#form").submit(function(){
    $.post("post.php", {
        message: $('#field').val(),
    }, function(data, status){
        alert("success");
    }).error(function(){
        alert(error);
    });
});

See http://api.jquery.com/jQuery.post/

2) You can periodically poll (like every minute) the server and if user don't contact server, destroy his session or send disconnect command to server when onunload is fired (not so reliable).

3) I would send server the timestamp or id of last image and it would send me back all messages since then.

It depends on how much "real-time" you want your chat. 1 second to 10 seconds is ideal.




回答2:


I think most chat applications are not using ajax. The problem with ajax is there is no mechanism for the server to directly send messages to clients. If you used ajax each client would have to check the server at a timed interval.

A solution to this problem is websockets or long polling. Check out ratchet it is websockets for php.


If you insist on using ajax you can:

  1. Append a class on submit to the div the message is stored on class='ephemeral'. If the ajax fails just $('.ephemeral').append('failed')
  2. This can be solved by constantly pinging the server with ajax calls, if a client hasn't pinged the server in the last 5 seconds the client has disconnected.
  3. You could just send the server the time of the latest chat message you received and have it pull from the database only chats after that time. This requires saving each chat as a seperate row in the database.


来源:https://stackoverflow.com/questions/13666872/php-chat-application-questions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!