gwt-comet用于GWT的基本用途 [英] Basic use of gwt-comet for GWT

查看:139
本文介绍了gwt-comet用于GWT的基本用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试此处的gwt-comet扩展程序。我无法从服务器获取任何消息到客户端。

I'm trying out the gwt-comet extension here. I cant get any messages from the server to the client.

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

I have a basic GWT application with a RPC service implementation.

客户端: MockGui.java

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

服务器: GreetingServiceImpl.java

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

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

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.

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

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

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

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

如果仍然无效,请执行

Window.alert(serverUrl);

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

and see if the URL is actually pointing to the comet servlet.

祝你好运!

这篇关于gwt-comet用于GWT的基本用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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