gwt-comet用于GWT的基本用途

时间:2011-05-15 02:40:11

标签: java gwt comet

我正在尝试gwt-comet扩展here。我无法从服务器获取任何消息给客户端。

我有一个带有RPC服务实现的基本GWT应用程序。

客户: 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();
}

服务器: 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>
    </servlet>

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

我希望触发客户端(侦听器)上的彗星监听器 - 但似乎没有任何事情发生。我不知道如何进一步调试问题 - 没有什么不能执行。

当彗星servlet(myComet)被触发以及它正在做什么时,我有什么方法可以看到吗?

1 个答案:

答案 0 :(得分:5)

这似乎是您用于彗星请求的网址:

String serverUrl = GWT.getModuleBaseURL() + "greet";

但是你在web.xml中的彗星URL是/ mockgui / comet。你能否至少尝试用这个代替上面的那一行?:

String serverUrl = GWT.getModuleBaseURL() + "comet";

如果仍然无效,请执行

Window.alert(serverUrl);

并查看URL是否实际指向comet servlet。

祝你好运!