多浏览器Windows同步
我正在尝试创建内联聊天。 当我的网站的两个窗口打开时,问题就出现了。 因为然后我的脚本将新消息发送到请求它的窗口(目前我使用简单的轮询,稍后将移至彗星)。
有没有什么我没有想到的好技术,以便我可以向两个浏览器窗口发送消息? 目前我正在使用读取标志,因此一旦消息传递到一个窗口,它就会被标记为已读。 因此永远不会被传递到另一个窗口。 我该如何解决这个问题?
感谢您的时间。
I am trying to create an inline chat. The problem arrives when two windows of my site are open. Because then my script sends the new message to the the window which requests it for (for the time being i am using simple polling, will move to comet later).
Is there any good technique that I haven't thought of, so that I can send a message to both the browser windows? Currently I am using a read flag, so as soon as the message gets delivered to one window, it is marked as read. Thus never getting delivered to the other window. How do I solve this problem?
Thank you for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我能想到的一件事是在消息上使用某种时间戳,而不是“已读”标志。
这样,当新消息的页面请求(我假设你正在使用某种计时来汇集服务器的新消息)发送它得到的最后一个时间戳时,服务器会发回之后的所有消息那一点。
One thing I can think of is to use some sort of timestamp on the messages, instead of the "read" flag.
This way, when the page request for the new messages (and I'm assuming uyou're using some sort of timing to pool the server for new messages) is sends the last timestamp it got, then the server sends back all the messages after that point.
每个窗口都需要是一个单独的“订阅者”,服务器需要向每个订阅者发送消息。 因此,您需要为每个订阅者设置一个“已读”标志,而不是一个“已读”标志。
不幸的是,这意味着为每个订阅者创建一个订阅者管理器,并提供一种知道订阅者何时消失的方法——也许是当订阅者停止轮询一分钟时。
在四人帮一书中查找“观察者”模式(也称为“发布-订阅”)(“设计模式:可重用面向对象软件的元素”,作者:Gamma、Helm、Johnson 和 Vlissides),ISBN 0-201 -633610-2) 或在线。
Each window needs to be a separate "subscriber", and the server needs to send the message to each subscriber. So instead of one "read" flag you need a "read" flag for each subscriber.
Unfortunately this means making a subscriber manager with a queue for each subscriber and a way of knowing when a subscriber disappears -- perhaps when the subscriber stops polling for one minute.
Look up the "Observer" pattern (also known as "Publish-Subscribe") in the Gang of Four book ("Design Patterns: Elements of Reusable Object-Oriented Software" by Gamma, Helm, Johnson and Vlissides", ISBN 0-201-633610-2) or online.