4
votes

I attempted to find an answer to my question with the search engine but i was unable to.

I'm using strophe.muc.js in my backboen project in order to make it real time website. And now i am trying to attach a Session, so that if a page get reloaded, no new connection get created.

function Chat(){
  var self = this;
  this.connection = false;
  this.jid = false;
  this.BOSH_SERVICE = 'http://183.155.10.55:7070/http-bind/';

  this.init = function () {
    self.connection = new Strophe.Connection(this.BOSH_SERVICE);
    self.connection.xmlInput = this.log;
    self.connection.xmlOutput =  this.log;

    if(isObjectNull(sessionStorage.getItem('rid')) && isObjectNull(sessionStorage.getItem('sid')) && isObjectNull(sessionStorage.getItem('jid'))) {
        self.connection.connect(
            "",
            "",
            self.events.onConnect);

    }else{
        self.connection.attach(
            sessionStorage.getItem('jid'),
            sessionStorage.getItem('sid'),
            sessionStorage.getItem('rid'),
            self.events.onConnect);
    }

 };

 this.out = function (message) {
    $('#log').append('<br />').append(
        message
    );
 };

 this.log = function( message ) {
    console.log( message );
 };

 this.events = {
    "onConnect": function (status) {
        if (status == Strophe.Status.CONNECTING) {

        } else if (status == Strophe.Status.CONNFAIL) {

        } else if (status == Strophe.Status.DISCONNECTING) {

        } else if (status == Strophe.Status.DISCONNECTED) {

        } else if (status == Strophe.Status.CONNECTED  || status == Strophe.Status.ATTACHED) {
            self.connection.addHandler(function (msg) {
                return self.events.onMessage( msg );
            }, null, 'message', null, null, null);


            self.connection.send($pres().tree());

            self.groupchat.join();
        }
    },

    "onMessage": function( msg ) {
        if(jQuery(msg).attr("type") == "chat") {
        }
        return true;
    }

 };

 this.sendMessage = function( recipient, message ) {
    var reply = $msg({to: recipient, type: "chat"})
        .c("body")
        .t(message);
    console.log("SENDING MESSAGE: " + message);
    self.connection.send(reply.tree());
 };

 this.groupchat = {
    "_chatRoomId": WebConfig.ChatRoomID,
    "_chatRoomNick": function(){
        var randomGUID = generateGuid();
        return randomGUID;
    },
    "join":function () {

        sessionStorage.setItem("rid",self.connection.rid);
        sessionStorage.setItem("sid",self.connection.sid);
        sessionStorage.setItem("jid",self.connection.jid);


        try {
            self.connection.muc.join(
                this._chatRoomId,
                this._chatRoomNick(),
                this.incomingMessageHandler,
                this.groupPresenceHandler,
                null
            );

        } catch (e) {
            console.error(e);
        }

    },

    "message":function (msg) {
        try {
            self.connection.muc.groupchat(
                this._chatRoomId,
                msg,
                null
            );
        } catch (e) {
            $.error(e);
        }
    },

    "incomingMessageHandler": function ( msg ) {
        console.log("MESSAGE HANDLE");

        if(jQuery(msg).attr("type") == "chat") {
            self.out( "<strong>" + jQuery(msg).attr("from") + ": </strong>" + jQuery(msg).find("body:first").text());
        }
    },

    "groupPresenceHandler": function ( presence ) {
        console.log("PRESENCE HANDLE");
        console.log(presence);
     }
 };

 this.init();

};

Before using attach(), beside connection loses while refreshing page, everything works so well. But after I added attach(), I got an error `POST http://183.155.10.55:7070/http-bind/ 404 (Invalid SID value.)

1

1 Answers

2
votes

In case anyone has met the same problem as me, I've done it by adjusting my code above a little bit (changing the place off setting the rid, sid, and jid from join() to $(window).unload().

$(window).unload(function() {
     sessionStorage.setItem("rid",self.connection.rid);
     sessionStorage.setItem("sid",self.connection.sid);
     sessionStorage.setItem("jid",self.connection.jid);
});

No need to increase rid .