使用GWT-Strophe的GWT XMPP客户端

时间:2011-07-07 23:45:19

标签: gwt strophe jsni

我正在使用GWT-Strophe连接到我的XMPP服务器。事情进展顺利,我能够连接到我的XMPP服务器并向其他用户发送消息。我收到邮件时遇到问题。我正在尝试复制Strophe echobot示例,但是当收到消息时,我的处理程序中的代码没有被执行。

以下是我用来连接和注册处理程序的代码:

connection = new Connection("http://localhost/proxy/");
handler = new Handler<Element>() {

    @Override
    public boolean handle(Element element) {
        GWT.log("Handling...");
        GWT.log(element.toString());

        String to = element.getAttribute("to");
        String from = element.getAttribute("from");
        String type = element.getAttribute("type");

        NodeList<com.google.gwt.dom.client.Element> elems = element.getElementsByTagName("body");

        if ((type == null ? "chat" == null : type.equals("chat")) && elems.getLength() > 0) {
            Element body = (Element) elems.getItem(0);

            GWT.log("ECHOBOT: I got a message from " + from + ": " + body.getText());
            String[][] attributes = {{"to", from}, {"from", to}, {"type", "chat"}};    
            Builder reply = Builder.$msg(attributes).cnode(body.copy());    
            connection.send(reply.tree());

            GWT.log("ECHOBOT: I sent " + from + ": " + body.getText());
        }    
        return true;
    }
};

StatusCallback callback = new Connection.StatusCallback() {

    @Override
    public void statusChanged(Status status, String reason) {

        if (status == Status.CONNECTING) {
            GWT.log("Strophe is connecting.");
        } else if (status == Status.CONNFAIL) {
            GWT.log("Strophe failed to connect.");
        } else if (status == Status.DISCONNECTING) {
            GWT.log("Strophe is disconnecting.");
        } else if (status == Status.DISCONNECTED) {
            GWT.log("Strophe is disconnected.");
        } else if (status == Status.CONNECTED) {
            GWT.log("Strophe is connected.");
            connection.addHandler(null, null, "message", null, null, handler);
            Builder pres = Builder.$pres(null);
            connection.send(pres);

            GWT.log("ECHOBOT: Send a message to " + connection.getJid() + " to talk to me.");
        }

    }
};

connection.connect("me@myserver.com", "password", callback);

2 个答案:

答案 0 :(得分:1)

更改行

connection.addHandler(null, null, "message", null, null, handler);

connection.addHandler(null, "message", null, null, null, handler);

它应该可以正常工作。

答案 1 :(得分:0)

你能在这里发布你如何连接gwt-strophe(如果你成功连接)? 或者如果您找到更好的解决方案,请在此处发布。我从gwt-strophe(包括gwt.xml和所有来源)制作了GWT兼容模块,并在我的GWT项目中使用。在编译期间没有错误,但当我打电话给我的小部件时,它说&#34;无法读取属性&#39;连接&#39;未定义&#34;。经过一些代码检查后,我没有找到Strophe对象初始化的地方

private native JavaScriptObject connection(String boshService) /*-{
    var connection = new $wnd.Strophe.Connection(boshService);
    return connection;
}-*/;

在运行时执行期间抛出错误,因为window.Strophe对象未定义 附:我还没有在这里找到如何添加评论,所以我已经做了回答&#34;在这个帖子中提问... 我只需要从GWT到我的openfire服务器的连接