使用GWT Strophe的GWT XMPP客户端

使用GWT Strophe的GWT XMPP客户端,gwt,strophe,jsni,Gwt,Strophe,Jsni,我正在使用连接到我的XMPP服务器。一切进展顺利,我能够连接到我的XMPP服务器并向其他用户发送消息。我在接收消息时遇到问题。我试图复制Strophe echobot示例,但在收到消息时,处理程序中的代码没有得到执行 下面是我用来连接和注册处理程序的代码: connection = new Connection("http://localhost/proxy/"); handler = new Handler<Element>() { @Override publi

我正在使用连接到我的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);
connection=新连接(“http://localhost/proxy/");
handler=newhandler(){
@凌驾
公共布尔句柄(元素){
GWT.log(“处理…”);
log(element.toString());
字符串to=element.getAttribute(“to”);
字符串from=element.getAttribute(“from”);
字符串类型=element.getAttribute(“类型”);
NodeList elems=element.getElementsByTagName(“body”);
if((type==null?“chat”==null:type.equals(“chat”)&&elems.getLength()>0){
元素体=(元素)elems.getItem(0);
log(“ECHOBOT:我从“+from+”:“+body.getText())收到一条消息);
字符串[][]属性={{“to”,from},{“from”,to},{“type”,“chat”};
Builder reply=Builder.$msg(attributes).cnode(body.copy());
connection.send(reply.tree());
log(“ECHOBOT:I发送了”+from+:“+body.getText());
}    
返回true;
}
};
StatusCallback callback=新建连接。StatusCallback(){
@凌驾
公共作废状态已更改(状态状态、字符串原因){
if(status==status.CONNECTING){
log(“Strophe正在连接”);
}else if(status==status.CONNFAIL){
log(“Strophe未能连接”);
}else if(状态==状态.断开连接){
GWT.log(“Strophe正在断开连接”);
}else if(状态==状态.已断开){
GWT.log(“Strophe已断开”);
}else if(status==status.CONNECTED){
GWT.log(“Strophe已连接”);
addHandler(null,null,“message”,null,null,handler);
Builder pres=Builder.$pres(空);
连接发送(pres);
log(“ECHOBOT:发送消息到“+connection.getJid()+”与我交谈。”);
}
}
};
连接。连接(“me@myserver.com“,”密码“,回调);
更改您的线路

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


它应该可以正常工作。

您能在这里发布您是如何连接gwt strophe的(如果您成功连接的话)? 或者,如果你发现了更好的解决方案,请在这里发布。我已经从GWT strophe(包括GWT.xml和所有源代码)制作了与GWT兼容的模块,并在我的GWT项目中使用。在编译过程中并没有出现错误,但当我调用我的小部件时,它说“无法读取未定义的属性‘连接’”。经过一些代码检查,我没有找到Strophe对象的初始化位置

private native JavaScriptObject connection(String boshService) /*-{
    var connection = new $wnd.Strophe.Connection(boshService);
    return connection;
}-*/;
运行时执行期间引发错误,因为window.Strophe对象未定义 p、 我在这里还没有找到如何添加评论,所以我在这个帖子中给出了问题的“答案”。。。 我只需要从GWT连接到我的openfire服务器

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