javascript - Strophe.js client connecting to server, disconnect/timeout -


i have little javascript xmpp client, written strophe, connects server hosted on hosted.im. think hosted.im uses ejabberd on backend.

i establish connection using

strophe.connection(myboshservice), , able send chat messages , forth. however, after time, seems, there automatic disconnect if there no activity.

now, question is, way keep session active, not disconnect. disconnect time seems short, 60 seconds or so.

should send kind of activity , forth keep open? or, seems simpler me, should somehow change timout of session. if so, can change this? server-setting, irregardless of strophe.connection object, or can set timeout when initializing strophe.connection?

thanks , help.

best regards,

chris

edit: here code use connecting:

i manage connection through global variable hello (yes, name awkward, took example):

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 " + + ".");     console.log("sending ping " + + ".");      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 server in " + elapsed + "ms.");     console.log('received pong 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; }  }; 

the main relevant objects here hello.connect , hello.partner, stores jid of person on accounts roster, 1 one chat.

then, in $(document).ready, bind 2 buttons connect , send messages respectively:

$(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()                                 }                     );                     });  }); 

clicking sign-in button triggers event "connect":

$(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; }); 

this creates strophe.connection , stores in hello.connection. also, sets callback function of connection object. code taken straight example in strophe.js book. anyway, callback checks status of connection, , if status === strophe.status.disconnected, triggers "disconnected", this:

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

anyway, happening that, reason, in callback set conn.connect, after short time, status evaluates strophe.status.disconnected, , not sure why, unless somewhere, either in server or in connection object, there timeout specified seems ca. 60 seconds.

as log of stanzas going , forth, guess need write handler see incoming stanzas, or possible see log of stanzas between client , server in ejabberd?

for sake of other people come upon , have similar problem, solution in case servers @ hosted.im send ping request every 60 seconds check if client still online.

this ping request looks this:

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

what needed, of course, form response, this:

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

note "to"-attribute. omitted @ beginning under assumption message sent no to-attribute automatically assumed client->server message. not in case however. not sure if case in general, or whether oddity of servers @ hosted.im.

thanks comments , suggestions!

best regards,

chris


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -