How to uniquely identify a socket with Node.js

后端 未结 7 1836
时光说笑
时光说笑 2020-12-10 01:25

TLDR; How to identify sockets in event based programming model.

I am just starting up with node.js , in the past i have done most of my coding part in C++ and PHP s

相关标签:
7条回答
  • 2020-12-10 02:30

    in typescript:

    import { v4 as uuidv4 } from 'uuid';
    import net from 'net';
    class Socket extends net.Socket {
      id?: string;
    }
    
    const server = net.createServer();
    
    server.on('connection', (conn) => {
      conn.id = uuidv4();
      conn.on('data', (data) => {
          console.log(conn.id);
      });
    });
    server.listen(3000);
    

    you need to add id first;

    0 讨论(0)
提交回复
热议问题