简体   繁体   English

套接字流可以在服务器中的ss.event.on上起作用

[英]Can socketstream act on ss.event.on in server

I am publishing messages to a Redis server with the Pub/Sub system. 我正在使用发布/订阅系统将消息发布到Redis服务器。 I am using Socketstream 0.3 with node.js to listen to and process these messages. 我将Socketstream 0.3与node.js一起使用以侦听和处理这些消息。 In client code (app.js) I can act on these messages just fine using ss.event.on: 在客户端代码(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

However, on some of these messages I also want the system to act even if no browser is accessing the website. 但是,在其中一些消息上,即使没有浏览器访问该网站,我也希望系统运行。 In other words, I'd like it to be in either app.js (or app.coffee) in root, or in server/rpc, so that it gets executed whenever the message arrives, regardless of a browser having opened the website. 换句话说,我希望它位于根目录下的app.js(或app.coffee)中,或位于服务器/ rpc中,以便无论何时有浏览器打开网站,它都会在消息到达时执行。

When I try to do that, I get an error message on ss.event.on (no object defined). 当我尝试这样做时,我在ss.event.on(未定义对象)上收到一条错误消息。 I've also tried using ss.api.event.on but it just looks like messages can only be received in the client code. 我也尝试过使用ss.api.event.on,但看起来只能在客户端代码中接收消息。 Is this true? 这是真的? Is there another way I can achieve what I want? 有没有其他方法可以实现我想要的?

Peter 彼得

If you're on the server you can just call the function directly from the same place the event is triggered. 如果您在服务器上,则可以直接在触发事件的同一位置直接调用该函数。

If you're committed to the semantic paradigm of events you can theme your naming (of functions etc) in an event style. 如果您致力于事件的语义范式,则可以使用事件样式将(函数等的)命名命名为主题。
You could also dig into Node's event-emmiter library and customise something that way. 您还可以深入研究Node的事件发射器库,并以这种方式进行自定义。

This is all just if you want to waste your time in this way adhering to a strictly event-oriented paradigm for program organisation. 这就是您要浪费时间来遵循程序组织的严格面向事件的范例的全部方法。

The simplest thing is as commenter Deif mentioned, just call the function directly. 最简单的事情就是评论者Deif提到的,只需直接调用该函数即可。 From the same place you (would) trigger the event. 您(将)从同一位置触发事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM