0

我有以下代码,因为有 N 个套接字发送顺序流。如何使用 nodejs 中的工作线程来扩展它。

const Net = require('net');
const port = 8080;
const server = new Net.Server();
const processor = require('processor.js');

server.listen(port, function() {
    console.log(`Server listening for connection requests on socket localhost:${port}`.);
});

server.on('connection', function(socket) {
    console.log('A new connection has been established.');
    
    let connectionInfo = {};
    connectionInfo.id = `${socket.remoteAddress}:${socket.remotePort}`;
    connectionInfo.partialBuffer= undefined;

    socket.on('data', function(chunk) {
        let chunkBuffer = Buffer.from(chunk);
        if(connectionInfo.partialBuffer){
            chunkBuffer = Buffer.concat([connInfo.partialBuffer, chunkBuffer]);
        }
        processor.processChunk(connectionInfo, chunkBuffer); // set connectionInfo.partialBuffer in processChunk() function 
    });

    socket.on('end', function() {
        console.log('Closing connection with the client');
    });

    socket.on('error', function(err) {
        console.log(`Error: ${err}`);
    });
});
4

0 回答 0