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?
});
我希望在客户端 socket.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屋!
查看全文