This repository was archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathapp.js
More file actions
111 lines (94 loc) · 3.11 KB
/
Copy pathapp.js
File metadata and controls
111 lines (94 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var getport = require('getport');
var spawn = require('child_process').spawn;
var parseTorrent = require('parse-torrent');
var processes = {}; // Peerflix processes
var users = 0;
server.listen(3000, function () {
console.log('app listening on port 3000!');
});
app.use(express.static('public'));
function printProcesses() {
console.log("------------------RUNNING PROCESSES------------------");
for(var p in processes) {
console.log(processes[p].name + " | Port:" + processes[p].port + " | Spectators: " + processes[p].spectators);
}
console.log("-----------------------------------------------------");
}
io.on('connection', function (socket) {
users++;
console.log("Connected users : " + users);
socket.on('disconnect', function() {
users--;
console.log("Connected users : " + users);
if(socket.playing != undefined) {
processes[socket.playing].spectators--;
if(processes[socket.playing].spectators === 0) {
processes[socket.playing].child.kill();
delete processes[socket.playing];
}
}
printProcesses();
});
socket.on('cancelTorrent', function () {
if(!socket.playing) return;
processes[socket.playing].spectators--;
if(processes[socket.playing].spectators === 0) {
processes[socket.playing].child.kill();
delete processes[socket.playing];
}
socket.playing = null;
printProcesses();
});
socket.on('getTorrent', function (data) {
var torrent = data.torrent
// There is already a stream running, kill it
if(socket.playing && process[socket.playing] != torrent) {
processes[socket.playing].spectators--;
if(processes[socket.playing].spectators === 0) {
processes[socket.playing].child.kill();
delete processes[socket.playing];
}
socket.playing = null;
}
// A process already exists for this torrent
if(processes[torrent]) {
port = processes[torrent].port;
processes[torrent].spectators++;
socket.playing = torrent;
socket.emit('port', port);
printProcesses();
return;
}
// Create a new process
getport(function (err, port) {
if (err) console.log(err);
var process = {};
var child = spawn('peerflix', [torrent, '--port='+ port, '--tmp=./tmp', '--remove'], {});
process.child = child;
process.port = port;
process.spectators = 0;
processes[torrent] = process;
processes[torrent].spectators++;
parseTorrent.remote(torrent, function (err, parsedTorrent) {
if (err) throw err
processes[torrent].name = parsedTorrent.name;
printProcesses();
})
socket.playing = torrent;
socket.emit('port', port);
child.stdout.on('data', function(data) {
//console.log('stdout: ' + data);
});
child.stderr.on('data', function(data) {
console.log('stderr: ' + data);
});
child.on('close', function (code, signal) {
console.log('child closed');
});
});
});
});