GWT XMPP client using GWT-Strophe
I'm using GWT-Strophe to connect to my XMPP server. Things are going well and I am able to connect to my XMPP server and send other users messages. I'm having a problem with receiving messages. I'm attempting to copy the Strophe echobot example, but the code in my Handler is not getting executed when a message is received.
Here is the code I am using to connect and register the handler:
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 " + 开发者_JAVA百科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);
Change your line
connection.addHandler(null, null, "message", null, null, handler);
with
connection.addHandler(null, "message", null, null, null, handler);
and it should work fine.
Can you post here how you connected gwt-strophe (if you successfully connected)? Or if you found better solution please post it here. I've made GWT compatible module from gwt-strophe(included gwt.xml and all sources) and used in my GWT project. During compilation there was no error but when i called my widget it says "Cannot read property 'Connection' of undefined". After some code inspection i didn't found where Strophe object is initialized
private native JavaScriptObject connection(String boshService) /*-{
var connection = new $wnd.Strophe.Connection(boshService);
return connection;
}-*/;
Error thrown during runtime execution because window.Strophe object is undefined p.s. i haven't found here how to add comment so i've made "answer" to ask question in this thread... All i need is connection from GWT to my openfire server
精彩评论