Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 通过TCP服务器提供mp3服务_Node.js_Sockets_Http_Tcp_Mp3 - Fatal编程技术网

Node.js 通过TCP服务器提供mp3服务

Node.js 通过TCP服务器提供mp3服务,node.js,sockets,http,tcp,mp3,Node.js,Sockets,Http,Tcp,Mp3,我试图通过TCP服务器提供mp3文件,只是添加一些头来模拟HTTP服务器。我当前使用的标题是: 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()}\

我试图通过TCP服务器提供mp3文件,只是添加一些头来模拟HTTP服务器。我当前使用的标题是:

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`
创建服务器的方法:

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()))
})
如果我不发送任何头文件(我删除了
socket.write(Buffer(header,'ascii'))
),我将下载该文件,其中包含如下示例请求:

http://192.168.0.12:65000/song.mp3
问题是我想将歌曲作为流媒体服务,但我不知道如何处理请求以及如何设置正确的头

这是示例请求的快照:

正如您在服务器代码中所看到的,响应是用socket.write写入的,这在
数据
事件处理程序中触发,我可以在该事件处理程序中访问有关请求的数据信息。这是第一个(有两个)请求数据的快照: