Skip to content

net: why net.Socket's property without pid info #20032

@MrTreasure

Description

@MrTreasure
  • Version:
  • Platform:
  • Subsystem:
  • Version: v8.9.3
  • Platform: win10 64bit

Why net create a IPC server, the coming socket without pid info. Under this condition I can't define which socket it is.Then I must send a message to tell server it's pid info. Code beblow here

const clientList: IClient[] = []
enum MSG_TYPE {
  INIT,
  DATA,
  COMMUNICATE
}

interface IClient {
  id: number | string
  socket: net.Socket
}

const handleClientMsg = (msg, current) => {
  switch (msg.type) {
    case MSG_TYPE.INIT:
      let client = clientList.find(item => item.socket === current)
      if (client) {
        client.id = msg.id
      }
      break
    
    case MSG_TYPE.COMMUNICATE: {
      let client = clientList.find(item => item.id === msg.id)
      if (client) {
        client.socket.write(msg.data)
      } 
    }
      break
    default:
      break
  }
}

server.on('connection', socket => {

  clientList.push({ id: undefined, socket })

  socket.on('data', function (msg) {
    let message = JSON.parse(msg.toString())
    handleClientMsg(message, this)
    console.log(this, this === socket)
    log(chalk.green(clientList.map(item => item.id).toString()))
  })
})

If soeckt has pid info, I can store it's pid info during connection event

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionIssues that look for answers.wrong repoIssues that should be opened in another repository.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions