GZIP的Javascript(浏览器)解压缩程序

GZIP的Javascript(浏览器)解压缩程序,javascript,compression,Javascript,Compression,这个问题与被认为是一个很好的Stackoverflow问题有点不同,但我想这可能是很多人都需要的,所以我的情况如下: 我正在制作一个web应用程序,主要通过手机访问,在互联网上行速度非常慢的地方(农场),但它需要与服务器保持非常动态的信息交换 到目前为止,我正在使用websocket来减少通信开销,只交换JSON数组上的原始数据,甚至通过这个套接字接收css和脚本,所有这些都被缩小了 为了减少更多的数据交换,我考虑在服务器端使用GZIP压缩,在客户端解压,反之亦然 作为压缩领域的一个完完全全的n

这个问题与被认为是一个很好的Stackoverflow问题有点不同,但我想这可能是很多人都需要的,所以我的情况如下:

我正在制作一个web应用程序,主要通过手机访问,在互联网上行速度非常慢的地方(农场),但它需要与服务器保持非常动态的信息交换

到目前为止,我正在使用websocket来减少通信开销,只交换JSON数组上的原始数据,甚至通过这个套接字接收css和脚本,所有这些都被缩小了

为了减少更多的数据交换,我考虑在服务器端使用GZIP压缩,在客户端解压,反之亦然

作为压缩领域的一个完完全全的noob,我想问你们,亲爱的朋友们,是否有用于压缩/解压缩文本数据的javascript插件

在第一次访问时不需要花费生命就可以加载的东西(具体来说,不到1米)。下面是我未整理的初始脚本,用于处理所有通信:

let ws = new WebSocket("wss://localhost/Socket");

ws.onopen = function () {
    let
            functions = new Map(),
            loadedScripts = new Set();

    //receives a JSON array with two elements
    //the first tells what to do with the data,
    //which is an integer that is key for a mapped function
    ws.onmessage = function (event) {
        let data = JSON.parse(event.data);

        //finds the determined function on the map and passes the second
        //item of the array as argument to it
        functions.get(data[0])(data[1]);
    };

    // the function #1 is the one who discovers if the reqested script
    // and version are already loaded on are stored on the local storage
    // from a previous access, if not sends a request for the given script
    // it receives an array of "requirements"
    functions.set(1, function (array) { //require [name, token, version]
        for (let require in array) {
            loadedScripts.add(require[0]);
            if (typeof window[require[0]] !== "function" || eval(require[0]).version !== require[2]) {
                if (eval(localStorage[require[0]]) !== require[2]) {
                    ws.send(JSON.stringify([2, require[1]]));
                }
            }
       }
    });

    // the #2 function is the one who evaluates the string, which is a 
    // script, received on the websocket
    // HERE COMPRESSION WOULD BE VERY HELPFULL
    functions.set(2, function (script) { //[scriptName, script]
        eval(script[1]);
        try {
            localStorage[script[0]] = script[1];
        } catch (e) {
            if (e instanceof DOMException) {
                for (let k in localStorage) {
                    if (!loadedScripts.has(k)) {
                        localStorage.removeItem(k);
                    }
                }
                functions.get(2)(script);
            } else {
                throw e;
            }
        }
    });
};

//...
我为任何拼写错误或错误的文字提前道歉,而不是我的语言

编辑


事实上,我已经在谷歌上搜索了一些插件,但我对这个主题的了解有限,这让我无法理解其中的大部分内容。所以我的问题更像是一个“noobs压缩”问题。

对于你的离题问题有很多可能的解决方案:pYep,replicate。我甚至不知道去那里的术语。谢谢