StreamHub 有一个 GWT Comet 模块:
http://code.google.com/p/gwt-comet-streamhub/
StreamHub是一个带有免费社区版的 Comet 服务器。这里有一个例子。
您需要下载 StreamHub Comet 服务器并创建一个新的 SubscriptionListener,使用 StockDemo 示例作为起点,然后创建一个新的 JsonPayload 来流式传输数据:
Payload payload = new JsonPayload("AAPL");
payload.addField("bid", "88.84");
payload.addField("ask", "88.86");
server.publish("AAPL", payload);
...
从 google 代码站点下载 JAR,将其添加到您的 GWT 项目类路径中,并将包含添加到您的 GWT 模块中:
<inherits name="com.google.gwt.json.JSON" />
<inherits name="com.streamhub.StreamHubGWTAdapter" />
从您的 GWT 代码连接和订阅:
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 代码中)以您喜欢的方式处理更新:
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。