147 lines
3.2 KiB
JavaScript
147 lines
3.2 KiB
JavaScript
const path = require('path');
|
|
const url = require('url');
|
|
const express = require('express');
|
|
const fs = require('fs');
|
|
const http = require('http');
|
|
const io = require('socket.io');
|
|
const app = express();
|
|
const agent = require('agentkeepalive');
|
|
const request = require('request');
|
|
|
|
const port = 8443;
|
|
const server = http.createServer(app).listen(port, function() {
|
|
console.log('Open http://localhost:' + port + '/ with a browser');
|
|
});
|
|
|
|
|
|
// TEST PROMISE
|
|
const readFile = (path, opts = 'utf8') =>
|
|
new Promise((res, rej) => {
|
|
fs.readFile(path, opts, (err, data) => {
|
|
if (err) rej(err)
|
|
else res(data)
|
|
})
|
|
})
|
|
|
|
|
|
|
|
// CORS
|
|
app.use((req, res, next) => {
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
next();
|
|
});
|
|
|
|
app.use(express.static(path.join(__dirname, 'static')));
|
|
|
|
const socket = io(server);
|
|
|
|
let streamerId = '';
|
|
let streamOnline = false;
|
|
let req;
|
|
|
|
socket.on('connection', (socket) => {
|
|
console.log('Connected ' + socket.id);
|
|
|
|
socket.on('error', (error) => {
|
|
console.error('Connection ' + socket.id + ' error', error);
|
|
});
|
|
|
|
socket.on('disconnect',(data) => {
|
|
if(streamerId == socket.id){
|
|
console.log('Stream offline! ' + streamerId+ ' closed');
|
|
} else {
|
|
console.log('Connection ' + socket.id + ' closed');
|
|
}
|
|
});
|
|
|
|
|
|
socket.on('stream', (data) => {
|
|
streamerId = socket.id;
|
|
streamOnline = true;
|
|
console.log('Stream active! Session ' + streamerId);
|
|
initConnection();
|
|
});
|
|
|
|
const onNewChunk = () => {
|
|
new Promise((res, rej) => {
|
|
socket.on('blob', (data) => {
|
|
if(data){
|
|
res(data)
|
|
}
|
|
else {
|
|
rej(data);
|
|
}
|
|
});
|
|
)}
|
|
}
|
|
|
|
try {
|
|
while(true){
|
|
req.write(await readFile('new.webm'));
|
|
}
|
|
} catch(ex){
|
|
req.end();
|
|
}
|
|
|
|
});
|
|
|
|
const initConnection = async () => {
|
|
const options = {
|
|
method: 'PUT',
|
|
forever: true,
|
|
uri: 'http://ws.kurento.fishrungames.com:8000/'+ streamerId + '.webm',
|
|
headers: {
|
|
'Connection': 'keep-alive',
|
|
'content-type': 'video/webm',
|
|
'ice-Public': '1',
|
|
'ice-Name': 'Stream' + streamerId,
|
|
'ice-Description': 'Steam' + streamerId
|
|
},
|
|
auth: {
|
|
user: 'source',
|
|
password: 'frg$$frg'
|
|
},
|
|
};
|
|
|
|
req = request(options, (err, message, data) => {
|
|
if(err)
|
|
console.log(err)
|
|
|
|
//console.log(message);
|
|
//console.log(data);
|
|
});
|
|
}
|
|
|
|
|
|
app.get('/video', function(req, res) {
|
|
const path = 'new.webm'
|
|
const stat = fs.statSync(path)
|
|
const fileSize = stat.size;
|
|
const range = req.headers.range;
|
|
if (range) {
|
|
const parts = range.replace(/bytes=/, "").split("-");
|
|
const start = parseInt(parts[0], 10);
|
|
const end = parts[1]
|
|
? parseInt(parts[1], 10)
|
|
: fileSize-1
|
|
const chunksize = (end-start)+1;
|
|
const file = fs.createReadStream(path, {start, end});
|
|
const head = {
|
|
'Content-Range': `bytes ${start}-${end}/${fileSize}`,
|
|
'Accept-Ranges': 'bytes',
|
|
'Content-Length': chunksize,
|
|
'Content-Type': 'video/webm',
|
|
}
|
|
res.writeHead(206, head);
|
|
file.pipe(res);
|
|
} else {
|
|
const head = {
|
|
'Accept-Ranges': 'bytes',
|
|
'Content-Length': fileSize,
|
|
'Content-Type': 'video/webm',
|
|
}
|
|
res.writeHead(200, head)
|
|
fs.createReadStream(path).pipe(res)
|
|
}
|
|
|
|
}) |