XMPP 和 Strophe

Posted

技术标签:

【中文标题】XMPP 和 Strophe【英文标题】:XMPP and Strophe 【发布时间】:2013-02-16 15:44:29 【问题描述】:

我想通过 Strophe 连接到 xmmp,我已经配置了任何东西

我在我的系统上使用 WAMP 来运行 localhost php

我在 openfire admin 中启用了脚本语法

我已将 APACHE httpd.conf 配置为

# XMPP proxy rule
ProxyRequests Off
ProxyPass /xmpp-httpbind http://127.0.0.1:7070/http-bind/
ProxyPassReverse /xmpp-httpbind http://127.0.0.1:7070/http-bind

我也使用基本的javascript文件连接到服务器,所以代码在这里

var BOSH_SERVICE = '/xmpp-httpbind'
var connection = null;

function log(msg) 

    $('#log').append('<div></div>').append(document.createTextNode(msg));


function rawInput(data)

    log('RECV: ' + data);


function rawOutput(data)

    log('SENT: ' + data);


function onConnect(status)

    if (status == Strophe.Status.CONNECTING) 
    log('Strophe is connecting.');
     else if (status == Strophe.Status.CONNFAIL) 
    log('Strophe failed to connect.');
    $('#connect').get(0).value = 'connect';
     else if (status == Strophe.Status.DISCONNECTING) 
    log('Strophe is disconnecting.');
     else if (status == Strophe.Status.DISCONNECTED) 
    log('Strophe is disconnected.');
    $('#connect').get(0).value = 'connect';
     else if (status == Strophe.Status.CONNECTED) 
    log('Strophe is connected.');
    connection.disconnect();
    


$(document).ready(function () 
    connection = new Strophe.Connection(BOSH_SERVICE);
    connection.rawInput = rawInput;
    connection.rawOutput = rawOutput;

    $('#connect').bind('click', function () 
    var button = $('#connect').get(0);
    if (button.value == 'connect') 
        button.value = 'disconnect';

        connection.connect($('#jid').get(0).value,
                   $('#pass').get(0).value,
                   onConnect);
     else 
        button.value = 'connect';
        connection.disconnect();
    
    );
);

当我想与 "admin@127.0.0.1" 连接时,我会进行 AUTHFAIL ,即使在使用 "admin" 之后,我也会收到以下回复:

JID:密码:

Strophe is connecting.
SENT: <body rid='1613006691' xmlns='http://jabber.org/protocol/httpbind' to='admin' xml:lang='en' wait='60' hold='1' content='text/xml; charset=utf-8' ver='1.6' xmpp:version='1.0' xmlns:xmpp='urn:xmpp:xbosh'/>
RECV: <body xmlns='http://jabber.org/protocol/httpbind' xmlns:stream='http://etherx.jabber.org/streams' authid='2ff1799d' sid='2ff1799d' secure='true' requests='2' inactivity='30' polling='5' wait='60' hold='1' ack='1613006691' maxpause='300' ver='1.6'><stream:features><mechanisms xmlns='urn:ietf:params:xml:ns:xmpp-sasl'><mechanism>DIGEST-MD5</mechanism><mechanism>PLAIN</mechanism><mechanism>ANONYMOUS</mechanism><mechanism>CRAM-MD5</mechanism></mechanisms><compression xmlns='http://jabber.org/features/compress'><method>zlib</method></compression><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></stream:features></body>
SENT: <body rid='1613006692' xmlns='http://jabber.org/protocol/httpbind' sid='2ff1799d'><auth xmlns='urn:ietf:params:xml:ns:xmpp-sasl' mechanism='ANONYMOUS'/></body>
RECV: <body xmlns='http://jabber.org/protocol/httpbind'><success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/></body>
SENT: <body rid='1613006693' xmlns='http://jabber.org/protocol/httpbind' sid='2ff1799d' to='admin' xml:lang='en' xmpp:restart='true' xmlns:xmpp='urn:xmpp:xbosh'/>
RECV: <body xmlns='http://jabber.org/protocol/httpbind' xmlns:stream='http://etherx.jabber.org/streams'><stream:features><compression xmlns='http://jabber.org/features/compress'><method>zlib</method></compression><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></stream:features></body>
SENT: <body rid='1613006694' xmlns='http://jabber.org/protocol/httpbind' sid='2ff1799d'><iq type='set' id='_bind_auth_2' xmlns='jabber:client'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'/></iq></body>
RECV: <body xmlns='http://jabber.org/protocol/httpbind'><iq xmlns='jabber:client' type='result' id='_bind_auth_2' to='pedram-pc/2ff1799d'><bind xmlns='urn:ietf:params:xml:ns:xmpp-bind'><jid>2ff1799d@pedram-pc/2ff1799d</jid></bind></iq></body>
SENT: <body rid='1613006695' xmlns='http://jabber.org/protocol/httpbind' sid='2ff1799d'><iq type='set' id='_session_auth_2' xmlns='jabber:client'><session xmlns='urn:ietf:params:xml:ns:xmpp-session'/></iq></body>
RECV: <body xmlns='http://jabber.org/protocol/httpbind'><iq xmlns='jabber:client' type='result' id='_session_auth_2' to='2ff1799d@pedram-pc/2ff1799d'/></body>
Strophe is connected.
Strophe is disconnecting.
SENT: <body rid='1613006696' xmlns='http://jabber.org/protocol/httpbind' sid='2ff1799d' type='terminate'><presence xmlns='jabber:client' type='unavailable'/></body>
RECV: <body xmlns='http://jabber.org/protocol/httpbind'/>
Strophe is disconnected.

我的问题在哪里?

【问题讨论】:

【参考方案1】:

您的日志显示:

Strophe is connected.
Strophe is disconnecting.

你的代码说:

 else if (status == Strophe.Status.CONNECTED) 
log('Strophe is connected.');
connection.disconnect();

所以......看起来一切都配置得很好,一旦成功连接,您的代码就会立即断开连接......

【讨论】:

以上是关于XMPP 和 Strophe的主要内容,如果未能解决你的问题,请参考以下文章

“xmlns:xmpp”和“xmpp:xmlns”有啥区别?

XMPP - 示例第 3 章“使用 JavaScript 和 jQuery 进行专业 XMPP 编程”不起作用

xmpp即时通讯协议的特性---长处和缺点!

如何创建nodejs xmpp服务器和android xmpp客户端

PHP 和 XMPP 双向

无法发送和接收 XMPP 消息