Grails:服务器到客户端的通知
我正在构建一个 Grails 应用程序,它可以通过 Web 查询多个 API。问题是这种查询非常耗时,对于用户来说点击一个按钮并等待这么长时间而页面没有任何变化确实很烦人。
我的应用程序的基本架构是,当用户单击按钮时,客户端使用 Prototype 库向服务器端执行 Ajax 请求。然后,服务器端连接到 Last.fm API 并检索事件列表。当服务器端完成用事件填充列表时,它会向客户端发送 JSON 响应,这允许客户端为 Google 地图上的每个事件创建标记。
我想要实现的是,服务器端在检索到一个事件后立即发送 JSON 响应,而不是等待从 API 检索到所有事件来发送 JSON 响应,从而允许客户端填充地图,而其他事件事件仍在检索中。
我有什么想法可以实现这个吗?我读到了 Ajax Push 的概念,但我不确定它是否是我所需要的。 感谢您的帮助!
I'm building a Grails app which queries several API's across the Web. The problem is that this queries are very time consuming and it is really annoying for the user to click one button and wait so much time without nothing changes in the page.
The basic architecture of my app is, when the user clicks the button, the client side performs an Ajax request with Prototype library to the server side. The server side, then, connects to the Last.fm API and retrieve a list of events. When the server side is finished populating the list with events it sends a JSON response to the client side which allows the client side to create markers for each event on a Google map.
What I want to achieve is, instead of waiting for all the events being retrieved from the API to send the JSON response, the server side sends a JSON response as soon as it retrieve one event, allowing the client side to populate the map while other events are yet being retrieved.
Any ideas how can I implement this? I read about the concept of Ajax Push but I'm not sure if it is what I need.
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法在服务器可能连接的客户端上打开侦听连接。相反,您需要的是与服务器的连接保持活动状态并可用于接收事件。这样,您可以直接返回“action”请求,并在last.fm 请求完成后通过持久事件连接通知客户端。
也就是说,我实现的方法是使用 ajax keepalived。
There is no way to open a listening connection on the client that your server might connect to. Instead, what you need is a connection to the server that is kept alive and can be used to receive events. This way, you could directly return the "action" request and inform the client through your persistent event connection once the last.fm request was completed.
That said, the way I would implement is using ajax keep alive.
查看 cometd 插件。
Take a look at cometd plugin.