webRTC-to-stream/server-test.js
2018-05-29 20:51:31 +05:00

160 lines
3.5 KiB
JavaScript
Executable File

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', async (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);
});
const onNewChunk = () => {
new Promise((res, rej) => {
socket.on('blob', (data) => {
if(data){
res(data)
}
else {
rej(data);
}
})
})
};
});
const initConnection = async () => {
const options = {
method: 'PUT',
forever: true,
uri: 'http://ws.kurento.fishrungames.com:8000/'+ 'qq' + '.ogv',
headers: {
'Connection': 'keep-alive',
'content-type': 'video/ogv',
'ice-Public': '1',
'ice-Name': 'Stream' + 'qq',
'ice-Description': 'Steam' + 'qq',
'Expect': '100-continue',
'Accept': '*/*'
},
auth: {
user: 'source',
password: 'frg$$frg'
}
};
fs.createReadStream('new.ogv').pipe(request.put(options, (error, response, body) =>
{
if(error){
console.log(error);
}
console.log('end');
}));
console.log('eng');
/*req = request(options, (err, message, data) => {
if(err)
console.log(err)
//console.log(message);
//console.log(data);
});*/
};
const job = async()=> {
console.log('Init');
let end = await initConnection();
//req.write(await readFile('new.webm'));
};
job();
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)
}
});