简体   繁体   English

几秒钟后重新加载聊天的另一种方法

[英]An alternate way of reloading a chat after few seconds

So I was implementing a chat room. 所以我当时正在建立一个聊天室。 I'll start off with the schema that I used. 我将从使用的架构开始。

  • I have a room table, that basically stores the information regarding the chatroom like the number of participant, the topic etc etc. 我有一个room表,它基本上存储了有关聊天室的信息,例如参加者的人数,主题等。
  • I have a users table that stores the users info. 我有一个存储用户信息的users表。
  • I have a posts table that stores the posts. 我有一个posts表,用于存储帖子。 This has a foreign key from Users and from room tables. 它具有来自用户和房间表的外键。
  • also, I have one final table that is to have a relation between users and rooms. 另外,我还有一张最终表要在用户和房间之间建立联系。 So it just has the roomid and the userid from the users who are a part of the room. 因此,它只包含来自房间一部分用户的roomid和userid。

Now, I have three divs on page, one for the chatarea, the other where the people online are shown and then there is a text area to post the message. 现在,我在页面上有三个div,一个用于聊天室,另一个用于显示在线用户,然后有一个文本区域来发布消息。

What I am doing currently is, to have a javascript function loadChats() , now this method calls a php file that just fetches all the posts in that particular room till now. 我目前正在做的是拥有一个javascript函数loadChats() ,现在此方法将调用一个php文件,该文件仅会提取到该特定房间的所有帖子。 And the same is dumped into my div ie "chatroom". 并将相同的内容转储到我的div即“聊天室”中。 Also, similarly, I have a loadParticipants() that load the users every other second. 另外,类似地,我还有一个loadParticipants() ,它每隔一loadParticipants()就会加载一次用户。

I am using jquery.post for the purpose and in the end of the method, I do a setTimeout in the end of the function. 我使用jquery.post来达到目的,并且在方法的结尾,我在函数的结尾执行了setTimeout Now here are my questions 现在这是我的问题

Ofcourse i can make this better. 当然,我可以做得更好。 Any suggestions? 有什么建议么? I was thinking of a few. 我在想一些。

  1. On every call to php, I get the entire chathistory and send it back to browser, ofcourse I can check if the count of messages is the same as it is on the client side, and if it is, then I wont send the messages. 在每次调用php时,我都会获取整个聊天记录并将其发送回浏览器,当然我可以检查消息计数是否与客户端相同,如果是,那么我将不发送消息。 But is it going to make it any better? 但这会使它变得更好吗? How? 怎么样?
  2. Also, making a call to server side every other second seems a bit too much of an overkill. 而且,每隔一秒钟向服务器端进行一次调用似乎有点过头了。 Is there any way to do it like, if some new chat is added to posts table, then that particular chatroom is notified and updated? 有什么方法可以做到,如果将新聊天添加到posts表中,则该特定聊天室将得到通知并更新? ie instead of constantly pinging the server to ask for new request, just ask it once, and wait if there is anything new or not. 即,不是持续对服务器进行ping操作以询问新请求,而是仅询问一次,然后等待是否有新消息。 When that request is completed, it pings the server again for the next update. 该请求完成后,它将再次ping服务器以进行下一次更新。

You should look into websockets (I've never used them with PHP but this seems really promising: http://socketo.me/ ). 您应该研究一下websockets(我从未在PHP中使用过它们,但这看起来确实很有希望: http : //socketo.me/ )。 What you can do is have the server push any new messages to the client whenever they come in, and have each of the clients push to the server, etc. This way you won't have to keep pinging the server over and over every 2 seconds, and loading tons of data to compare. 您可以做的是让服务器在客户端收到任何新消息时都将它们推送到客户端,并让每个客户端都推送到服务器,依此类推。这样一来,您就不必每隔一遍又一遍地对服务器进行ping操作秒,然后加载大量数据进行比较。 When there's a new message, the server saves it to some database and then pushes that message through all the open sockets. 收到新消息时,服务器将其保存到某个数据库中,然后通过所有打开的套接字推送该消息。 Same thing with logging in/logging off. 登录/注销也一样。

edit: Just looked through the page even more and their tutorial even goes through how to get it set up with a basic chatroom-esque functionality. 编辑:仅浏览了整个页面,他们的教程甚至介绍了如何使用基本的聊天室式功能进行设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM