编码技术效率问题:聊天框设计
我有一个问题要问任何有处理 PHP、Jquery 和 SQL 复杂编码经验的人。我意识到我写了很多,但我想详细一点。我对 1 解决方案有一个问题,我可以想到,但它似乎不是很有效,尤其是在处理大量数据的情况下交通。我有一个存储来自用户的消息的表。我正在设计一个聊天平台,用户可以打开聊天框并向其他用户(他们的朋友)发送即时消息。
我需要能够拥有它,这样当用户打开他的在线朋友的盒子并发送消息时,该朋友将弹出一个包含一条或多条消息的聊天框,并且该盒子将从那里更新。我希望当用户浏览网站时,该框还可以在页面之间保留以前发送的消息,直到他关闭该框。发生这种情况时,我创建了一个已查看列,我可以将当时该框中的所有消息标记为 1。从那时起,这些消息将不会弹出。只有新人才会。因此,关闭盒子实际上会重置它。
我有一个简单的 JSON 函数,用于发送信息,并且 php 处理程序运行查询并返回所有消息。然后将它们分类到各自的盒子中。
我认为一个解决方案是在 json 代码上设置刷新时间间隔,并在已查看 = 0 的情况下不断检查消息。如果有并且该框尚未弹出,它会将消息 html 放入框中。这样做的问题是,查询将选择用户收到的所有消息,并且它们将不断覆盖那些不会在视觉上显示但似乎对系统造成负担的框。我试图想出一种涉及查询的方法,该查询检查大于 jquery 函数中发送的时间戳的时间戳。 如果有人有任何建议或有用的文章或信息,我将不胜感激。
$.getJSON("../collabrr/chatbox.php?action=view&load=initial&receiver="+username+ "&token="+ token, function(json) {
for(i=0; i < json.length; i++) {
openchat({ data : { user : json[i].user }}); //makes chatbox open up with name of sender
$('#chatbox[data-name="'+json[i].user+'"]>#messagebox').prepend('<div id="chatbox-response">'+json[i].user+':'+json[i].message+'</div>').find('li').fadeIn(1500);
}
});
$sql = 'SELECT timestamp, user, message, receiver
FROM chatbox WHERE receiver=? AND viewed=? ORDER BY timestamp DESC';
$stmt = $conn->prepare($sql);
$result=$stmt->execute(array($_GET['receiver'],0));
}
Field Type Null Key Default Extra
id int(6) NO PRI NULL auto_increment
convo_id varchar(35) NO NULL
timestamp int(11) NO NULL
user varchar(25) NO NULL
receiver varchar(25) NO NULL
message varchar(150) NO NULL
viewed int(1) NO NULL
I have a question for anyone with experience in dealing with complex coding in PHP, Jquery and SQL. I realize I wrote a lot but I wanted to be detailed. I have an issue with 1 solution that I can think of but it doesn't seem very efficient, especially with large amounts of traffic. I have a table that stores messages from users. I am designing a chat platform where users can open a chatbox and send instant messages to other users who are their friends.
I need the ability to have it so when a user opens a box from his online friends and sends a message, that friend will have a chatbox pop up with the message or messages and the box will update from there. I would like that box to also maintain the previously sent messages in it from page to page when the user is navigating the site until he closes the box. When this happens, I created a viewed column which I can mark as 1 for all the messages that were in that box at the time. From then on those messages will not pop up. Only new ones will. So closing the box essentially resets it.
I have a simple JSON function where information is sent and a php handler runs a query and returns all the messages. They are then sorted into their boxes.
What I think is one solution is to put a refresh time interval on the json code and check for messages constantly where viewed=0. If there are and the box has not yet popped up, it will and have the messages html'd into the boxes. The problem with this is that the query will be selecting all the messages that the user has received and they will be constantly overwriting the boxes which won't show visually but seems taxing on the system. I was trying to think of a way that involves a query that checks for timestamps that are greater then a timestamp sent in the jquery function. IF anyone has any recommendations or useful articles or information, I would greatly appreciate it.
$.getJSON("../collabrr/chatbox.php?action=view&load=initial&receiver="+username+ "&token="+ token, function(json) {
for(i=0; i < json.length; i++) {
openchat({ data : { user : json[i].user }}); //makes chatbox open up with name of sender
$('#chatbox[data-name="'+json[i].user+'"]>#messagebox').prepend('<div id="chatbox-response">'+json[i].user+':'+json[i].message+'</div>').find('li').fadeIn(1500);
}
});
$sql = 'SELECT timestamp, user, message, receiver
FROM chatbox WHERE receiver=? AND viewed=? ORDER BY timestamp DESC';
$stmt = $conn->prepare($sql);
$result=$stmt->execute(array($_GET['receiver'],0));
}
Field Type Null Key Default Extra
id int(6) NO PRI NULL auto_increment
convo_id varchar(35) NO NULL
timestamp int(11) NO NULL
user varchar(25) NO NULL
receiver varchar(25) NO NULL
message varchar(150) NO NULL
viewed int(1) NO NULL
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
轮询服务器更新是一种解决方案。我可以给你另一个,但我不知道你是否有可用的资源/时间来实施它。
简而言之,这就是我在实现类似的东西时所做的:基本思想是使用 Websockets 并保持对后端开放的套接字。最好是 node.js 使用 socket.io 因为它的非阻塞性质。然后你可以使用 redis 及其 pub/sub 能够从客户端 A 接收更新并将其推送到客户端 B。
当客户端 A 加载您的网站时,它会通过 Websocket 连接到正在运行的 Node.js 进程,将其称为 PUBLISHER。发布者为此客户端订阅 Redis 中的特定频道。客户端 B 加载您的网站,它也连接到发布者等,就像客户端 A 一样。现在客户端 A 写入一些内容并将其发送到发布者。发布者将此事件发布到其 redis 通道。客户 B 受到关注,因为他不仅订阅了自己的频道,还订阅了 A 的频道(也许是因为他们是您社交网站上的朋友,如果您有的话)。
这听起来可能相当复杂,而且实现起来并不容易,但这也许可以让您对如何实现这样的发布/订阅系统有一个基本的了解。轮询只能作为后备解决方案,因为在高流量网站上,每隔 100 毫秒左右使用 ajax 请求不断轮询您的网络服务器将导致极端负载。
Polling the server for updates is one solution. I can give you another one, however I dont know if you have the resources availabe/the time to implement it.
In very short, this is what I did when I implemented something similiar: the basic idea is to use Websockets and keep a socket open to a backend. Preferably node.js using socket.io because of its non blocking nature. Then you would use redis and its pub/sub capabilities to receive updates from client A and push them to client B.
When client A loads your website, it connects via a Websocket to a running node.js process, call it PUBLISHER. Publisher subscribes itself to a specific channel in redis for this client. Client B loads your website, it also connects to Publisher etc. just like client A. Now client A writes something and it is sent to Publisher. Publisher publishes this event to its redis channel. Client B gets noticed, because he is not only subscribed to his channel, but also to A´s channel (maybe because they are friens on your social network site if you have one).
This sounds probably rather complicated, and it is not that easy to implement, but maybe this can give you a basic idea about how such a pub/sub system is implemented. Polling should only act as a fallback solution because on a high traffic website constantly polling your webserver with ajax requests every 100ms or so will cause extreme load.
听起来您想要的是 Ajax 推送解决方案(由于某种未知原因也称为“Comet”)。
这是一个解释如何在 PHP/Javascript 中执行此操作的网站:http://www.zeitoun。 net/articles/comet_and_php/start
另请参阅维基百科页面:http://en.wikipedia.org/wiki/Comet_%28programming%29
(呵呵,我注意到其中还包括对“彗星”这个名字的解释)
It sounds like what you're wanting is an Ajax push solution (also known as 'Comet' for some unknown reason).
Here is a site which explains how to do it in PHP/Javascript: http://www.zeitoun.net/articles/comet_and_php/start
See also the wikipedia page: http://en.wikipedia.org/wiki/Comet_%28programming%29
(heh, I note that also includes an explanation of the name 'Comet')