任何方式的NodeJS服务器推Javascript来客户端的更新?客户端、方式、服务器、NodeJS

2023-09-10 15:00:59 作者:北栀怨寒

在Scala的Liftweb有一些类允许用于Javascript被通过彗星/ AJAX推到浏览器的页面加载后。 这里有些文档。

In Scala's Liftweb there are some classes which allow for Javascript to be pushed to the browser via comet/ajax after the page has loaded. Some documentation here.

的使用可以是,例如当某人提交一个表单到服务器,该形式将通过AJAX提交然后一些JavaScript可以从服务器发送到客户机,以显示一些错误消息。嗯,这只是举例来说,有更好的方法做表单验证。

The usage could be for example when someone submits a form to the server, the form would submit via AJAX and then some Javascript can be sent from the server to the client to show some error message. Well this is just for example, there are better ways to do form validation.

有没有办法从NodeJS服务器推的Javascript给客户做更新?任何标准的方式来做到这一点?为什么我要做到这一点,它的更好,以让所有客户端和服务器code位于同一个服务器上的文件。

Is there any way to push Javascript from the NodeJS server to the client to do updates? Any standard way to do this? Why I want to do this is that its nicer to have all client and server code located in one server file.

另外,作为一个次要的问题,我可以从服务器端访问该网页的数据?这将是很好能够从服务器端从服务器端查询页面元素或查询一些页面数据。

Also as a secondary question, can I access the page data from the server side? It would be nice to be able to query a page element from the server side or query some page data from the server side.

推荐答案

您可以通过基于动态对象predefining函数发送双向,或者通过建立一个系统建立这个功能集成到socket.io,这将解析和双方运行JavaScript。根据你正在努力做到这一点可能需要一段时间得到什么,能够安全地为你工作(不只是扔一切的eval());

You can build this functionality into socket.io, either by predefining functions based on dynamic objects that are sent bi-directionally, or by building a system which will parse and run javascript from both sides. Depending on what you are trying to do it might take a while to get that working for you securely (not just throwing everything in eval());

http://socket.io/

另外还有建在Node.js的一个图书馆具有此功能内置的。我没有亲自使用过它,所以我不能证明该系统的可靠性和安全性,但它应该是一个良好的开端

Alternatively there is a library built on node.js that has this functionality built in. I have not personally used it so I can't attest to the reliability or security of the system, but it should be a good place to start.

http://www.nowjs.com/ https://github.com/Flotype/now

希望这有助于!