我一直在使用Node.js中的二进制流进行实验,但令我惊讶的是,确实有一个工作演示,它使用节点无线电流获取Shoutcast流,并使用分块编码将其推入HTML5元素。但这仅适用于Safari!
这是我的服务器代码:
var radio = require("radio-stream");
var http = require('http');
var url = "http://67.205.85.183:7714";
var stream = radio.createReadStream(url);
var clients = [];
stream.on("connect", function() {
console.error("Radio Stream connected!");
console.error(stream.headers);
});
// When a chunk of data is received on the stream, push it to all connected clients
stream.on("data", function (chunk) {
if (clients.length > 0){
for (client in clients){
clients[client].write(chunk);
};
}
});
// When a 'metadata' event happens, usually a new song is starting.
stream.on("metadata", function(title) {
console.error(title);
});
// Listen on a web port and respond with a chunked response header.
var server = http.createServer(function(req, res){
res.writeHead(200,{
"Content-Type": "audio/mpeg",
'Transfer-Encoding': 'chunked'
});
// Add the response to the clients array to receive streaming
clients.push(res);
console.log('Client connected; streaming');
});
server.listen("8000", "127.0.0.1");
console.log('Server running at http://127.0.0.1:8000');
我的客户代码很简单:
这在Mac上的Safari 5中可以正常运行,但在Chrome或Firefox中似乎无法执行任何操作。有任何想法吗?
可能的候选对象包括编码问题,或者只是部分实现的HTML5功能…