我想通过基于NodeJS的后端将图像从一个客户端流式传输到另一个客户端。我每隔40ms向后端发送一个图像,然后将图像发送到另一个客户端。我的问题是,每个客户端对在后端增加了5%的负载。每张图片大约20KB。
我的前端代码如下所示:
const socket = require('socket.io-client')('http://localhost:3000');
const fs = require('fs');
socket.on('connect', function () {
console.log('connected');
socket.emit('room', '123456');
});
socket.on('joined', () => {
console.log('joined the room');
const file = fs.readFileSync('./1902865_597805016961665_1536612023_n.jpg');
setInterval(() => {
socket.emit('frame', file);
}, 40);
});
后端看起来像这样:
const app = require('express')();
const http = require('http').Server(app);
const io = require('socket.io')(http);
http.listen(3000, function () {
console.log('listening on *:3000');
});
io.on('connection', socket => {
socket.on('room', function (room) {
console.log(`got join request for ${room}`);
socket.join(room);
socket.myroom = room;
socket.emit('joined');
});
socket.on('disconnect', function () {
socket.to(socket.myroom).emit('disconnected');
socket.disconnect();
});
socket.frameCounter = 0;
socket.on('frame', frame => {
console.dir(`frameCounter ${socket.frameCounter}`);
socket.frameCounter += 1;
socket.to(socket.myroom).emit('frame', frame);
});
});
可在此处找到一个工作示例:https://code.flyacts.com/tsc/socket-io-performance-problems/
这是预期的行为吗?或者我做错了什么?