Added bigbuck.mp4, the sample file Install nodemon module for streaming Modified index.html to contain the video, and changed the title Modified index.js to add /video dir for the video streaming
55 lines
1.6 KiB
JavaScript
55 lines
1.6 KiB
JavaScript
const fs = require("fs");
|
|
const express = require('express')
|
|
const app = express()
|
|
const server = require('http').createServer(app);
|
|
const WebSocket = require('ws')
|
|
|
|
const wss = new WebSocket.Server({ server:server });
|
|
|
|
wss.on('connection', function connection(ws) {
|
|
console.log('A new client Connected!');
|
|
ws.send('Welcome New Client!');
|
|
});
|
|
|
|
app.get("/", function (req, res) {
|
|
res.sendFile(__dirname + "/index.html");
|
|
});
|
|
|
|
|
|
app.get("/video", function (req, res) {
|
|
// Ensure there is a range given for the video
|
|
const range = req.headers.range;
|
|
if (!range) {
|
|
res.status(400).send("Requires Range header");
|
|
}
|
|
|
|
// get video stats (about 61MB)
|
|
const videoPath = "bigbuck.mp4";
|
|
const videoSize = fs.statSync("bigbuck.mp4").size;
|
|
|
|
// Parse Range
|
|
// Example: "bytes=32324-"
|
|
const CHUNK_SIZE = 10 ** 5; // 1MB
|
|
const start = Number(range.replace(/\D/g, ""));
|
|
const end = Math.min(start + CHUNK_SIZE, videoSize - 1);
|
|
|
|
// Create headers
|
|
const contentLength = end - start + 1;
|
|
const headers = {
|
|
"Content-Range": `bytes ${start}-${end}/${videoSize}`,
|
|
"Accept-Ranges": "bytes",
|
|
"Content-Length": contentLength,
|
|
"Content-Type": "video/mp4",
|
|
};
|
|
|
|
// HTTP Status 206 for Partial Content
|
|
res.writeHead(206, headers);
|
|
|
|
// create video read stream for this particular chunk
|
|
const videoStream = fs.createReadStream(videoPath, { start, end });
|
|
|
|
// Stream the video chunk to the client
|
|
videoStream.pipe(res);
|
|
});
|
|
|
|
server.listen(3000, () => console.log(`Lisening on port :3000`)) |