GWT / Comet: any experience?

2020-02-23 05:10发布

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?

标签: json gwt comet
8条回答
▲ chillily
2楼-- · 2020-02-23 05:42

The JBoss Errai project has a message bus that provides bi-directional messaging that provides a good alternative to cometd.

查看更多
我只想做你的唯一
3楼-- · 2020-02-23 05:42

We are using Atmosphere Framewrok(http://async-io.org/) for ServerPush/Comet in GWT aplication.

On a client side Framework has GWT integration that is pretty straightforward. On a server side it uses plain Servlet.

We are currently using it in production with 1000+ concurent users in clustered environment. We had some problems on the way that had to be solved by modifying Atmosphere source. Also the documentation is really thin.

Framework is free to use.

查看更多
该账号已被封号
4楼-- · 2020-02-23 05:47

there is indeed a cometd-like library for gwt - http://code.google.com/p/gwteventservice/

But i ve not personally used it, so cant really vouch for whether its good or not, but the doco seems quite good. worth a try.

Theres a few other ones i ve seen, like gwt-rocket's cometd library.

查看更多
Viruses.
5楼-- · 2020-02-23 05:50

Also, some insight on GWT/Comet integration is available there, using even more cutting-and-bleeding edge technology: "Jetty Continuations". Worth taking a look.

查看更多
Ridiculous、
6楼-- · 2020-02-23 05:51

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.

查看更多
欢心
7楼-- · 2020-02-23 05:56

Here you can find a description (with some source samples) of how to do this for IBM WebSphere Application Server. Shouldn't be too different with Jetty or any other Comet-enabled J2EE server. Briefly, the idea is: encode your Java object to JSON string via GWT RPC, then using cometd send it to the client, where it is received by Dojo, which triggers your JSNI code, which calls your widget methods, where you deserialize the object again using GWT RPC. Voila! :)

My experience with this setup is positive, there were no problems with it except for the security questions. It is not really clear how to implement security for comet in this case... Seems that Comet update servlets should have different URLs and then J2EE security can be applied.

查看更多
登录 后发表回答