Search code examples
node.jssocketstcpsocket.iotcpclient

using tcp sockets and socket.io sockets along side eachother


so i have a project i am making that communicates using 2 forms of sockets tcp sockets and web sockets using socket.io

when i fire up my server clients connect to it using tcp and when i open up my web interface it connects using socket.io (this is how i control the whole program) i do not know how to be able to write to the tcp sockets within a socket.io event and is it even possible here is some of my code

this is my tcp server

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

  })

});

and then under that i start my socket.io server

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

})

is there anyway that this is possible or am i just SOL. thanks in advance


Solution

  • so i did some thinking and was wondering why calling tcpClients[agent].socket.write('whatever'); wasnt working and i realized i didn't have the right information stored in my tcpClients array i was just storing sock.id as socket instead of the entire socket instance made from the net library :) so now i can call it just like this

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

    and it seems to work like a charm