Sending files through net.socket

前端 未结 1 1597
孤城傲影
孤城傲影 2021-01-25 18:46

I trying to create a socket server over the net-module in node.js. There should be different clients connect to it and be able to send/receive commands which will be evaluated o

相关标签:
1条回答
  • 2021-01-25 19:37

    Since TCP is emulating a stream, you don't want to rely on anything about how the stream is broken into separate data events. The data given to one callback could be the first half of something or 2 things.

    Instead, you want to emulate your datagram protocol on top of this reliable stream by appending stream contents to the end of a buffer and removing complete messages from the front for processing.

    For example, this simple server from the docs, demonstrates a minimal valid TCP server:

    const net = require('net');
    
    const server = net.createServer((socket) => {
      let name = '';
      socket.setEncoding('utf8');
      socket.on('data', (chunk) => name += chunk);
      socket.on('end', () => socket.end(`hello ${name}`));
    });
    
    server.listen(8000);
    
    • It assembles a buffer with no assumption about the number of data call(s) in its simple case, the buffer is a single message to use at the end event.
    • To process messages before the end of the connection, you also want to examine the front of the buffer at the end of every data event to look if some messages are complete and ready to process. This separation of complete messages needs to be part of your protocol.

    While message separation can be done by length indicators or reserved sequences, reserved sequences require encoding files (to avoid accidentally seeing them in data) and scanning data continuously to find them. This makes length indicators preferable for dealing with the file data.

    So for example, the file [data] response first becomes file [#####] [data] where ##### tells you how much data to keep assembling on the buffer before a data callback will remove this entire message from the front of the buffer for processing as a fileSave().

    Then, to handle more granular interactivity, simply break up these operations into separate smaller interactions, for example replace file [wholefilecount] [data] responses with filechunk [0-maxchunk] [data] responses that require a filenext command to continue and send a fileend to tell the client the file is done.

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