是否有一个javascript客户端库,用于排队具有存储支持的Ajax请求

时间:2021-12-28 15:46:49

I'm looking for a javascript client side lib to handle an ajax or websocket requests queue like this :

我正在寻找一个javascript客户端lib来处理像这样的ajax或websocket请求队列:

  • directly transmit request when user is online and the server is up
  • 当用户在线且服务器启动时直接传输请求
  • store requests when user is offline or server is down using local storage
  • 当用户离线或服务器因使用本地存储而关闭时存储请求
  • send stored requests when user is online and server is back online
  • 当用户在线且服务器重新联机时发送存储的请求

Ideally :

理想情况下:

  • without jquery
  • 没有jquery
  • I'm also looking for a serverside component to go along
  • 我也在寻找一个服务器端组件

I can't find anything like this. Should I build one myself using localstorage, window.navigator.onLine and socket.io for instance or is there already some lib for that purpose ?

我找不到这样的东西。我应该自己使用localstorage构建一个,例如window.navigator.onLine和socket.io还是已经有一些用于此目的的lib?

Thanks.

谢谢。

1 个解决方案

#1


4  

The question is probably going to be closed, since asking for a library is usually not welcomed on *. However, this is a fun little challenge. (Took me ~15 minutes to complete.)

问题可能会被关闭,因为在*上通常不欢迎要求库。然而,这是一个有趣的小挑战。 (花了我大约15分钟才完成。)

This is actually not a very big task to handle, barely 50 lines or so. That's probably why you can't find a small library doing this.

这实际上不是一个很难处理的任务,只有50行左右。这可能就是为什么你找不到这样做的小型图书馆的原因。

function Sender() {
    var requests = [],
        state = 0; // 0 = closed, 1 = open
        ws = new WebSocket();

    ws.onopen = function() {
        state = 1;
        if (requests.length) {
            send();
        }
    };

    ws.onerror = function() {
        if (ws.readyState !== 1) { // 1 is "OPEN"
            close();
        }
    };

    // Let's fill in the requests array if necessary
    // IIFE because I don't want to pollute the scope with `req`
    (function() {
        var req = JSON.parse(localStorage.get('queue'));
        if (req.length) {
            requests = requests.concat(req);
        }
    }());

    return {
        send: function(msg) {
            if (state === 0) {
                requests.push(msg);
            }
            else {
                send();
            }
        },
        close: function() {
            state = 0;
            localStorage.set('queue', JSON.stringify(requests));
        }
    };

    function send() {
        var done = false;
        while (!done) {
            if (state === 1) {
                ws.send(requests.pop());
                if (!requests.length) {
                    done = true;
                }
            }
            else {
                done = true;
            }
        }
    }
}

// Usage example
var sender = Sender();
sender.send('message');

// let's say your "disconnect" button is in a variable
disconnect.addEventListener('click', function() {
    sender.close();
}, false);

The server doesn't have to handle anything except normal requests. If you want, you can add a timestamp to the message you send, this way it's quite easy (2 lines or so) to keep track of the time.

除正常请求外,服务器不必处理任何事情。如果需要,可以为发送的消息添加时间戳,这样可以很容易地(2行左右)跟踪时间。

#1


4  

The question is probably going to be closed, since asking for a library is usually not welcomed on *. However, this is a fun little challenge. (Took me ~15 minutes to complete.)

问题可能会被关闭,因为在*上通常不欢迎要求库。然而,这是一个有趣的小挑战。 (花了我大约15分钟才完成。)

This is actually not a very big task to handle, barely 50 lines or so. That's probably why you can't find a small library doing this.

这实际上不是一个很难处理的任务,只有50行左右。这可能就是为什么你找不到这样做的小型图书馆的原因。

function Sender() {
    var requests = [],
        state = 0; // 0 = closed, 1 = open
        ws = new WebSocket();

    ws.onopen = function() {
        state = 1;
        if (requests.length) {
            send();
        }
    };

    ws.onerror = function() {
        if (ws.readyState !== 1) { // 1 is "OPEN"
            close();
        }
    };

    // Let's fill in the requests array if necessary
    // IIFE because I don't want to pollute the scope with `req`
    (function() {
        var req = JSON.parse(localStorage.get('queue'));
        if (req.length) {
            requests = requests.concat(req);
        }
    }());

    return {
        send: function(msg) {
            if (state === 0) {
                requests.push(msg);
            }
            else {
                send();
            }
        },
        close: function() {
            state = 0;
            localStorage.set('queue', JSON.stringify(requests));
        }
    };

    function send() {
        var done = false;
        while (!done) {
            if (state === 1) {
                ws.send(requests.pop());
                if (!requests.length) {
                    done = true;
                }
            }
            else {
                done = true;
            }
        }
    }
}

// Usage example
var sender = Sender();
sender.send('message');

// let's say your "disconnect" button is in a variable
disconnect.addEventListener('click', function() {
    sender.close();
}, false);

The server doesn't have to handle anything except normal requests. If you want, you can add a timestamp to the message you send, this way it's quite easy (2 lines or so) to keep track of the time.

除正常请求外,服务器不必处理任何事情。如果需要,可以为发送的消息添加时间戳,这样可以很容易地(2行左右)跟踪时间。