PHP聊天应用问题 [英] Php chat application questions

查看:90
本文介绍了PHP聊天应用问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是php / mysql的新手,已经决定实现一个简单的聊天应用程序。这是一些特定的问题。

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

1)
在Facebook中,如果我发送聊天消息,使用javascript更新聊天记录,但如果连接断开,一段时间后还会显示传递失败标记。我的问题是在使用AJAX / jquery发送消息之前,如何检查是否已建立与服务器的连接,以便我也可以通知用户该消息未传递。

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)考虑这种简化的情况。聊天室有2个插槽,也就是说,只有2个人可以共享聊天室并可以随时彼此聊天。如果第三个用户尝试登录,则将通知他聊天室已满。我可以将用户的登录数据存储为会话变量或类似的变量。我的问题是...如果两个人已经在聊天,而一个人只是关闭了浏览器却没有注销或类似的东西...在这种情况下,会话变量仍然存在,应用程序仍然认为有两个人在聊天如果用户不在浏览器窗口中处于活动状态,如何删除会话变量并为第三个家伙腾出空间?

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)聊天应用程序(如fb聊天和Google聊天)如何更新其聊天记录?他们是请求整个聊天日志(例如由10条消息组成)还是只是使用AJAX请求当前消息?每个请求的理想持续时间是多少?我正在使用1秒...

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)在jquery中,ajax回调函数会告诉您是否发生错误:

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);
    });
});

请参见 http://api.jquery.com/jQuery.post/

2)您可以定期轮询(就像每分钟一样)服务器,如果用户不联系服务器,则在触发onunload时销毁会话或向服务器发送断开连接命令(不太可靠)。

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)我会发送服务器最后一张图片的时间戳或ID,它将从那时起向我发送所有消息。

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

这取决于您希望聊天的实时程度。 1秒到10秒是理想的选择。

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

这篇关于PHP聊天应用问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆