-
So let's say I want to make a twitter bot. I want to send a certain message to whoever has sent it a reply, so I need to make an event for it. Obviously one way is to get all the replies (or last n replies) in a certain time interval, find out which ones are new, etc; but first of all it's not live, and it requires an extra query to find new tweets.
所以,假设我想制作一个Twitter机器人。我想向发送回复的人发送一条消息,所以我需要为它做一个事件。显然,一种方法是在某个时间间隔内获得所有回复(或最后n个回复),找出哪些是新的,等等;但首先它不是实时的,它需要额外的查询才能找到新的推文。
-
Say we want to track some changes in a website. For instance, we want to handle an event when that change happens, instantly.
假设我们想要跟踪网站中的一些变化。例如,我们希望在发生变化时立即处理事件。
I used socket.io
to handle some other kind of events, like when some changes happen in a particular port, but I couldn't figure out how I can handle these types of events.
我使用socket.io来处理其他类型的事件,比如在特定端口发生某些变化时,但我无法弄清楚如何处理这些类型的事件。
1 个解决方案
#1
The word "event" does not mean what you think it means!
“事件”这个词并不代表你认为它意味着什么!
- In a DOM environment, an Event is a very specific (and core) concept which allows you to write code based on user interactions with elements on the screen.
- In NodeJS, an Event is something that can be generated and announced by an instance of
events.EventEmitter
- In your question, an Event seems to refer to anything that happens on the internet, potentially anywhere.
在DOM环境中,Event是一个非常具体(和核心)的概念,它允许您根据用户与屏幕上元素的交互来编写代码。
在NodeJS中,Event可以由events.EventEmitter实例生成并公布
在你的问题中,一个事件似乎指的是互联网上发生的任何事情,可能在任何地方。
Under that last definition, there is simply no single answer for how to "track an event."
根据最后的定义,对于如何“跟踪事件”,根本没有单一的答案。
If you want to write code that can respond to change (which is just a more specific version of "react to an input") you need to create a mechanism to identify that a change has occurred, followed by a mechanism to trigger whatever code you want to be run in response (this last part is you would normally call "emitting" an "event").
如果你想编写可以响应变化的代码(这只是“对输入作出反应”的更具体版本),你需要创建一种机制来识别发生了变化,然后是一种机制来触发任何代码你想要在响应中运行(最后一部分是你通常称之为“发出”一个“事件”)。
SocketIO accomplishes both of these things for certain situations, using a graceful degredataion of protocols in order to explicitly emit local events that you can listen for and handle. It starts trying to use WebSockets, and eventually falls back to more expensive techniques such as polling.
SocketIO在某些情况下使用优雅的协议降级来完成这两件事,以便显式地发出您可以监听和处理的本地事件。它开始尝试使用WebSockets,并最终回归到更昂贵的技术,如轮询。
SocketIO only works if the source of the information or change has decided to support the protocol. In those cases, the source is actually emitting the event (over websockets) and socketIO listens for it.
SocketIO仅在信息源或更改决定支持该协议时才有效。在这些情况下,源实际上发出事件(通过websockets)并且socketIO监听它。
In cases where the source of the information you are looking for does not support websockets (and hasn't been coded to explicitly notify your servers of changes), you are going to have to come up with your own solutions. However: You shouldn't think of this as a case of tracking "events". Rather, you are watching for changes.
如果您要查找的信息来源不支持websockets(并且尚未编码以明确通知您的服务器更改),您将不得不提出自己的解决方案。但是:您不应该将此视为跟踪“事件”的情况。相反,你正在关注变化。
How you watch for changes will depend on the nature of the change. Generally you'll probably have to poll for it.
您如何观察变化将取决于变化的性质。一般来说,你可能需要进行民意调查。
#1
The word "event" does not mean what you think it means!
“事件”这个词并不代表你认为它意味着什么!
- In a DOM environment, an Event is a very specific (and core) concept which allows you to write code based on user interactions with elements on the screen.
- In NodeJS, an Event is something that can be generated and announced by an instance of
events.EventEmitter
- In your question, an Event seems to refer to anything that happens on the internet, potentially anywhere.
在DOM环境中,Event是一个非常具体(和核心)的概念,它允许您根据用户与屏幕上元素的交互来编写代码。
在NodeJS中,Event可以由events.EventEmitter实例生成并公布
在你的问题中,一个事件似乎指的是互联网上发生的任何事情,可能在任何地方。
Under that last definition, there is simply no single answer for how to "track an event."
根据最后的定义,对于如何“跟踪事件”,根本没有单一的答案。
If you want to write code that can respond to change (which is just a more specific version of "react to an input") you need to create a mechanism to identify that a change has occurred, followed by a mechanism to trigger whatever code you want to be run in response (this last part is you would normally call "emitting" an "event").
如果你想编写可以响应变化的代码(这只是“对输入作出反应”的更具体版本),你需要创建一种机制来识别发生了变化,然后是一种机制来触发任何代码你想要在响应中运行(最后一部分是你通常称之为“发出”一个“事件”)。
SocketIO accomplishes both of these things for certain situations, using a graceful degredataion of protocols in order to explicitly emit local events that you can listen for and handle. It starts trying to use WebSockets, and eventually falls back to more expensive techniques such as polling.
SocketIO在某些情况下使用优雅的协议降级来完成这两件事,以便显式地发出您可以监听和处理的本地事件。它开始尝试使用WebSockets,并最终回归到更昂贵的技术,如轮询。
SocketIO only works if the source of the information or change has decided to support the protocol. In those cases, the source is actually emitting the event (over websockets) and socketIO listens for it.
SocketIO仅在信息源或更改决定支持该协议时才有效。在这些情况下,源实际上发出事件(通过websockets)并且socketIO监听它。
In cases where the source of the information you are looking for does not support websockets (and hasn't been coded to explicitly notify your servers of changes), you are going to have to come up with your own solutions. However: You shouldn't think of this as a case of tracking "events". Rather, you are watching for changes.
如果您要查找的信息来源不支持websockets(并且尚未编码以明确通知您的服务器更改),您将不得不提出自己的解决方案。但是:您不应该将此视为跟踪“事件”的情况。相反,你正在关注变化。
How you watch for changes will depend on the nature of the change. Generally you'll probably have to poll for it.
您如何观察变化将取决于变化的性质。一般来说,你可能需要进行民意调查。