如何让Ajax在聊天应用程序中工作? [英] how do i get ajax to work in a chat app?

查看:58
本文介绍了如何让Ajax在聊天应用程序中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一种使用ajax,jquery和php而不使用任何套接字或xmpp技术的聊天应用程序的方法.该应用程序使用文件来存储和显示两个用户之间的消息.

当我仅使用一个中央文件作为所有用户在线之间所有消息的日志时,我就可以使用它,问题是,例如,如果我的网站有50位访问者,而他们都想互相聊天,他们都会查看相同的日志文件,它将变成不是特定于用户的聊天室,因此要解决此问题,我制作了一个工作如下的系统:

用户登录后,所有用户的朋友都从mysql数据库中加载并显示在右侧,当用户单击朋友的名字时,将创建一个新文件,其中包含他们的唯一ID ,然后该文件将同时显示给他们两个,这样,所有用户每对都有自己的日志文件,例如如果James和John ahd的ID分别为456和457,则文件456-457-.html将在两个页面上显示为聊天日志文件,依此类推.当用户登录时,会自动为他们创建一个新文件,其中将包含提示他们接受聊天邀请的消息,因此,当用户James登录时,将创建一个带有其ID 456的文件,并显示为一个小文件.部分,因此,如果用户john想与James聊天,他将单击用户James的名字,这将创建文件456-457-.html并将其加载到用户John的页面上,一条消息将被写入用户James的文件提示他单击john的名字,单击它,文件456-457-.html也将显示在James的页面上,然后两个人开始聊天并发送消息通过ajax无缝连接.日志文件456-457-.html将每2.5秒重新加载一次,以便两个用户都始终使用新消息进行更新.

我有整个代码,我认为ajax和jquery有问题,代码将其详细实现,处理了可能出现的所有情况,例如防止制作相同用户的两个文件,加载正确的文件等.系统包含三个文件,即testing.php,makesend.php和style3.css.

我已经阅读了发布规则,并且无法在此处发布大量代码,因此我将发布我认为需要修改的内容:

i''ve been working on a way to implement a chat application using ajax, jquery and php without any sockets or xmpp technology. the app uses files to store and display messages between two users.

i got it to work when using just one central file that acted as a log for all the messages between all users online, problem is if for example, my website had 50 visitors and they all wanted to chat with each other, they would all be viewing the same log file, it would become a chat room that is not user specific, so to fix this, i made a system that works like this:

The user logs in, all the user''s friends are loaded from a mysql database and displayed to the right, when the user clicks on a friend''s name, a new file, bearing both their unique id''s is created, this file will then be displayed to both of them, that way, all the users would have their own log file per pair, e.g. if James and John ahd ids 456 and 457 respectively, file 456-457-.html would be displayed on both their pages as the chat log file and so on. when a user signs in, a new file is automatically made for them, this would contain messages that would prompt them to accept chat invitations, so when user James logs in, a file bearing his id, 456 would be made and displayed as a small section, so if user john wanted to chat with James he would click on user James'' name, this would create the file 456-457-.html and load it onto user John''s page, a message would be written into user James'' file prompting him to click on john''s name, upon clicking it, the file 456-457-.html would be displayed on James'' page as well, the two would then begin chatting and the messages would be sent seamlessly via ajax. the log file 456-457-.html would be reloaded every 2.5 seconds so that both users are always updated with new messages.

i have the entire code and i think that the ajax and jquery have a problem, the code implements this into detail, dealing with all the conditions that may arise, e.g. preventing two files for the same users from being made, loading the correct file etc. the system consists of three files, testing.php, makesend.php and style3.css.

I''ve read the posting rules and i can''t post a large chunk of code here, so i''ll post what i think needs modification:

//global variables
var username;
var friendname;
function makefile(username,friendname)
{
var ext=".html";
//concatenate
var dash="-";
var full=username.concat(dash,friendname,dash,ext);
var str=friendname.concat(dash,username,dash,ext);
//the first thing, is to use ajax to send the data to the script
$("#myresponse").html(full);
$.ajax({
type:"POST",
url:"makesend.php",
data: {full:full},
//what to do if data was sent:
success: function(full){
    document.getElementById('myresponse').innerHTML=full;


}
});
return false;




该部分接收用户名和朋友名,并使用ajax将它们发送到制作相关文件的脚本中,是否有错误?另外,如果有人有兴趣帮助我并且想查看所有文件,我会很感激,并且会发送或发布它们,我是一个初学者,所以这个问题可能会通过编码母版快速解决.




that part receives the user name and friend name and uses ajax to send them to the script that makes the relevant file, does it have an error? also, i kindly ask for help, if there''s anyone interested in helping me and would like to see all the files, i''d be very thankful and would send or post them, i''m a beginner, so the problem is likely to be resolved quickly by coding masters. thanks in advance.

推荐答案

(" ).html(full);
("#myresponse").html(full);


.ajax({ 类型:" , url:" , 数据:{full:full}, // 如果发送了数据该怎么办: 成功:功能(完整){ 文档 .getElementById(' myresponse') .innerHTML = full; } }); 返回 错误;
.ajax({ type:"POST", url:"makesend.php", data: {full:full}, //what to do if data was sent: success: function(full){ document.getElementById('myresponse').innerHTML=full; } }); return false;




该部分接收用户名和朋友名,并使用ajax将它们发送到制作相关文件的脚本中,是否有错误?另外,如果有人有兴趣帮助我并且想查看所有文件,我会很感激,并且会发送或发布它们,我是一个初学者,所以这个问题可能会通过编码母版快速解决.预先感谢.




that part receives the user name and friend name and uses ajax to send them to the script that makes the relevant file, does it have an error? also, i kindly ask for help, if there''s anyone interested in helping me and would like to see all the files, i''d be very thankful and would send or post them, i''m a beginner, so the problem is likely to be resolved quickly by coding masters. thanks in advance.


这篇关于如何让Ajax在聊天应用程序中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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