Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MrTreasure opened this issue Apr 14, 2018 · 1 comment
Closed

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

MrTreasure opened this issue Apr 14, 2018 · 1 comment
Labels
question Issues that look for answers. wrong repo Issues that should be opened in another repository.

Comments

@MrTreasure
Copy link

MrTreasure commented Apr 14, 2018

  • 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

@MrTreasure MrTreasure changed the title why net.Socket's property without pid info net: why net.Socket's property without pid info Apr 14, 2018
@bnoordhuis
Copy link
Member

Can you post your question to the nodejs/help repo? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that look for answers. wrong repo Issues that should be opened in another repository.
Projects
None yet
Development

No branches or pull requests

2 participants