node.js - Serve mp3 through TCP server -
i'm trying serve mp3 file through tcp server adding headers emulate http server. headers i'm using are:
var header = `http/1.1 206 partial content\r\ncontent-type: audio/mpeg\r\ncontent-length: ${filestat.size}\r\n'cache-control: no-cache'\r\nconnection: keep-alive\r\nlast-modified: ${date.togmtstring()}\r\n\r\n`
an way create server:
let server = net.createserver((socket) => { socket.on('data', (data) => { socket.write(buffer(header, 'ascii')) rnfetchblob.fs.readstream('/storage/sdcard1/music/elpisito.mp3', 'ascii') .then((stream) => { stream.open() stream.ondata((chunk) => { socket.write(buffer(chunk, 'ascii')) }) stream.onend(() => { socket.end() }) }) } }) socket.on('error', (error) => { console.log('error ' + error) }) }).listen(serverport, '0.0.0.0', () => { console.log('opened server on ' + json.stringify(server.address())) })
if don't send header (i delete socket.write(buffer(header, 'ascii'))
) download file sample request looks this:
http://192.168.0.12:65000/song.mp3
the problem want serve song streaming don't know how handle requests , how set proper header.
this snapshot of sample request:
as can see in server code, response written socket.write
, triggered in data
event handler have access data info requests. snapshot of first (there two) request data:
Comments
Post a Comment