Basic use of gwt-comet for GWT
I'm trying out the gwt-comet extension here. I cant get any messages from the server to the client.
I have a basic GWT application with a RPC service implementation.
Client: MockGui.java
public class MockGui implements EntryPoint {
@SerialTypes({
Message.class
})
public static abstract class MyMessageSerializer extends CometSerializer {
}
public void onModuleLoad() {
...
goServer();
}
public void goServer() {
GreetingServiceAsync service = GWT.create(GreetingService.class);
service.greetServer(new Message(), new AsyncCallback<Void>() {
@Override
public void onSuccess(Void result) {
// TODO Auto-generated method stub
}
@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
}
});
CometListener listener = new CometListener() {
public void onConnected(int heartbeat) {
}
public void onDisconnected() {
}
public void onHeartbeat() {
}
public void onRefresh() {
}
public void onError(Throwable exception, boolean connected) {
// warn the user of the connection error
}
public void onMessage(List<? extends Serializable> messages) {
for (Serializable message : messages) {
if (message.getClass().equals(Message.class)) {
Message myMessage = (Message) message;
Window.alert(myMessage .getMessage());
Log.info("This is a 'INFO' test message");
Log.warn("This is a 'WARN' test message");
}
}
}
};
String serverUrl = GWT.getModuleBaseURL() + "greet";
CometSerializer serializer = GWT.create(MyMessageSerializer.class);
CometClient client = new CometClient(serverUrl, serializer, listener);
client.start();
}
Server: GreetingServiceImpl.java
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
List<Message> messages = new ArrayList<Message>();
public void greetServer(Message message) throws IllegalArgumentException {
HttpSession httpSession = getThreadLocalRequest().getSession();
CometSession cometSession = CometServlet.getCometSession(httpSession);
Message m = new Message();
m.setMessage("test from server");
cometSession.enqueue(m);
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- Servlets -->
<servlet>
<servlet-name>greetServlet</servlet-name>
<servlet-class>test.mock.gui.server.GreetingServiceImpl</servlet-class>
</ser开发者_开发技巧vlet>
<servlet-mapping>
<servlet-name>greetServlet</servlet-name>
<url-pattern>/mockgui/greet</url-pattern>
</servlet-mapping>
<!-- the comet servlet for streaming messages to the client -->
<servlet>
<servlet-name>myComet</servlet-name>
<servlet-class>net.zschech.gwt.comet.server.CometServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myComet</servlet-name>
<url-pattern>/mockgui/comet</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>MockGui.html</welcome-file>
</welcome-file-list>
</web-app>
I expect the comet listener on the client side (listener) to be triggered - yet, nothing seems to happen. I don't know how I can debug the problem any further - nothing fails to execute.
Is there any way I can see when the comet servlet (myComet) is triggered and what it's doing?
It seems this is the URL you are using for the comet request:
String serverUrl = GWT.getModuleBaseURL() + "greet";
But your comet URL in web.xml is /mockgui/comet. Can you at least try replacing the above line with this?:
String serverUrl = GWT.getModuleBaseURL() + "comet";
and if it still doesn't work, do a
Window.alert(serverUrl);
and see if the URL is actually pointing to the comet servlet.
Good luck!
精彩评论