Strophe.js客户端连接到服务器,断开连接/超时

Strophe.js client connecting to server, disconnect/timeout

本文关键字:连接 断开 超时 js 客户端 Strophe 服务器      更新时间:2023-09-26

我有一个用Strophe编写的JavaScript XMPP小客户端,它连接到hosted.im上托管的服务器。

我使用建立连接

CCD_ 1并且能够来回发送聊天消息。然而,在一段时间后,如果没有活动,似乎会自动断开连接。

现在,我的问题是,什么是保持会话活动的好方法,这样它就不会断开连接。断开连接的时间似乎很短,大约60秒左右

我应该来回发送一些活动来保持它的开放性吗?或者,在我看来更简单的是,我是否应该以某种方式改变会议的时间安排。如果是,我在哪里可以更改?这是一个服务器设置,与Strophe.Connection对象无关,还是我可以在初始化Strophe.Cnnection时设置超时?

谢谢你的帮助。

致问候,

Chris

编辑:这是我用于连接的代码:

我通过一个全局变量Hello来管理连接(是的,名称很尴尬,我从一个例子中得到了它):

var Hello = {
connection: null,
start_time: null,
partner: {
    jid: null,
    name: null
},
log: function (msg) {
    $('#log').append("<p>" + msg + "</p>");
},
send_ping: function (to) {
    var ping = $iq({
        to: to,
        type: "get",
        id: "ping1"}).c("ping", {xmlns: "urn:xmpp:ping"});
    Hello.log("Sending ping to " + to + ".");
    console.log("Sending ping to " + to + ".");
    Hello.start_time = (new Date()).getTime();
    Hello.connection.send(ping);
},
handle_pong: function (iq) {
    var elapsed = (new Date()).getTime() - Hello.start_time;
    Hello.log("Received pong from server in " + elapsed + "ms.");
    console.log('Received pong from server in " + elapsed + "ms.');
    $('#login').hide();
    $('#chat').show();
    //window.location = "chat.html";
    //Hello.connection.disconnect();
    return true;
}, 
//"<active xmlns="http://jabber.org/protocol/chatstates"/><body xmlns="http://jabber.org/protocol/httpbind">tuiuyi</body>"
displayIncomingText: function (text) {
    var body = $(text).find("xml > body");
    if (body.length === 0)
    {
        body = $(text).find('body');
        if (body.length > 0) 
        {
            body = body.text();
            $('#chattext').append("<p>"+ body + "</p>");
        }
        else
        {
            body = null;
        }
    }
    return true;
},
readRoster: function (iq) {
    $(iq).find('item').each(function () {
        var jid = $(this).attr('jid');
        var name = $(this).attr('name') || jid;
        Hello.partner.name = name;
        Hello.partner.jid = jid;
    });
    return true;
}
};

这里的主要相关对象是Hello.connect和Hello.parther,它们存储账户名册上唯一一个人的jid,因为这是一对一聊天。

然后,在$(document).ready中,我绑定了两个按钮,分别连接和发送消息:

$(document).ready(function () {
$('#chat').hide();
$('#chatSend').bind('click', function () {
    Hello.connection.send(
        $msg(
            {to : Hello.partner.jid, type : 'chat'}
            ).c('body').t($('#chattextinput').val())
            );
            $('#chattext').append("<p align='right'>" + $('#chattextinput').val() + "</p>");
    });

$('#SignIn').bind('click', function () {
$(document).trigger('connect', { 
                                jid: $('#eMail').val(), password: $('#password_f').val()
                                }
                    );
                    });
});

点击登录按钮触发事件"连接":

$(document).bind('connect', function (ev, data) {
console.log('connect fired');
var conn = new Strophe.Connection("http://bosh.metajack.im:5280/xmpp-httpbind");
conn.connect(data.jid, data.password, function (status) {
    console.log('callback being done');
    if (status === Strophe.Status.CONNECTED) {
        alert('connected!');
        $(document).trigger('connected');
        alert('Connected successfully');
    } else if (status === Strophe.Status.DISCONNECTED) {
        $(document).trigger('disconnected');
    }
    else
    {
        Hello.log("error");
        console.log('error');
    }
});
Hello.connection = conn;
});

这创建了Strophe.Connection并将其存储在Hello.Connection中。此外,它还设置了连接对象的回调函数。这段代码直接取自Strophe.js书中的一个例子。无论如何,回调会检查连接的状态,如果status==Strophe.status.DISCONNECTED,则触发"DISCONNECTED",它只会执行以下操作:

$(document).bind('disconnected', function () {
Hello.log("Connection terminated.");
console.log('Connection terminated.');
// remove dead connection object
Hello.connection = null;
});

无论如何,由于某种原因,在使用conn.connect设置的回调中,短时间后,状态评估为Strophe.status.DISCONNECTED,我不知道为什么,除非在服务器或连接对象中的某个地方指定了一个似乎为60秒的超时。

至于来回的节日志,我想我需要快速编写一个处理程序来查看所有传入的节,或者可以在ejabberd中查看客户端和服务器之间的所有节的日志吗?

为了其他遇到类似问题的人,本例的解决方案是hosted.im的服务器每60秒发送一个ping请求,以检查客户端是否仍在线。

这个ping请求看起来像这样:

<iq from="testserver.p1.im" to="chris@testserver.p1.im/23064809721410433741569348" id="164323654" type="get"> <ping xmlns="urn:xmpp:ping"></ping> </iq>

当然,需要的是形成一个响应,它看起来像这样:

<iq from="chris@testerver.p1.im" to="testserver.p1.im" id="164323654" type="result" xmlns="jabber:client"><ping xmlns="urn:xmpp:ping"/></iq>

注意"to"-属性。一开始我省略了它,因为我假设发送的带有no to属性的消息会自动被假设为客户端->服务器消息。然而,在这种情况下并非如此。不确定这是否是一般情况,或者这是否是hosted.im的服务器的一个奇怪现象。

感谢大家的意见和建议!

致问候,

Chris