简体   繁体   English

以gzip格式从node.js服务器向客户端发送socket.io响应数据

[英]Send socket.io response data to client from node.js server in gzip format

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. 我遇到的问题是我不知道如何能够判断我发送回客户端的数据是否以gzip格式压缩。 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}
...

To me this looks like the server is writing the response in ascii form rather than compressing it first before sending. 对我来说,这似乎是服务器以ascii形式编写响应,而不是在发送之前先将其压缩。

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. 根据我的阅读,只要我设置'浏览器客户端gzip',我的响应应该被发送gzip。 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. 如果他们不是我怎么做的话,如果我是如何从服务器的调试信息中知道它们实际上是压缩响应。

When I launch the server I use the following command in BASH: 当我启动服务器时,我在BASH中使用以下命令:

$ NODE_ENV=production node app.js $ NODE_ENV =生产节点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();
});

The browser client gzip option enables gzip compression for the socket.io script that's served from /socket.io/socket.io.js . browser client gzip选项为从/socket.io/socket.io.js提供的socket.io 脚本启用gzip压缩。 It does not affect the actual WebSocket connection. 它不会影响实际的WebSocket连接。

The WebSocket protocol itself only recently added support for compression of data sent over the socket. WebSocket协议本身最近才增加了对通过套接字发送的数据压缩的支持。 Soket.io does not yet support compression , nor do other node WebSocket servers . Soket.io 尚不支持压缩其他节点WebSocket服务器也不支持

To be honest, with the small amount of data you're sending in your example, compression will actually be counter-productive as it's likely to increase the amount of data sent over the wire. 老实说,对于您在示例中发送的少量数据,压缩实际上会适得其反,因为它可能会增加通过线路发送的数据量。

socket.io 1.4支持默认情况下启用压缩。

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. 在阅读了一些评论后,我决定查看第三方库来处理客户端的解压缩,这导致我转向JSXCompressor。

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

JSXCompressor will take the base64 encoded gzipped data from the server and handle the decompressing and decoding. JSXCompressor将从服务器获取base64编码的gzip压缩数据并处理解压缩和解码。 Simply download the library and put it in the appropriate folder. 只需下载库并将其放在适当的文件夹中。

On the server side I'm using zlib to handle the gzipping. 在服务器端,我使用zlib来处理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();
});

现在默认启用压缩(> 1.4),另请参阅此帖子以供参考: http//socket.io/blog/socket-io-1-4-0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Node.js socket.io服务器向浏览器客户端发送二进制数据? - How to send binary data from a Node.js socket.io server to a browser client? Send Message to Socket, 从C#客户端到node.js + socket.io服务器 - Send Message to Socket, from C# client to node.js + socket.io server 如何将二进制数据从Node.js socket.io(v2.0.x)服务器发送到浏览器客户端? - How to send binary data from a Node.js socket.io (v2.0.x) server to a browser client? 使用socket.io和node.js从服务器向客户端发送消息 - Send message to client from server using socket.io and node.js 无法将数据从一个客户端发送到socket.io Node.JS中的另一个客户端 - Unable to send data from one Client to another in socket.io Node.JS 如何使用 socket.io 在客户端(网络浏览器)中显示来自 node.js 服务器的实时数据? - How to use socket.io to display realtime data in the client (web browser) from the node.js server? 使用 socket.io 和 node.js 向特定客户端发送消息 - Send message to specific client with socket.io and node.js node.js socket.io无法发送到特定客户端 - node.js socket.io cannot send to specific client node.js socket.io客户端和服务器 - node.js socket.io client and server 从不带socket.io的node.js向客户端发送消息 - Send message to client from node.js without socket.io
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM