nodejs使用socket.io传输大文件,一上传,就重连......
采用分片上传
服务端:
io.on('connection', (socket) => {
console.log('socket===>>>', socket.id);
socket.on('upload', (msg) => {
console.log('upload===>>>', msg);
uploadio(msg, socket);
});
socket.on('merge', (msg) => {
console.log('merge===>>>', msg);
merge(msg, socket);
});
});
客户端:
const that = this;
this.socket = io('', {
query: { room: 'upload', userId: `client_${Math.random()}` }
});
this.socket.on('connect', function() {
console.log('连接成功');
});
// socket.on("event", function (data) {});
this.socket.on('disconnect', function(err) {
console.log(`断开连接${err}`);
});
this.socket.on('reconnect', function(data) {
console.log(`重新连接${data}`);
});
this.socket.on('uploaded', () => {
that.chunkDoneTotal += 1;
if (that.chunkTotal === that.chunkDoneTotal) {
that.socket.emit('merge', {
chunkSize: that.chunkSize,
name: that.fileName,
total: that.chunkTotal,
hash: that.fileHash,
});
}
});
this.socket.on('done', () => {
showTips('success', '文件分片上传完结并合并成功');
});
发现上传小文件是可以的,只要一分片就必重连接
重连后,服务端,无法监听到upload事件,开始以为是缓冲区问题,设置了缓冲区大小还是不行
const io = require('socket.io')(server, { maxhttpbuffersize: 1024 1024 100 });
求问,是啥原因呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论