我正在使用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,以便服务器知道它已经接收到所有数据并可以对数据进行一些处理。
预先感谢