使用 Javascript 向客户端发送命令

发布于 2024-12-08 09:01:29 字数 359 浏览 1 评论 0原文

对于我正在规划的站点,我需要服务器能够向登录用户的浏览器发送命令。

简单的例子:当呼叫中心代表接到电话时,我想自动拉起来电的领导。

我已经整理好了其余的技术,我需要草图一下实际的沟通方式浏览器将会工作。

从我迄今为止的研究来看,这通常是通过桌面软件或插件来完成的,但在这一点上感觉相当老套。我知道可以用javascript来完成,要么通过编写一些不断轮询服务器命令的东西(这可以工作,但感觉有点浪费和蛮力),或者通过一些其他技术为服务器提供了一种调用客户端的方法。

像 stackoverflow 使用的任何机制来告诉您当前问题的新答案已经出现一样,可能会起到作用。

那么有已知的最佳实践吗?什么对你有用?

For a site I'm planning, I need the server to be able to send commands to a logged-in user's browser.

Simplic example: When a call center rep gets a call, I want to automatically pull up the lead who is calling in.

I've got the rest of the technology sorted out and I need to get a sketch together of how the actual communication to the browser will work.

From my research so far, this is often done with desktop software or plugins, but that feels pretty old school at this point. I know it can be done with javascript, either by writing something that constantly polls the server for commands (this could work, but it feels a little wasteful and brute force-y) or by some other technique that gives the server a way to call out to the client.

Something like whatever mechanism stackoverflow uses to tell you new answers have arrived on the current question would probably do the trick.

So is there a known best practice? What worked for you?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

罗罗贝儿 2024-12-15 09:01:29

正如其他答案所暗示的那样,WebSockets 和长轮询可能是您的一个不错的答案。但也要确保调查支持后端(系统的非浏览器、大铁部分)所需的软件。如果每个浏览器都需要一个始终打开的文件描述符,那么这可能会增加。 -- 有多种方法可以添加对更多文件描述符的支持,但请确定这是否是您想要采取的方法。

从整个系统的角度来看,设置轮询系统可能(也可能不会)更容易。轮询服务器没有任何问题,特别是专门的低开销、“我现在没有什么可以给你”类型的服务器。

请记住,您希望您的解决方案稳健。它应该顺利地处理客户端断开连接、重新启动、间歇性网络故障等。

请记住测试以上所有内容......

WebSockets and long polling may be a good answer for you, as the other answers suggest. But also be sure to investigate the sw needed to support either on the backend (the non-browser, big iron part of your system.) If an always open file descriptor is needed per browser, that can add up. -- there are ways to add support for more file descriptors, but decide if that's the road you want to take.

From a total system viewpoint, it may (or may not) be easier to set up a polling system. There's nothing wrong with polling the server, especially a specialized low overhead, "I've got nothing for you now"-type server.

Remember that you want your solution to be robust. It should smoothly handle client disconnects, reboots, intermittent network failure, etc.

Remember to test all of the above....

糖果控 2024-12-15 09:01:29

我相信这就是新的 Web 套接字 html 5 标准试图创建的内容,打开服务器和客户端之间的连接以允许双向通信。但在该标准生效之前,目前除了民意调查之外,我不知道还有其他方法。

I believe that this is what the new web sockets html 5 standard is attempting to create, opening a connection between server and client to allow two way communication. But before that standard takes hold I'm not aware of any way other than polling at the moment.

随梦而飞# 2024-12-15 09:01:29

服务器通常无法调用客户端。为了做到这一点,必须在客户端安装一个服务器软件来监听和响应。对于网络浏览器来说,就没有这样的事情。所以你唯一能做的就是轮询服务器并检查是否有新内容。

编辑:

我做了一个小测试来看看 stackoverflow 如何处理这个问题。我的结论是使用以下系统进行定期轮询。当您想要添加答案时,会向服务器发送一个发布请求,也许是为了启动一个计数器。之后,每隔一段时间,请求就会发送到 /posts/ID/editor-heartbeat/edit 发送草稿(在此之前您设法输入的内容),响应是一个包含请求成功状态和消息数量的 josn在那之前发布的。如果该数字不为 0,则会显示一个栏来提醒您。

The server can not usually call the client. In order to do that, there must be a server software installed on the client side that would listen and respond. In case of a web browser there is no such thing. So the only thing you can do is to poll the server and check if you have something new.

EDIT:

I made a little test to see how stackoverflow works with this. My conclusion is periodic polling with the following system. When you want to add an answer, a post request is sent to the server, maybe to start a counter. After that at some intervals, requests are sent to /posts/ID/editor-heartbeat/edit sending the draft (what you managed to type until then) and the respons is a josn containing the success status of your request and the number of messages that where posted until then. If that number is different than 0 than the bar is shown to alert you.

羁〃客ぐ 2024-12-15 09:01:29

Websockets 就是你想要的。

Websockets are what you want.

錯遇了你 2024-12-15 09:01:29

执行此操作的传统方法是客户端定期发出 XHRequest,询问“您有新消息要给我吗?”

还有更奇特的方法,其中涉及不关闭 TCP 连接和/或 iframe;如果你有兴趣,你可以谷歌一下 GMail 是如何做到的。

Websockets 是一项相当新的技术,仅在绝对最新的浏览器上实现,尽管我可能会冒险。

The traditional way to do this has been with periodic XHRequests from the client, asking "do you have a new message for me?"

There are fancier ways, which involve not closing a TCP connection and/or iframes; if you are interested you can google for how GMail does it.

Websockets are a fairly new technology which is only implemented on the absolutely latest browsers, though I might risk it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文