无法通过SocketIO NodeJS发送流。

huangapple go评论63阅读模式
英文:

Unable to send streams via SocketIO NodeJS

问题

如标题所示,我正在尝试通过SocketIO套接字发送数据流。

我正在编写一个Node TS应用程序,可能需要发送大文件,因此默认的Buffer对象不足够。

到目前为止,我有这段代码:

客户端

socket.emit("upload", file.stream(), (response:any) => {
    console.log(response);
})

服务器端

socket.on("upload", (data, callback) => {
    console.log(data)
})

客户端和服务器之间的连接正在工作,但是当上传文件时,服务器收到一个空对象而不是流。但在前端或后端代码中都没有出现错误。

有没有关于如何通过SocketIO发送数据流的想法?我已经研究过socket.io-stream,但我还没有成功在ES模块中使用它。

英文:

as the title implies I am trying to send streams of data through SocketIO sockets.

I am writing a Node TS application that may need to send large files so the default Buffer object isn't enough.

So far I have this code:

Client Side

socket.emit("upload", file.stream(), (response:any) => {
    console.log(response);
})

Server side

socket.on("upload", (data, callback) => {
    console.log(data)
})

The connection between the client and server is working, however when uploading files the server receives and empty object instead of the stream. But there's no error appearing in neither the front end or back end codes.

Any ideas on how to send stream through SocketIO? I have looked into socket.io-stream but I haven't managed to make it work with ES modules.

答案1

得分: 1

我为你编写了一个示例,使用了 socket.io-stream

server.ts:

import * as path from 'path';
import * as http from 'http';
import * as fs from 'fs';
import { Server } from 'socket.io';
import * as ss from 'socket.io-stream';

const httpServer = http.createServer();
const io = new Server(httpServer);

io.on('connection', (socket) => {
  ss(socket).on('upload', (stream, data) => {
    const filename = path.basename(data.name);
    stream.pipe(fs.createWriteStream(`./${filename}`));
    stream.on('end', () => {
      socket.emit('completed', '上传完成');
    });
  });
});

httpServer.listen(3000, () => {
  console.log('端口:3000');
});

client.ts:

import { io } from 'socket.io-client';
import * as ss from 'socket.io-stream';
import * as fs from 'fs';

const socket = io('http://localhost:3000');
const filePath = '../../../README.md'; // 文件路径
const file = fs.createReadStream(filePath);
const stream = ss.createStream();

ss(socket).emit('upload', stream, { name: filePath.split('/').pop() });
file.pipe(stream);

socket.on('completed', (message) => {
  console.log(message);
});

所以,我认为这个示例可以帮助你。

英文:

I write an example for you with socket.io-stream:

server.ts:

import * as path from 'path';
import * as http from 'http';
import * as fs from 'fs';
import { Server } from 'socket.io';
import * as ss from 'socket.io-stream';

const httpServer = http.createServer();
const io = new Server(httpServer);

io.on('connection', (socket) => {
  ss(socket).on('upload', (stream, data) => {
    const filename = path.basename(data.name);
    stream.pipe(fs.createWriteStream(`./${filename}`));
    stream.on('end', () => {
      socket.emit('completed', 'upload completed');
    });
  });
});

httpServer.listen(3000, () => {
  console.log('port:3000');
});

client.ts:

import { io } from 'socket.io-client';
import * as ss from 'socket.io-stream';
import * as fs from 'fs';

const socket = io('http://localhost:3000');
const filePath = '../../../README.md'; //file path
const file = fs.createReadStream(filePath);
const stream = ss.createStream();

ss(socket).emit('upload', stream, { name: filePath.split('/').pop() });
file.pipe(stream);

socket.on('completed', (message) => {
  console.log(message);
});

so, I think this example can help you.

huangapple
  • 本文由 发表于 2023年7月10日 23:59:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定