有一個GWT彗星模塊StreamHub:
http://code.google.com/p/gwt-comet-streamhub/
StreamHub是一個免費的社區版彗星服務器。有一個例子在行動here。
你需要下載StreamHub彗星服務器並創建一個新的SubscriptionListener,使用StockDemo例子作爲起點,然後創建一個新的JsonPayload到流中的數據:
Payload payload = new JsonPayload("AAPL");
payload.addField("bid", "88.84");
payload.addField("ask", "88.86");
server.publish("AAPL", payload);
...
從下載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。
+1優秀的文章,一些偉大的見解。 – Federer 2010-04-06 08:16:40
+1這正是我如何做到的。在Servlet 3.0規範中,「多線程」問題的解決方案拼寫爲「可掛起的請求」。 Jetty在7.0中對此提供了早期支持。 Tomcat稱之爲別的東西。 – 2010-08-22 21:34:48