-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (27 loc) · 1009 Bytes
/
index.js
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
var torrentStream = require( 'torrent-stream' ),
segmenter = require( 'stream-segmenter' ),
db = require( 'orchestrate' )( process.env.ORCHESTRATE_TOKEN ),
io = require( 'socket.io' )( process.env.PORT )
io.on( 'connection', function( socket ) {
console.log( '-> Incoming connection', socket.id )
socket.on( 'songrequest', function( params ) {
console.log( '-> Song request', params )
db.get( 'song', params.id ).then( function( result ) {
var magnet = result.body.magnet,
filename = result.body.filename,
engine = torrentStream( magnet )
engine.on( 'ready', function() {
engine.files.forEach( function( file ) {
if( file.name.indexOf( filename ) == 0 ) {
segmenter( file, { chunkSize: 256000 }, function( id, segment ) {
if( id == 1 ) {
socket.emit( 'songaudiochunk', segment )
}
}, function() {
})
}
})
})
})
})
})