通过socket.io通过websockets传输图像时负载很重

我想通过基于NodeJS的后端将图像从一个客户端传输到另一个客户端。 我每40毫秒发送一个图像到后端,然后将图像发送给另一个客户端。 我的问题是,每个客户端在后端增加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); }); }); 

一个工作示例可以在这里find: https : //code.flyacts.com/tsc/socket-io-performance-problems/

这是预期的行为? 还是我做错了什么?