用简单的 javascript 实现 long pull / comet 不需要 XSS 框架
我有自己的精简脚本,目前根据浏览器支持使用 XHR 或脚本标签。这些请求最终返回一些 JSON。我的问题现在是这个对象的元素现在需要由服务器在客户端上更新,即我需要实现某种长拉/彗星解决方案。
Google 似乎想出了很多使用各种框架(例如 JQuery 等)的解决方案来在客户端执行此操作。但这对我来说不是一个选择。
我想知道你们是否对我如何扩展现有方法以允许从服务器进行彗星样式更新有任何建议。标准方法之一似乎是使用隐藏的 iframe。这是不行的,因为为我提供 json 数据的应用程序服务器与实际的网络服务器不同。
I have my own stripped down script that currently makes uses of XHR or script tags depending on browser support. These requests ultimately return some JSON. My problem is now elements of this object now need to be updated by the server while on the client i.e. i need to implement some kinda of long pull/comet soln.
Google seems to come up with lots of solns using various frameworks such as JQuery etc.. to do this on the client side. However this is not an option for me.
I was wondering if you guys had any suggestions on how I could extend my existing approach to allow comet style updates from the server. One of the standard approaches seems to be using hidden iframes. This is a no go as the app server that is providing me with my json data is different to the actual webserver.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery 只是简单地包装了 XHR/XMLHTTPRequest 对象。
首先,您需要一个小函数来以跨浏览器的方式返回对象。
这只需 3 行或更少的代码即可完成,并不太困难。也就是说,这些代码片段非常适合修复不同的浏览器问题,例如内存泄漏。我强烈建议您使用这个。这些当然跨越 3 行以上(除非缩小)。但无论哪种情况,如果您想要重复连接,您都无法从头开始执行此操作。接下来,在服务器端,假设您在 PHP:
客户端,只要连接结束就重复查询。此时,还要处理输出。
客户端示例:
jQuery simply wraps around the XHR/XMLHTTPRequest object.
First of, you need a small function to return the object in a cross-browser manner.
This is done in 3 lines or less, not too difficult.That said, the are great snippets for fixing different browser issues, such as memory leaks. I highly suggest you use this one. These of course span more than 3 lines (unless minified). But in either case, if you want repeated connections, you just can't do this from scratch.Next, on the server side, assuming you're on PHP:
Client side, just repeat the query whenever the connection ends. At this point, also handle the output.
Clients-side example: