2013-05-13 83 views
0

我試圖壓縮來自節點js應用程序的輸出。我能夠在服務器端成功壓縮文本字符串。但是與它發送的頭說,HTML /純文本節點JS發射方法 - 將輸出標頭設置爲Zlib

所以瀏覽器無法

我使用節點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); 

任何想法或幫助將不勝感激

回答

0

Socket.io將採取您的壓縮的字符串,並將其傳回客戶端使用的傳輸,socket.io客戶端已經與你的socket.io服務器建立了聯繫。

這可能是長時間輪詢或websockets例如。隨着websocket傳輸,沒有內容類型的頭文件,因爲所有內容都是二進制的。

我能想到的2種選擇:

  • 使用ZIP庫在瀏覽器中虛增您的base64編碼字符串。

  • 發出鏈接到其客戶端執行GET上的客戶端。然後,您可以將該內容作爲常規http請求提供,您可以使用連接模塊進行壓縮,如上所述here