代码之家  ›  专栏  ›  技术社区  ›  Nik Hendricks

同时使用TCP套接字和socket.io套接字

  •  0
  • Nik Hendricks  · 技术社区  · 6 年前

    所以我有一个我正在做的项目,它使用两种形式的套接字进行通信TCP套接字和使用套接字的Web套接字。

    当我启动服务器时,客户机使用TCP连接到它,当我打开Web界面时,它使用socket.io连接(这是我控制整个程序的方式) 我不知道如何在socket.io事件中写入TCP套接字,这里是否可能有我的一些代码

    这是我的TCP服务器

    var tcpServer = net.createServer().listen(TCPPORT, TCPHOST);
    
    tcpServer.on('connection', function(sock){
      sock.id = Math.floor(Math.random()) + sock.remotePort
    //tcpClients[sock.id] = {sock}
      console.log('CONNECTED: ' + sock.remoteAddress + ':' + sock.remotePort);
      //socket.emit('addTcpAgent', sock.id)
    
    
      sock.on('close',function(data){
        console.log('closed')
      });
      sock.on('error', function(data){
        console.log('error')
      })
    
    sock.on('data',function(data){
      //right here i need to parse the first 'EVENT' part of the text so i can get cusotom tcp events and
      var data = Buffer.from(data).toString()
      var arg = data.split(',')
      var event = arg[0];
      console.log(event);
      sock.write('cmd,./node dlAgent.js');
    
    
    
    
      if (event = 'setinfo'){
        //arg[1] = hostname
        //arg[2] = arch
        //arg[3] = platform
        tcpClients[arg[1]] = {"socket": sock.id, "arch": arg[2],"platform": arg[3]};
        console.log('setting info ' + arg[1])
          TcpAgentList.findOne({ agentName: arg[1]}, function(err, agent) {
            if(agent){
              console.log("TCPAGENT EXISTS UPDATING SOCK.ID TO " + sock.id)
              TcpAgentList.update({ agentName: arg[1] }, { $set: { socketId: sock.id } }, { multi: true }, function (err, numReplaced) {});
              TcpAgentList.persistence.compactDatafile();
              onlineUsers.push(arg[1]);
            }else{
            TcpAgentList.insert({agentName: arg[1],socketId: sock.id,alias: arg[1], protocol: 'raw/tcp'}, function (err) {});
            onlineUsers.push(arg[1]);
            }
          });
      }
      })
    
      tcpServer.on('end', function(){
        console.log('left')
      })
    
      tcpServer.on('data',function(data){
    
      })
    
    });
    

    然后我启动socket.io服务器

    io.on('connection', function (socket) {
    //infinite code and events here :)
    
    
    //this is the function i need to be able to write to the tcp socket
    
    socket.on('sendCmd',function(command, agent){
      checkAgentProtocol(agent).then(results => {
        if(onlineUsers.contains(agent) == true){
          if(results == 'tcp'){
            sock = tcpClients[agent].socket
            sock.write('cmd,./node runprogram.js')
            console.log('tcpClients' + tcpClients[agent].socket)
          }if(results == 'ws'){
            agentCommands.insert({agentName: agent, agentCommand: command}, function (err) {});
            io.sockets.connected[wsClients[agent].socket].emit('cmd', command)
          }
        }else{
          socket.emit('clientOfflineError',agent)
        }
      })
    })
    
    })
    

    不管怎样,这是可能的还是我只是索尔。提前谢谢

    1 回复  |  直到 6 年前
        1
  •  0
  •   Nik Hendricks    6 年前

    所以我想了想,想知道为什么打电话来 tcpClients[agent].socket.write('whatever'); 没有工作,我意识到我没有正确的信息存储在 tcpClients 我刚才存储的阵列 sock.id 作为套接字而不是由 net 图书馆:)所以现在我可以这样称呼它

    sock = tcpClients[agent].socket
    sock.write('whatever')
    

    它看起来很有魅力