2016-05-17 5 views
1

私はこのサイトに掲載されたいくつかの同様の問題を発見したと言って始めます。彼らのどれも私の状況には適用されません。socket.io 3回発射する

私はサーバーとクライアントを持っています(node.js/socket.ioの標準です)、ボタンを押すとsocketイベントを送出します。これはうまく動作します...それは3回(少なくともサーバーが3回機能を実行する)を放出するように思われる。私はこの時点で長すぎるコードを見つめており、別の目を必要としています。

うまくいけば誰かがアイデアを持っていることを望みます。

クライアントコード:

importJS('/js/pages/admin_base.js',function(){ 
    var restartLMC = function(io){ 
     toggleLoad(); 
     var user = localStorage.getItem('User'); 
     io.emit('restart_request',{session: user}); 
    }; 
    AdminIO = new io('http://localhost:26266'); 
    AdminIO.on('restart_success',function(dat){ 
     toggleLoad(); 
     dropInfo(dat); 
    }); 
    AdminIO.on('sendError',function(dat){ 
     dropInfo(dat,{level: 'error'}); 
    }); 
    AdminIO.on('restart_fail',function(dat){ 
     toggleLoad(); 
     dropInfo(dat,{level: 'error'}); 
    }); 
    $('#restart').on('click',function(){ 
     restartLMC(AdminIO); 
    }); 
}); 

管理コード:これを見て、それに好奇心旺盛質問/状況を発見したかもしれない人のために

process.stdout.write('\033c'); 
console.log('\x1b[36m', "Admin server starting...", '\x1b[0m'); 
var 
    ini = require('node-ini') 
    , conf = ini.parseSync('../config.ini') 
    , CS = require('../lm_modules/CoreSync.js') 
    , CoreSync = new CS() 
    , checkSession = function (session, callback) { 
      var res; 
      if (!CoreSync) { throw "Fatal error, there is no connection to the Core service!"; } 
      if (CoreSync.sessions) { 
       if (CoreSync.sessions[session]) { 
        res = CoreSync.sessions[session]; 
        callback(res); 
       } 
       else { 
        CoreSync.sync('session', function (err, dat) { 
         if (CoreSync.sessions[session]) { 
          res = CoreSync.sessions[session]; 
          callback(res); 
         } else { res = false; callback(res); } 
        }); 
       } 
      } else { 
       res = false; callback(res); 
      } 
      if (res === "undefined") { callback(false); } 
     } 
    , runCMD = function(cmd,errCB,callback){ 
     var 
      command 
      , args; 
     if(cmd.cmd){ command = cmd.cmd; } else { command = cmd; } 
     if(cmd.args){ args = cmd.args; } 
     const spawn = require('child_process').spawn;   
     const ex = spawn(command, args); 
     ex.stdout.on('data', (data) => { 
      callback(data); 
     }); 
     ex.stderr.on('data', (data) => { 
      errCB(data); 
     }); 
     ex.on('close', (code) => { 

     }); 
    }  
    , executeCMD = function(cmd,callback){ 
     const exec = require('child_process').exec 
       , cdw = (__dirname + '/../'); 
     exec(cmd, {cwd: cdw}, (err, stdout, stderr) => { 
      if (err) { 
       callback(err,null); 
       return; 
      } 
      callback(stderr,stdout); 
     }); 
    }  
    , io = require('socket.io').listen(26266) // can use up to 26485 

console.log('\x1b[32m', "Admin server started.", '\x1b[0m'); 
console.log("Admin server listening at " + "http://" + conf["Server"]["binding"] + ":26266"); 
io.on('connection', function (socket) { 
    socket.on('restart_request', function(req){ 
     console.log('Recieved restart request');  
     var success = false 
      , session = JSON.parse(req.session) 
      , sessionID = session.sessionID; 
     checkSession(sessionID, function (ses) { 
      if (ses === false) { console.error('CheckSession failed: No session exists'); return; } 
      if (ses.user.uuid !== session.uuid) { console.error('CheckSession failed: UUID mismatched'); return; } 
      if (ses.user.role < conf['Permissions']['lm_restart']){ socket.emit('restart_fail','Insufficient permissions.'); return; } 
      if(process.platform === 'win32'){    
       executeCMD('cd',function(err,res){ 
        var errSent = false; 
        if(err){       
         console.error(err); 
         if(!errSent){ socket.emit('sendError','Restart failed'); } 
         errSent = true; 
         if(res === null){return;} 
        } 
        console.log(res); 
        socket.emit('restart_success','LM successfully restarted.'); 
       });     
      } 
      else if(process.platform === 'linux'){ 

      } 
     }); 
    }); 
}); 

答えて

0

...私は、この2つの部分を発見しました。

最初の部分は、バインド時の$()です。何らかの理由で(たとえそれがjsコードで何度も呼ばれていたとしても)、バインディングの前にunbind()を追加すると、問題が一部解決されました...余分な発光が3から2にカットされましたサーバーアプリケーションでは、3つまで戻ってきました...)

私が見つけた他の部分は、何らかの理由でsocket.io接続がソケットサーバーが実行されている回数だけ複製されていたことでした。この問題の詳細here ...この問題の原因が見つかったら、私の問題は解決されると思います。

関連する問題