1

我正在使用 ws npm 模块创建 websocket 客户端并使用 send() 方法将大数据流发送到 websocket 服务器

我很困惑如何让服务器知道客户端已经发送了所有数据并且服务器可以开始处理,即如何从客户端发送 EOF?

这是我的服务器:

import WebSocket = require('ws');
import fs = require('fs');
var port: number = 8989;
var WebSocketServer = WebSocket.Server;
var wss = new WebSocketServer({ port: port });

wss.on('connection', (ws: WebSocket) => {
    console.log('Web Socket Server Connected');

    ws.on('close', function() {
        console.log("Connection closed by client");
        //ws.close();
    });

     //connection is up, let's add a simple simple event
    ws.on('message', (message: string) => {

        console.log('Data received from client');
        ws.send(message);
        } catch(error) {
            console.log('Connection is closed');
        }
    });

    //send immediatly a feedback to the incoming connection    
    ws.send('Hi there, I am a WebSocket server');
});

console.log(`Server started on port 8989`);

这是我的客户代码:

const ws = new WebSocket('ws://localhost:8989/');

      try {
        ws.on('open', function open() {
          console.log('Connected to Server');
          ws.send(streamData);
        });
      } catch (error) {
      }

      ws.on('message', (returnData: Buffer) => {

        // tslint:disable-next-line:no-console
        console.log('Message from Server: ' + returnData.length);

        console.log('Data received from server');
      });
      ws.on('close', function close() {
        // tslint:disable-next-line:no-console
        console.log('Closing connection');
      });

    });

我只是将 streamData 从客户端发送到服务器,并且我正在将数据从服务器回显到客户端。

我想知道如何将 EOF 从客户端发送到服务器,以便服务器知道它已收到所有数据并可以对数据进行一些处理。

提前致谢

4

0 回答 0