简体   繁体   English

如何在WebSocket中使用RSVP延迟?

[英]How do I use RSVP deferreds with WebSocket?

I need to open multiple WebSocket connections concurrently and then perform actions once all the connections are opened. 我需要同时打开多个WebSocket连接,然后在打开所有连接后执行操作。

I am using the defer() , all() , and then() methods of RSVP.js. 我正在使用RSVP.js的defer()all()then()方法。 However, neither the then() nor the catch() is being triggered, and I cannot figure out where I am going wrong. 但是, then()catch()都没有被触发,而且我无法弄清楚哪里出了问题。

Here is my code: 这是我的代码:

var numberOfThreads = 6,
    openSockets = function () {
        var // the deferred object
            d,
            // array of promises
            socketsOpened = [],
            // websocket object and index
            ws, i;

        // open a new WebSocket connection for each iteration
        for (i = 0; i < numberOfThreads; i += 1) {
            // create deferred object
            d = RSVP.defer();

            // push promise into array
            socketsOpened.push(d.promise);

            // create websocket connection to valid, working socket server
            ws = new WebSocket(url);

            // websocket events
            ws.onopen = function () {
                // when socket is open, resolve deferred
                d.resolve();
            };
        }

        // when all connections are open, do stuff
        RSVP
        .all(socketsOpened)
        .then(function () {
            console.log('All sockets are opened!');
        }).catch(function () {
            console.log('Oops! Something has gone wrong!');
        });
    };

openSockets();

What am I doing wrong? 我究竟做错了什么?

Instead of assigning the deferred to d , I should create an array ( d = [] ), assign each deferred to the i position of the array ( d[i] = RSVP.defer() ), and push d[i] into the socketsOpened array. 而不是将deferred分配给d ,我应该创建一个数组( d = [] ),将每个deferred分配给该数组的i位置( d[i] = RSVP.defer() ),然后将d[i]推入socketsOpened数组。

Here's the new code: 这是新的代码:

var numberOfThreads = 6,
    openSockets = function () {
        var // an array for deferred objects
            d = [],
            // array of promises
            socketsOpened = [],
            // websocket object and index
            ws, i;

        // open a new WebSocket connection for each iteration
        for (i = 0; i < numberOfThreads; i += 1) {
            // protect i with closure
            (function (i) {
                // create deferred object
                d[i] = RSVP.defer();

                // push promise into array
                socketsOpened.push(d[i].promise);

                // create websocket connection to valid, working socket server
                ws = new WebSocket(url);

                // websocket events
                ws.onopen = function () {
                    // when socket is open, resolve deferred
                    d[i].resolve();
                };
            }(i));
        }

        // when all connections are open, do stuff
        RSVP
        .all(socketsOpened)
        .then(function () {
            console.log('All sockets are opened!');
        }).catch(function () {
            console.log('Oops! Something has gone wrong!');
        });
    };

openSockets();

Apologies for wasting everyone's time! 很抱歉浪费大家的时间!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM