本文介绍了404使用attach()时Strophe中的SID值无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用搜索引擎找到我的问题的答案,但我无法做到。

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

我正在使用 strophe.muc .js 在我的backboen项目中,以使其成为实时网站。现在我正在尝试附加一个Session,这样如果一个页面重新加载,就不会创建新的连接。

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

};

在使用attach()之前,在刷新页面时连接丢失,一切都运行良好。但是在我添加了attach()之后,我收到了一个错误`POST 404(无效的SID值。)

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.)

推荐答案

如果有人遇到了和我一样的问题,我已经通过稍微调整我的代码来完成它(更改地方设置 rid sid jid join() $(窗口).unload()

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

无需增加 rid

这篇关于404使用attach()时Strophe中的SID值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 01:27