socket io大规模客户端断开连接

时间:2015-03-13 09:44:14

标签: ios node.js socket.io

我正在运行带有socket.io的nodejs服务器,用于客户端之间的实时通信,并且突然所有客户端(iPad)都断开连接。除了运行"仪表板"在Mac上。服务器没有崩溃,之后客户端没有重新连接。我不得不重新启动节点服务器,然后重新连接所有节点服务器。 有没有人遇到这样的事情?如果重要的话,我可以提供代码。客户端正在运行WebApp。 服务器:

`io.on('connection', function(socket){
var id = socket.id;
writeTo("User Connected! Group: "+socket.id);

socket.emit('responseID' , { socketId : id } );

socket.on( 'registerGroup' , function(data){
    var group;
    if(data.groupId!=''){
        group = data.groupId;
        socket.emit('registerIDRes' , { groupId : group } );
    }else{
        groupCount ++;
        group = groupCount;
        socket.emit('registerIDRes' , { groupId : group } );
    }

    groups[socket.id] = group;
    $("#log_wrapper").prepend('<div class="alert alert-success" role="alert">Group Register: '+group+'</div>');
    writeTo("Group Register: "+group);
});

/**
 * Pause Group Request
 */
socket.on( 'pauseGroupReq' , function(data){
    console.log("pauseReq");
    io.sockets.emit('pauseGroupRes' , { groupId : data.groupId , option : data.option } );
    $("#log_wrapper").prepend('<div class="alert alert-info" role="alert">Pause Group: '+data.groupId+' , Option : '+data.option+'</div>');
    writeTo("Pause Group: "+group);
});

/**
 * Pause All Request
 */
socket.on( 'pauseAllReq' , function(data){
    console.log("pauseAll");
    io.sockets.emit('pauseAllRes' , { option : data.option } );
    $("#log_wrapper").prepend('<div class="alert alert-info" role="alert">Pause All Groups!</div>');
    writeTo("Pause All Groups");
});

/**
 * Send Answer Request
 */
socket.on( 'sendAnswerReq' , function(data){
    console.log("send Answer Req");
    io.sockets.emit('sendAnswerRes' , data );
    $("#log_wrapper").prepend('<div class="alert alert-info" role="alert">Answer Received! Group: '+data.igroup+'!</div>');
    writeTo("Answer Received!Chapter: "+data.ichapter+data.isubchapterletter+" Group "+data.igroup+".  Answer: "+data.raw);
});

/**
 * Unlock Waiting Request
 */
socket.on( 'unlockWaitingReq' , function(data){
    console.log("Unlock Request");
    io.sockets.emit('unlockWaitingrRes' , data );
    $("#log_wrapper").prepend('<div class="alert alert-info" role="alert">Unlocking screen!</div>');
    writeTo("Screen Unlocked!");
});

/**
 * Show Video Request
 */
socket.on( 'videoReq' , function(data){
    console.log("Unlock Request");
    io.sockets.emit('videoRes' , data );
    $("#log_wrapper").prepend('<div class="alert alert-info" role="alert">Show Video!'+data.action+'</div>');
    writeTo("Show Video! "+data.action);
});

/**
 * Project Request
 */
socket.on( 'projectReq' , function(data){
    console.log("Project Request");
    console.log(data);
    io.sockets.emit('projectRes' , data );
    $("#log_wrapper").prepend('<div class="alert alert-info" role="alert">Projecting!</div>');
    writeTo("Project On Screen!");
});

socket.on('disconnect' , function(){
    $("#log_wrapper").prepend('<div class="alert alert-danger" role="alert">User Disconnected: '+groups[socket.id]+'</div>');
    writeTo("User Disconnected! "+socket.id);
});

/**
 * Reset Course
 */
socket.on( 'resetCourse' , function(){
    console.log("resetCourse");
    $("#log_wrapper").html('');
    writeTo("Reset Course!");
});
});`

客户端:

`/* Request socket to the server */
this.socket = io.connect(host,{'forceNew':true });

/**
 * Register ID
 * msg : { id : 'Int' }
 */
this.socket.on('responseID',function(msg){
    this.id = msg.socketId;
    var msg = {};
    if(localStorage.getItem("groupId")!=undefined){
        if(group != 0){
            msg.groupId = group;
        }else{
            msg.groupId = localStorage.getItem("groupId");
        }
    }else{
        console.log(group);
        if(group != 0){
            msg.groupId = group;
        }else{
            msg.groupId = '';
        }
    }
    this.emit('registerGroup' , msg );
});

/**
 * Register Group ID
 */
this.socket.on( 'registerIDRes' ,function(msg){
    group = msg.groupId;
    localStorage.setItem('groupId' , msg.groupId );
    console.log(group);
    groupOk();
});

/**
 * Possible deprecated for conferences
 * Pause This Group
 * msg : { group : 'Int' , option : 'on/off' }
 */
this.socket.on('pauseGroupRes',function(msg){
    console.log("Pause Req!");
    if(group == msg.groupId){
        if (msg.option=='on') {
            $('#pause_screen').show();
        } else {
            $('#pause_screen').hide();
        }
    }
});

/**
 * Pause All
 * msg : { option : 'on/off' }
 */
this.socket.on('pauseAllRes', function(msg){
    console.log("Pause Msg Received!");
    if (msg.option=='on') {
        $('#pause_screen').show();
    } else {
        $('#pause_screen').hide();
    }
});

/**
 * UnlockScreen
 * msg : { auto : 'on/off' }
 */
this.socket.on('unlockWaitingrRes',function(msg){
    console.log(chaptersubchapterletter);
    console.log(msg.screenId);
    if(!$('#contentContinue').is(":visible")){
        $('#contentContinue').removeClass().addClass('contentContinue');
        if(msg.auto && chaptersubchapterletter == msg.screenId){
            $('#contentContinue').trigger(clickevent);
        }/* False implement*/
    }
});

/**
 * Skip To
 * msg : { module : 'Int' }
 */
this.socket.on('skipto',function(msg){
    subchapter = parseInt(msg.module)-1;
    ChangePage(1);
});

/**
 * Sends Answer To the Dashboard
 * moduleId : 1a,1b,1c...
 * @param JSON answer
 */
this.sendAnswer = function(answer , chapter , subchapter , raw) {
    this.socket.emit('sendAnswerReq' , { igroup : group , icontenthtmlinn : answer , ichapter : chapter , isubchapterletter : subchapter ,raw :raw});
};`

在日志中:

`Group Register: 18
Answer Received!Chapter: 1e Group 17.  Answer: "   {\"name\":\"write\",\"answer\":[\"God Komunikasjon\",\"Bli bedre på  Formidling av budskap. \",\"Fremstå som en seriøs aktør utad.\",\"Åpenhet om selskapet/status. \"]}"
Answer Received!Chapter: 1e Group 16.  Answer: " {\"name\":\"write\",\"answer\":[\"By på seg selv \",\"ta eierskap \",\"Kommunisere og dele på tvers\",\"fremoverlent\"]}"
Project On Screen!
Project On Screen!
Answer Received!Chapter: 1e Group 8.  Answer: "{\"name\":\"write\",\"answer\":[\"Felles forståelse for hvor vi er og skal\",\"Prestasjonsfokusert\",\"Modig og innovativ\",\"Lojal\"]}"
Answer Received!Chapter: 1e Group 11.  Answer: "{\"name\":\"write\",\"answer\":[\"Eventyrlyst, utforske\",\"Mer pågående\",\"Å tørre, sterk vilje\",\"Stolt, entusiastisk\"]}"
Answer Received!Chapter: 1e Group 9.  Answer: "{\"name\":\"write\",\"answer\":[\"Skape et godt arbeidsmiljø\",\"Retningsorentert\",\"Gi tillit og selvstendighet\",\"Levere\"]}"
User Disconnected! evMCg5tCgExXqHUvAAAX
User Disconnected! pFXWmPFNrAqMkZctAAAY
User Disconnected! 5wzBafgilF2RQLC3AAAZ
User Disconnected! rKs0T389UBKgNiwkAAAa
User Disconnected! gnMDytiyk4vEkzliAAAc
User Disconnected! kViwnytGi9dM-sp0AAAd
User Disconnected! 7ptMHQy3YN-GcFTyAAAe
User Disconnected! YiS0gKH3IWwV4LG1AAAf
User Disconnected! uTlRPsb58Ar8N-hJAAAg
User Disconnected! g63h0-qq2DeZATscAAAh

`

0 个答案:

没有答案