处理Strophe js中的存在

时间:2015-07-20 11:46:38

标签: javascript jquery strophe

我有一个简单的聊天应用程序构建与strophe js。我正在展示在线的唯一用户。但问题是当用户上线时,几秒钟后用户自动离线。

这是我的代码:

function onConnect(status)
    {
        // Functions runs while users trys to login to the XMPP server

        var iq = null;

        switch (status)
        {
            case Strophe.Status.CONNECTING:
                log('Connecting.');
                break;
            case Strophe.Status.CONNFAIL:
                log('Failed to connect.');
                $('#connect').get(0).value = 'connect';
                break;
            case Strophe.Status.DISCONNECTING:
                log('Disconnecting.');
                break;
            case Strophe.Status.DISCONNECTED:
                log('Disconnected.');
                $('#connect').get(0).value = 'connect';
                break;
            case Strophe.Status.CONNECTED:
                log('Connected.');
                connection.addHandler(onMessage, null, 'message', null, null,  null);
                connection.addHandler(onPresence, null, 'presence', null, null, null);

                iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'});
                connection.sendIQ(iq, onRoster);

                break;
            default:
                break;
        }
    }
function onPresence(pres)
    {
        var fromJid = $(pres).attr("from"),
        fromBareJid = Strophe.getBareJidFromJid(fromJid),
        myBareJid = Strophe.getBareJidFromJid(connection.jid),
        type = $(pres).attr("type"),
        show = $(pres).children("show").text(),
        statusMsg = $(pres).children("status").text(),
        contactDropDown = $('#to-jid'),
        line;

        $.each(roster, function (index, rosterEntry) {
            if (rosterEntry.jid === fromBareJid) {
                if (type === "unavailable") {
                rosterEntry.presence = "offline";
                rosterEntry.message = null;
            } else {
                if (show) {
                    rosterEntry.presence = show;
                } else {
                    rosterEntry.presence = 'online';
                }
                if (statusMsg) {
                    rosterEntry.message = statusMsg;
                } else {
                    rosterEntry.message = null;
                }
            }
        }
    });

    showRoster();

    if (fromBareJid !== myBareJid) {
        if (type !== 'unavailable') {
            if (!_.contains(onlineContacts, fromBareJid)) {
                onlineContacts.push(fromBareJid);
            }

            line = fromBareJid + " is ";

            if (show) {
                line += show;
            } else {
                line += "online";
            }

            if (statusMsg) {
                line += ", \"" + statusMsg + "\"";
            }

            showMessage(line);
        } else {
            onlineContacts = _.reject(onlineContacts, function (jid) {
                return (jid === fromBareJid);
            });
            showMessage(fromBareJid + " is offline");
        }

        contactDropDown.empty();
        contactDropDown.append($("<option />").text("Choose a contact..."));
        $.each(onlineContacts, function (index, contact) {
            contactDropDown.append($("<option />").val(contact).text(contact));
        });
    }

    return true;
}

function onRoster(iq) {
    $(iq).find('item').each(function () {
        var jid = $(this).attr('jid'),
            name = $(this).attr('name'),
            show = "",
            rosterEntry = {
                jid: jid,
                name: name,
                presence: 'offline',
                message: null
            };

        roster.push(rosterEntry);
    });

    // showRoster();
    connection.send($pres().tree());
}

function showRoster() {
    rosterbox.val("");
    $.each(roster, function (index, rosterEntry) {
        var line = "";

        line += rosterEntry.jid;

        if (rosterEntry.name) {
            line += " (" + rosterEntry.name + ")";
        }

        line += ": " + rosterEntry.presence;

        if (rosterEntry.message !== null) {
            line += ", \"" + rosterEntry.message + "\"";
        }

        rosterbox.val(rosterbox.val() + line + "\n");
    });
}

使用此代码,用户将自动脱机。无法找到确切问题的位置。请帮忙

0 个答案:

没有答案