GWT / Comet:任何经验? [英] GWT / Comet: any experience?

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

问题描述

有没有什么办法可以从GWT订阅到JSON对象流,并在保持活动连接的情况下监听传入事件,而不是一次尝试全部获取它们?我相信这项技术的流行词是彗星。



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



{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组件(表格,标签)。任何想法如何做到这一点?

解决方案

有一个用于StreamHub的GWT Comet模块:

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



StreamHub a>是具有免费社区版的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,听众);
...

然后在更新侦听器中处理更新如何这个GWT代码):

  public class StockListener实现StreamHubGWTUpdateListener {
public void onUpdate(String topic,JSONObject update){
String bid =((JSONString)update.get(bid))。stringValue();
String ask =((JSONString)update.get(ask))。stringValue();
字符串符号=主题;
...
}
}

别忘了将streamhub-min.js包含在您的GWT项目主HTML页面中。


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".

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"}
...

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

解决方案

There is a GWT Comet Module for StreamHub:

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

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

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);
...

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" />

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);
...

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;
          ...
      }
}

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

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

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