0

我正在尝试压缩节点 js 应用程序的输出。我能够在服务器端成功压缩文本字符串。但随它发送的标头显示 HTML/纯文本

所以浏览器无法解压

我正在使用节点 v0.6.18

我的代码如下:

    var http  = require('http'),
url   = require('url'),
fs    = require('fs'),
amqp  = require('amqp'),
redis = require('redis'),
zlib  = require('zlib'),    
sys   = require(process.binding('natives').util ? 'util' : 'sys');

var exchangeName= 'conferenceTest';

send404 = function(res) {
    res.writeHead(404);
    res.write('404');
    res.end();
};

server = http.createServer(function(req, res) {
    var path = url.parse(req.url).pathname;
    switch (path) {

        case '/':

            fs.readFile(__dirname + "/index.html", function(err, data) {

                if (err) {
                    return send404(res);
                } else {
                    res.writeHead(200, {'Content-Type': 'application/zip','Connection':'close', 'content-encoding': 'gzip'});
                    res.write(data, 'utf8');
                    res.end();
                }

            });
        break;
    }

});

// listen to the http server for socket connections
var io = require('socket.io').listen(server); 

var connection = amqp.createConnection({host: 'localhost'});

connection.on('ready', function() {

    var exchange = connection.exchange(exchangeName, { // create exchange
        type: 'direct',
        durable: true
    });

    io.set('close timeout',500);
    io.set('browser client gzip',true);

    io.sockets.on('connection', function(client) {
        console.log("client connected");

        client.on('setQueue', function(data) { 
            var queue = connection.queue(data.queueName, { 
                durable: true,
                autoDelete: false
            });
        });

            /************************** CHANGE VIEW EVENT HANDLER STARTS **************************/
                    client.on('changeview', function(data) {
                            var queue = connection.queue(data.queueName, { //create queue
                                    durable: true,
                                    autoDelete: false
                            });

                            var plaintext = "Put any kind of meat on a stick and roast it over a flame and it immediately becomes food fit for gods. No country understands this sacred rule of seared meat like Turkey.Turkish kebabs are the incarnation of the meat lovers most exotic fantasies, with grilled lamb, beef and chicken as skewer MVPs.Most kebab restaurants also have a long list of Turkish starters called meze that are as delicious as the main dishes.Turkeys best alcoholic complement for all that meat is raki -- an aniseed-flavored drink that s often diluted with water and chilled with ice. Frothy, yogurt-based ayran is a great non-alcoholic complement to heavy dishes. But who are we kidding -- you just want the meat. Heres where to get it in Turkey.";

                            zlib.deflate(plaintext, function(err, buffer) {
                             if (!err) {
                                console.log("Original   Length: " + plaintext.length);
                                console.log("Compressed Length: " + buffer.toString('base64').length);
                                io.sockets.emit('changeview', buffer.toString('base64'));
                              }
                            });
                    });



    });

});

    process.on( 'uncaughtException', function ( err ) {
   console.log( 'Uncaught Exception: ' + err.message );
   });

    server.listen(18080);

任何想法或帮助将不胜感激

4

1 回答 1

0

Socket.io will take your compressed string and pass it back to the client using the transport that the socket.io client has established with your socket.io server.

This could be long polling or websockets for example. With the websocket transport there is no content-type header as everything is binary.

I can think of 2 options:

  • Use a zip library in the browser to inflate your base64 encoded string.

or

  • Emit a link to the client which the client does a GET on. You can then serve up that content as a regular http request which you can compress using a connect module as discussed here
于 2013-05-13T13:06:04.360 回答