使用attach()时Strophe中出现404 Invalid SID value错误

4
我尝试使用搜索引擎找到我的问题的答案,但是没有成功。
我正在我的backboen项目中使用strophe.muc.js,以使其成为实时网站。现在我正在尝试附加一个会话,这样如果页面重新加载,就不会创建新的连接。
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();

};

在使用attach()之前,除了刷新页面时连接丢失外,一切都运行得很好。但是在添加attach()之后,我收到了一个错误`POST http://183.155.10.55:7070/http-bind/ 404 (Invalid SID value.)`。
1个回答

2

如果有人和我遇到了同样的问题,我通过微调我的代码解决了它(将设置ridsidjid的位置从join()更改为$(window).unload())。

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

不需要增加 rid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接