创建基于网络的聊天系统的几个问题
我决定创建一个基于网络的聊天系统来获得体验。我混合使用 AJAX(jQuery)、PHP 和 JSON 来传输数据。现在我开始思考某些事情,我陷入了思维障碍。
现在,我使用 javascript 将最后加载的消息 ID 发布到 php 文件,该文件查询数据并回显 json 中的新帖子,然后在页面上按顺序显示这些帖子。但是,日期并不反映用户的当前时间。由于我使用 php 来获取当前时间,因此我不知道如何向用户显示考虑到他们的时区的正确时间。其次,我如何使用此方法合并在线列表?我可以创建一个单独的表,并在用户创建会话时更新它,并在结束会话时删除他们的名字;但如果他们没有正确关闭怎么办?我是否应该将他们最后发送的消息添加到表中,如果距离他们最后一条消息已经过去了大约 5 分钟,则认为用户已断开连接?最后,我用来收集新帖子的方法有效吗?有更好的方法来解决这个问题吗?我很感激任何意见。
I've decided to create a web based chat system for the experience. I'm using a mixture of AJAX(jQuery), PHP, and JSON to transfer the data. Now that I've started thinking about certain things, I've come to a mind block.
Right now, I use javascript to post the last loaded message id to a php file that queries the data and echoes new posts in json and then displays those posts in order on the page. However, the dates don't reflect the current time for the user. Since I use php to get the current time, I have no idea how to display the correct time to the user which takes into account of their time zone. Second, how would I incorporate a who's online list with this method? I could create a separate table and update it when a user creates a session and delete their name when they end the session; but what if they don't close it properly? Should I just add their last sent message into the the table and if it's been about 5 minutes since their last message consider the user disconnected? Lastly, is the method I'm using to collect new posts efficient? This there a better way to go about this? I appreciate any input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎相关:确定用户的时区
我'我会让你去那里查看代码片段,这样你就可以对你的赞成票给予适当的认可。
我的印象是 Javascript 是获取这些数据的最好/最简单的方法。
我可能会做的是使用 GMT 或其他固定时区来处理所有服务器内容,然后根据时区在浏览器上使用 js 进行调整。要么在对话开始时收集一次,然后相应地调整你的输出。任何一种方式都可能各有利弊。
编辑:
哦,是的,关于“谁在线”,我认为您正朝着正确的方向前进。我可能会建议 2 个列表。 “谁活跃”和“谁最近活跃”
这样,您可以在 5 分钟后将人们置于不活动状态,并在 10 分钟后将其视为已断开连接。我想大概是一样的,但对我来说似乎更准确。
另一种选择是将 ajax 请求设置为每分钟左右自动触发一次请求。当他们停止时,你就知道用户已经离开了。
This seems related: Determine a User's Timezone
I'm going to make you go there for the code snip so you give proper credit with your upvotes.
I get the impression that Javascript is the best/easiest way to get that data.
What I would probably do is use GMT or some other fixed time zone for all your server stuff and then just adjust that with js once it hits the browser depending on their time zone. Either that or just collect it once at the start of the conversation and adjust your output accordingly. There might be advantages to either way.
Edit:
Oh yeah, about the "who's online" I think you're headed in the right direction. I might suggest 2 lists. "Who's active" and "Who was active recently"
That way you can put people inactive after 5 mins and consider them disconnected after 10 or something. I guess it's about the same but it seems more accurate to me somehow.
The other option would be to set an ajax request to automatically fire of a request every minute or so. When they stop then you know the user is gone.