图像将无法在tls服务器上正确显示

时间:2019-05-21 23:46:35

标签: javascript node.js

当我查看图像时,我得到的只是一个方形框

这是从今天开始运行的最新节点,我尝试在utf8中打开二进制文件,并尝试不指定编码

var fs = require("fs");
var net = require("net");
var tls = require("tls");
var config = require("config");
var clients = [];
mount = ";";
function get(data) {
    var r = /GET\s(.+)\sHTTP/gms;
    var rt = r.exec(data)
    if (rt != null) { return rt[1] }
}

function handle_data(data, socket) {
    if (get(data) == "/") {
        var read = fs.readFile("index.html", function(err, data) {
            if (err){
                console.log(err);
            }
        var data = data.toString();
        var content = data.replace("{port}", config.JCasterPort).replace("{mount}", mount);
        var length = content.length
        var header = config.HTTP_RESP.replace("{length}", length).replace("{cts}", content).replace("{type}", "text/html");
        socket.write(header)
        });
    }
    if (get(data) == "/img/content-bg.png") {
        var read = fs.readFile("img/content-bg.png", "utf8", function(err, data) {
                        if (err){
                                console.log(err);
                        }
        var data = data.toString();
                var content = data;
                var length = content.length
                var header = config.HTTP_RESP.replace("{length}", length).replace("{cts}", content).replace("{type}", "image/png");
                socket.write(header)
        });

    }
    if (get(data) == "/img/background.gif") {
                var read = fs.readFile("img/background.gif", "utf8", function(err, data) {
                        if (err){
                                console.log(err);
                        }
        var data = data.toString();
                var content = data;
                var length = content.length
                var header = config.HTTP_RESP.replace("{length}", length).replace("{cts}", content).replace("{type}", "image/gif");
                socket.write(header)
        });
        }
    if (get(data) == "/img/background.jpg") {
                var read = fs.readFile("img/background.jpg", function(err, data) {
                        if (err){
                                console.log(err);
                        }
        var data = data.toString()
                var content = data;
                var length = content.length
                var header = config.HTTP_RESP.replace("{length}", length).replace("{cts}", content).replace("{type}", "image/jpg");
                socket.write(header)
        });
        }


}

if (config.JCasterSSL) {
    var key = config.JCasterKey;
    var cert = config.JCasterCert;
    var options = {
            key: fs.readFileSync(key),
            cert: fs.readFileSync(cert)
    };
    var server = tls.createServer(options, function(socket) {
        socket.on("data", function(data) {
            var data = data.toString();
            handle_data(data, socket)
        });
});
server.listen(config.JCasterPort, function(){
    console.log("Listening on port: " + config.JCasterPort)
});
} else {

}
//HTTP_RESP = HTTP/1.1 200 OK\r\nContent-Type: {type}\r\nContent-Length: {length}\r\n\r\n{cts}

我的目标是显示我知道可以使用内置http / https库的图像,但我需要它能够广播音频数据 我唯一没有尝试过的就是编码为ASCII

0 个答案:

没有答案