本文介绍了Promisify Socket.IO / EventEmitter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Google上搜索Promisify Socket.IO时,我找不到多少这一点让我感到有些惊讶。这是不常见的吗?

I am a bit surprised by the fact that I don't find much when googling for Promisify Socket.IO. Is it that uncommon?

我也有麻烦自己宣传:

Promise.promisifyAll(io)
io.onceAsync('connect')
.then((socket) => ...)
.catch((err) => console.log(error))

这总是会触发错误情况,我假设是因为 .once 是一个只有一个参数的回调函数,其中Promises期望第一个参数是错误。知道如何处理这类事情吗?

This always triggers the error case, I assume because .once is a callback with only one argument, where Promises expect the first one to be the error. Any idea how to deal with these kind of things?

推荐答案

我能想到承诺不合适的几个原因通常使用socket.io和 EventEmitter 接口:

I can think of several reasons why promises aren't a good fit with socket.io and EventEmitter interfaces in general:


  1. 大多数情况下,socket.io是一个事件驱动的接口,并且承诺不会在架构上与可能发生多次的事件对齐(因为承诺是一次性设备)。是的,您可以使用 .connect()的承诺,但不能使用传入消息。因此,大多数人(包括我自己)可能认为使用promises半边界而另一半使用事件处理程序是不合理的。最好在整个API中使用一个模型。

  1. For the most part, socket.io is an event driven interface and promises do not align architecturally with events that can occur more than once (since a promise is a one-shot device). Yes, you can use a promise for .connect(), but not for incoming messages. So, most people (myself included) probably don't think it makes sense to have half an interface using promises and the other half using event handlers. Likely better to use one model for the whole API.

Promise.promisifyAll()需要节点。 js样式异步回调(错误值作为第一个参数,数据作为第二个参数),这不是任何socket.io事件处理程序回调使用的。为了使promises能够与 connect 事件一起工作,你必须编写自己的自定义promsification,这可能比使用它编写的事件处理程序更多的工作。

Promise.promisifyAll() requires node.js style async callbacks (with error value as the first argument and data as the second argument) and that isn't what any of the socket.io event handler callbacks use. To make promises work with something like the connect event you'd have to write your own custom promsification which is likely more work than just using the event handler it is written for.

如果您尝试使用其他异步事件协调下一次事件的发生,则上述情况可能会例外(通常不会做的事情)在这种情况下,承诺可能对协调有用。例如,假设您想知道三个单独的异步操作何时全部完成,其中一个是下一次发生的socket.io事件。然后,手动宣传该事件可能是有意义的,因此您可以使用promises来协调多个异步操作。

An exception to the above might be if you are trying to coordinate the next occurrence of an event with other async events (something that isn't usually done) in which case promises might be useful for the coordination. As an example, supposed you wanted to know when three separate asynchronous operations were all complete, one of which was the next occurrence of a socket.io event. Then, it might make sense to manually promisify that event so you could use promises to coordinate your multiple async operations.

但对于正常的socket.io使用,承诺只是'良好的建筑设计。类似地,您通常不会对网页中的点击处理程序使用承诺。

But for normal socket.io usage, promises just aren't a good architectural fit. Similarly, you wouldn't typically use promises for a click handler in a web page.

仅供参考,如果您愿意只承诺连接操作,您可以这样手动执行:

FYI, if you want to promisify just the connect operation, you could do that manually like this:

io.connectAsync = function(url, options) {
    return new Promise(function(resolve, reject) {
        io.connect(url, options);
        io.once('connect', function(socket) {
            resolve(socket);
        });
        io.once('connect_error', function() {
            reject(new Error('connect_error'));
        });
        io.once('connect_timeout', function() {
            reject(new Error('connect_timeout'));
        });
    });
}


io.connectAsync().then(function(socket) {
    // connected here
    socket.on('someMsg', function() {
       // process message here
    });
}, function(err) {
    // error connecting here
});

这篇关于Promisify Socket.IO / EventEmitter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 04:29
查看更多