以gzip格式从node.js服务器向客户端发送socket.io响应数据 [英] Send socket.io response data to client from node.js server in gzip format

查看:93
本文介绍了以gzip格式从node.js服务器向客户端发送socket.io响应数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题是我不知道如何能够判断我发送回客户端的数据是否以gzip格式压缩。从我看到的命令行查看服务器的输出:

The issue I'm having is that I don't know how to be able to tell whether or not the data that I'm sending back to the client is compressed in gzip format. Looking at the output of my server from the command line I'm seeing:

debug - websocket writing 3:::{"result":1368673052397}
debug - websocket writing 3:::{"result":1368673053399}
...

对我来说,这似乎是服务器以ascii格式编写响应,而不是在发送之前先将其压缩。

To me this looks like the server is writing the response in ascii form rather than compressing it first before sending.

以下是我编写的例子就是为了产生这些结果。根据我的阅读,只要我设置'浏览器客户端gzip',我的响应应该被发送gzip。如果他们不是我怎么做的,如果我是如何从服务器的调试信息中知道它们实际上是压缩响应。

Below is the example I've written to produce these results. From what I've read as long as I set 'browser client gzip' my responses should be getting sent gzipped. If they're not how do I do this and if I am how can I tell from the server's debug info that they are in fact compressed responses.

当我启动时服务器我在BASH中使用以下命令:

When I launch the server I use the following command in BASH:

$ NODE_ENV =生产节点app.js

$ NODE_ENV=production node app.js

var express = require('express'),
    http    = require('http');

var app     = express(),
    server  = http.createServer(app),
    io      = require('socket.io').listen(server);

io.configure('production', function() {
    io.enable('browser client minification');
    io.enable('browser client etag');
    io.enable('browser client gzip');
    io.set('log level', 3);
});

app.use(express.logger('dev'));

app.get('/', function(req, res) {
    res.send(
    "<script src='/socket.io/socket.io.js'></script>\n"+
    "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>\n"+
    "<script>\n"+
    "var socket=io.connect('http://127.0.0.1:3000');\n"+
    "socket.on('message', function(data) {\n"+
    "   $(\"h2\").text(data);\n"+
    "});\n"+
    "</script>\n"+
    "<h1>"+process.env.NODE_ENV+"</h1>\n"+
    "<h2></h2>\n"
    );
});

server.listen('3000');

io.sockets.on('connection', function(webSocket) {
    function whileLoop() {
        setTimeout(function() {
                var epoch = (new Date).getTime();
                var jsonData = "{\"result\":"+epoch+"}";
                webSocket.send(jsonData);
            whileLoop();
        }, 1000);
    }
    whileLoop();
});


推荐答案

在阅读了一些评论后,我决定查看第三方库在客户端处理解压缩,导致我转向JSXCompressor。

After reading some of the comments I decided to look at 3rd party libraries to handle the decompression on the client side which led me to JSXCompressor.

http://jsxgraph.uni-bayreuth.de/wp/jsxcompressor/

JSXCompressor将采用base64从服务器编码gzip压缩数据并处理解压缩和解码。只需下载库并将其放在适当的文件夹中。

JSXCompressor will take the base64 encoded gzipped data from the server and handle the decompressing and decoding. Simply download the library and put it in the appropriate folder.

在服务器端我使用zlib来处理gzipping。

On the server side I'm using zlib to handle the gzipping.

var express = require('express'),
    http    = require('http')
    zlib    = require('zlib');

var app     = express(),
    server  = http.createServer(app),
    io      = require('socket.io').listen(server);

app.use(express.logger('dev'));
app.use(express.static(__dirname + '/public'));

app.get('/', function(req, res) {
    res.send(
    "<script src='/socket.io/socket.io.js'></script>\n"+
    "<script src='/java/jsxcompressor.min.js'></script>\n"+
    "<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js'></script>\n"+
    "<script>\n"+
    "var socket=io.connect('http://127.0.0.1:3000');\n"+
    "socket.on('message', function(data) {\n"+
    "   var jsonData = JXG.decompress(data);"+
    "   $(\"h1\").text(jsonData);\n"+
    "});\n"+
    "</script>\n"+
    "<h1></h1>\n"
    );
});

server.listen('3000');

io.sockets.on('connection', function(webSocket) {
    function whileLoop() {
        setTimeout(function() {
                var epoch = (new Date).getTime();
                var jsonData = "{\"result\":"+epoch+"}";
                zlib.gzip(jsonData, function(err, buffer) {
                    webSocket.send(buffer.toString('base64'));
                });
            whileLoop();
        }, 1000);
    }
    whileLoop();
});

这篇关于以gzip格式从node.js服务器向客户端发送socket.io响应数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆