简体   繁体   English

视频 stream 在 NodeJS 中从客户端到服务器

[英]Video stream from client to server in NodeJS

I'm currently developing an application to stream video feed from client to server in NodeJS.我目前正在开发一个应用程序,用于 stream 视频提要,在 NodeJS 中从客户端到服务器。 The server side code is as shown below服务端代码如下图

server.js服务器.js

const express = require('express')
const app = express()
const morgan = require('morgan')
const server = require('http').Server(app)
const io = require('socket.io')(server)
const auth = require('./routes/auth')
const video = require('./routes/videoproc')(io)


const bodyParser = require('body-parser')


app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(morgan('dev'))
app.use("/users", auth)
app.use("/exam", video);


server.listen(3000)

videoproc.js videoproc.js

const express = require('express')
const video = express.Router()
const ss = require('socket.io-stream')
const fs  = require('fs')
const path = require('path')

module.exports = (io) => {
    video.post('/video', (req, res) => {
        io.on('connection', (socket) => {
            ss(socket).on('video', (stream, data) => {
                const fileName = path.basename('test')
                stream.pipe(fs.createWriteStream(fileName))
            })
        })
    })
    return video
}

This is the client side code这是客户端代码

var io = require('socket.io-client');
var ss = require('socket.io-stream');
var fs = require('fs')
var socket = io.connect('http://localhost:3000/exam/video/');
var stream = ss.createStream();
var filename = './test.webm';
 
ss(socket).emit('video', stream, {name: filename});
fs.createReadStream(filename).pipe(stream);

As a test I've send a video feed to the server but the file is not being stored, some help to solve this issue is appreciated.作为测试,我已将视频源发送到服务器,但未存储文件,感谢您提供解决此问题的帮助。

Server you should:服务器您应该:

const bodyParser = require('body-parser')


app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
app.use(morgan('dev'))
// app.use("/users", auth)
app.use("/exam", video);

io.on('connection', (socket) => {
    console.log('CONNECTED');
    ss(socket).on('video', (stream, data) => {
        console.log('RECEIVED');
        const fileName = path.basename('test')
        stream.pipe(fs.createWriteStream(fileName))
    })
})
server.listen(3000)

and Client: param in a connect function is just "http://localhost:3000/", exclude route和客户端:连接 function 中的参数只是“http://localhost:3000/”,排除路由

var socket = io.connect('http://localhost:3000/');
var stream = ss.createStream();
var filename = './test.webm';
 
ss(socket).emit('video', stream, {name: filename});
fs.createReadStream(filename).pipe(stream);

At client: connect to a domain, not route: ref在客户端:连接到域,而不是路由: ref

At server: you placed io.on on a route, io.on is just listen when you call the route.在服务器:您将 io.on 放在路由上, io.on 只是在您调用路由时监听。 if you want run when server starting, you should place io.on outside the route如果你想在服务器启动时运行,你应该把 io.on 放在路由外面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Stream 通过我的nodejs服务器从另一台服务器到客户端的视频 - Stream a video from another server to the client through my nodejs server 如何从Node.js服务器流到客户端 - how to stream from nodejs server to client 通过服务器将视频从一个客户端传输到另一个客户端 - Stream video from one client to another client through a server 来自Blob NodeJS的视频流 - Video stream from Blob NodeJS 有没有办法在没有 WebRTC 的情况下从客户端 A -> 服务器 -> 客户端 B 的 stream 视频进行一对多广播? - Is there a way to stream video from client A -> server -> client B for one-to-many broadcast without WebRTC? nodejs ffmpeg 在特定时间播放视频并将其流式传输到客户端 - nodejs ffmpeg play video at specific time and stream it to client 同时将视频文件从nodejs服务器流传输到许多HTML5客户端 - stream a video file from nodejs server to many HTML5 clients simultaneously Nodejs 从 CouchDB 流式传输视频并显示在视频标签中 - Nodejs stream video from CouchDB and display in video tag 在nodejs中流式传输视频 - Stream video in nodejs 如何使用 nodejs 将视频形式设备流式传输到摄取服务器 - How to stream video form device to ingestion server using nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM