GWT/Comet:有什么经验吗? [英] GWT / Comet: any experience?

查看:21
本文介绍了GWT/Comet:有什么经验吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以从 GWT订阅"到 JSON 对象流并在保持活动连接上侦听传入事件,而无需尝试一次获取它们?我相信这项技术的流行语是彗星".

Is there any way to "subscribe" from GWT to JSON objects stream and listen to incoming events on keep-alive connection, without trying to fetch them all at once? I believe that the buzzword-du-jour for this technology is "Comet".

假设我有 HTTP 服务,它打开保持活动连接,并实时将 JSON 对象与传入的股票报价放在那里:

Let's assume that I have HTTP service which opens keep-alive connection and put JSON objects with incoming stock quotes there in real time:

{"symbol": "AAPL", "bid": "88.84", "ask":"88.86"}
{"symbol": "AAPL", "bid": "88.85", "ask":"88.87"}
{"symbol": "IBM", "bid": "87.48", "ask":"87.49"}
{"symbol": "GOOG", "bid": "305.64", "ask":"305.67"}
...

我需要监听这些事件并实时更新 GWT 组件(表、标签).任何想法如何做到这一点?

I need to listen to this events and update GWT components (tables, labels) in realtime. Any ideas how to do it?

推荐答案

StreamHub 有一个 GWT Comet 模块:

There is a GWT Comet Module for StreamHub:

http://code.google.com/p/gwt-comet-streamhub/

StreamHub 是一个免费社区版的 Comet 服务器.有一个例子 here.

StreamHub is a Comet server with a free community edition. There is an example of it in action here.

您需要下载 StreamHub Comet 服务器并创建一个新的 SubscriptionListener,使用 StockDemo 示例作为起点,然后创建一个新的 JsonPayload 来流式传输数据:

You'll need to download the StreamHub Comet server and create a new SubscriptionListener, use the StockDemo example as a starting point, then create a new JsonPayload to stream the data:

Payload payload = new JsonPayload("AAPL");
payload.addField("bid", "88.84");
payload.addField("ask", "88.86");
server.publish("AAPL", payload);
...

从谷歌代码站点下载 JAR,将其添加到您的 GWT 项目类路径并将包含添加到您的 GWT 模块:

Download the JAR from the google code site, add it to your GWT projects classpath and add the include to your GWT module:

<inherits name="com.google.gwt.json.JSON" />
<inherits name="com.streamhub.StreamHubGWTAdapter" />

连接并订阅您的 GWT 代码:

Connect and subscribe from your GWT code:

StreamHubGWTAdapter streamhub = new StreamHubGWTAdapter();
streamhub.connect("http://localhost:7979/");
StreamHubGWTUpdateListener listener = new StockListener();
streamhub.subscribe("AAPL", listener);
streamhub.subscribe("IBM", listener);
streamhub.subscribe("GOOG", listener);
...

然后在更新侦听器中(也在 GWT 代码中)按照您喜欢的方式处理更新:

Then process the updates how you like in the update listener (also in the GWT code):

public class StockListener implements StreamHubGWTUpdateListener {
      public void onUpdate(String topic, JSONObject update) {
          String bid = ((JSONString)update.get("bid")).stringValue();
          String ask = ((JSONString)update.get("ask")).stringValue();
          String symbol = topic;
          ...
      }
}

不要忘记在您的 GWT 项目主 HTML 页面中包含 streamhub-min.js.

Don't forget to include streamhub-min.js in your GWT projects main HTML page.

这篇关于GWT/Comet:有什么经验吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆