简体   繁体   English

node.js 中所有会话通用的整数变量

[英]Integer variable common for all sessions in node.js

How to have an integer variable common for all client sessions in node.js and access them in client html?如何在 node.js 中为所有客户端会话设置一个通用的整数变量并在客户端 html 中访问它们? (that is, I need a integer variable which can be displayed in the html page and can be updated in html page. The new updated value should be displayed for other clients.) I knew this is a silly question. (也就是说,我需要一个可以在html页面中显示并可以在html页面中更新的整数变量。应该为其他客户端显示新的更新值。)我知道这是一个愚蠢的问题。 But I am sorry.但我很抱歉。 I am totally new to node.js and I do not have much time.我对 node.js 完全陌生,我没有太多时间。 I need a solution at the earliest.我需要尽早解决。 Thank you.谢谢你。

There are multiple options for getting the common variable into a web page:将公共变量放入网页有多种选择:

  1. You can embed an actual <script> tag with the variable in every page that is served from the server.您可以在服务器提供的每个页面中嵌入带有变量的实际<script>标记。 This technique would be easiest if using a template engine so that a common sub-template can be included in every page.如果使用模板引擎,那么这种技术将是最简单的,这样每个页面中都可以包含一个公共子模板。

  2. You can have each page send an ajax call to the server when it first loads to fetch the value of the variable.您可以让每个页面在第一次加载时向服务器发送一个 ajax 调用以获取变量的值。

  3. You can have each page create a webSocket connection to the server when it first loads and then when the server receives the connection, it can send back the value of the variable.你可以让每个页面在第一次加载时创建一个到服务器的 webSocket 连接,然后当服务器收到连接时,它可以发回变量的值。

When the client wants to update the variable, it has a couple options for informing the server:当客户端想要更新变量时,它有几个选项可以通知服务器:

  1. If using the webSocket connection above, it can just send a message to the server that the variable has been uddated and send the new value.如果使用上面的 webSocket 连接,它可以只向服务器发送一条消息,表明该变量已被更新并发送新值。 The server can then broadcast to the other webSocket-connected clients what the new value is.然后服务器可以向其他 webSocket 连接的客户端广播新值是什么。

  2. If using the either of the other two options above, then whenever the client wants to change the value, it can issue an ajax call to the server to update the value.如果使用上述其他两个选项中的任何一个,那么无论何时客户端想要更改该值,它都可以向服务器发出 ajax 调用以更新该值。 Other than the webSocket option above, the only way for the other clients to know when the value of the variable changes is for each client to regularly poll the server asking to fetch the current value of the variable (say every 30 seconds or so).除了上面的 webSocket 选项,其他客户端知道变量值何时更改的唯一方法是每个客户端定期轮询服务器,要求获取变量的当前值(比如每 30 秒左右)。 This is not particularly efficient.这不是特别有效。

Because part of your problem is for all connected clients to know when the variable has change, you need the ability for the server to "push" an updated value to all clients.因为您的问题的一部分是所有连接的客户端都知道变量何时发生变化,所以您需要服务器能够将更新的值“推送”到所有客户端。 This is what webSockets were invented for and would likely be the recommended option.这就是 webSockets 的发明目的,并且可能是推荐的选项。

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

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