Socket.io客户端:使用一个处理程序来响应所有事件? [英] Socket.io Client: respond to all events with one handler?
本文介绍了Socket.io客户端:使用一个处理程序来响应所有事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可以让socket.io客户端对所有事件进行响应,而不必分别指定每个事件。
Is it possible to have a socket.io client respond to all events without to have specify each event individually?
例如,这样的东西现在不能工作):
For example, something like this (which obviously doesn't work right now):
var socket = io.connect("http://myserver");
socket.on("*", function(){
// listen to any and all events that are emitted from the
// socket.io back-end server, and handle them here.
// is this possible? how can i do this?
});
我希望在客户端套接字收到任何/所有事件时调用此回调函数.io代码。
I want this callback function to be called when any / all events are received by the client-side socket.io code.
这可能吗?如何?
推荐答案
看起来socket.io库将这些存储在字典中。因此,如果不修改源代码,不要以为这是可能的。
It looks like the socket.io library stores these in a dictionary. As such, don't think this would be possible without modifying the source.
从源:
EventEmitter.prototype.on = function (name, fn) {
if (!this.$events) {
this.$events = {};
}
if (!this.$events[name]) {
this.$events[name] = fn;
} else if (io.util.isArray(this.$events[name])) {
this.$events[name].push(fn);
} else {
this.$events[name] = [this.$events[name], fn];
}
return this;
};
这篇关于Socket.io客户端:使用一个处理程序来响应所有事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文