使用 GWT-Strope 的 GWT XMPP 客户端

发布于 2024-11-19 03:55:04 字数 2397 浏览 9 评论 0原文

我正在使用 GWT-Stropice 连接到我的 XMPP 服务器。一切进展顺利,我能够连接到我的 XMPP 服务器并向其他用户发送消息。我在接收消息时遇到问题。我正在尝试复制 Strope 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("[email protected]", "password", callback);

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 " + 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("[email protected]", "password", callback);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光清浅 2024-11-26 03:55:04

改变你的线路

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

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

它应该可以正常工作。

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.

墨小墨 2024-11-26 03:55:04

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

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

运行时执行期间抛出错误,因为 window.Strope 对象未定义
PS我在这里没有找到如何添加评论,所以我在这个帖子中提出了“答案”来提问......
我所需要的只是从 GWT 到我的 openfire 服务器的连接

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文