听服务器更改 [英] Listen to server changes
问题描述
服务器具有某些变量,其值会定期更改。这些更改不会发出任何事件。我的JavaScript应用程序通过服务器的HTTP请求接收值,在这里我可以访问服务器的当前变量值。
a server has certain variables with values which change on a regular basis. These changes don't emit events or anything. My JavaScript Application receives the values through HTTP-Requests with the Server, where i can access the current variable values of the server.
因为我一直都想使用最新的方法到目前为止,是一个间隔内的正常GET请求。
但这似乎不正确...
Since i always want the latest value my approach so far is a normal GET Request inside an interval. But this does not seem right...
有人有更好的主意吗?我不能使用套接字,因为服务器没有发出任何信号,对吧?
Does anyone have a better idea? I can't use sockets since the server isn't emitting anything, right?
谢谢!
推荐答案
在node.js应用程序(服务器)中,您可以:
In a node.js app (server), you could :
- 设置
socket.io
与客户的连接 - 创建事件
EventEmitter
A
- 订阅事件发射器
- 执行
setInterval
检查您的变量 - 当其中一个变量更改时->
A.emit('变量名称',newValue)
- 您从
A接收到事件
并通过socket.io
- set up a
socket.io
connection with your client - create an event
EventEmitter
A
- subscribe to your event emitter
- do a
setInterval
to check your variables - when one of your variables has changed ->
A.emit('variable-name', newValue)
- You recieve the event from
A
and send it to your client throughsocket.io
这篇关于听服务器更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!