我正在使用 Pub/Sub 系统将消息发布到 Redis 服务器。我正在使用带有 node.js 的 Socketstream 0.3 来收听和处理这些消息。在客户端代码(app.js)中,我可以使用 ss.event.on 很好地处理这些消息:
ss.event.on "portux", (object) ->
# Handle messages of the type Switch 3 true (to switch 3 on) or Switch 2 false
if object.type is "Switch"
sw = object.location
# the cmd is now in the quantity field
cmd = object.quantity
if object.value then cmd += 'On;' else cmd += 'Off;'
send cmd
但是,对于其中一些消息,即使没有浏览器访问该网站,我也希望系统能够采取行动。换句话说,我希望它位于根目录下的 app.js(或 app.coffee)中,或者位于 server/rpc 中,以便在消息到达时执行它,而不管浏览器是否打开了网站。
当我尝试这样做时,我在 ss.event.on 上收到一条错误消息(未定义对象)。我也尝试过使用 ss.api.event.on 但看起来只能在客户端代码中接收消息。这是真的?还有另一种方法可以实现我想要的吗?
彼得