1

我正在尝试 Node 和 Socket IO,我正在尝试以类似的方式匹配路由节点,例如

app.get("controller/action/somevar", callback);

但是在套接字事件匹配中使用正则表达式似乎不起作用。有没有办法匹配正则表达式事件,或者至少匹配通配符“*”

我试过这个。

socket.on(/.+/gi, function (data) {
socket.emit('set_time', { time: '13:32'});
});

我想拦截来自某些控制器或包含某些变量的所有事件。

ps:我发现一个博客提到没有办法匹配正则表达式事件,但那是2年前,也许事情已经改变了。

4

1 回答 1

2

Sockets inherit from EventEmitter so only support regularly named events (no fancy stuff like wildcards or regexes).

A somewhat related question discusses some options to implement an Express-like routing layer on top of socket.io, but the solution isn't a drop in replacement and requires both client and server code rewrites.

A rather different approach, but perhaps applicable in your situation, is taken by the sockatron module.

于 2013-11-07T18:35:31.717 回答