Javascript TCP 套接字连接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9154035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 05:46:25  来源:igfitidea点击:

Javascript TCP socket connection

javascriptjqueryhtmlajaxsockets

提问by Mehran

Basically, I have a server that sends out info (in this case temperature data, every second)

基本上,我有一个发送信息的服务器(在这种情况下,每秒发送一次温度数据)

I want to have the temperature displayed and dynamically updated on a webpage, with only javascript.

我想在网页上显示和动态更新温度,只有 javascript。

I cannot change the server in anyway. It seems simple but I couldn't find what I needed.

我无论如何都无法更改服务器。看起来很简单,但我找不到我需要的东西。

采纳答案by Brad

What you need to do is make the connection with PHP, or something else server side, and return the result via AJAX to your JavaScript.

您需要做的是与 PHP 或其他服务器端建立连接,然后通过 AJAX 将结果返回给您的 JavaScript。

回答by phihag

JavaScript can notopen arbitrary TCP connections, as that would allow for all kinds of mayhem when combined with services who authenticate based on IP address, or not at all (and are only protected by firewalls). This includes Windows or NFS file shares, your average printer, your average LAN game, your average home router's web interface and lots of enterprise software.

JavaScript无法打开任意 TCP 连接,因为当与基于 IP 地址进行身份验证的服务结合使用时,这会导致各种混乱,或者根本不打开(并且仅受防火墙保护)。这包括 Windows 或 NFS 文件共享、普通打印机、普通局域网游戏、普通家用路由器的 Web 界面和许多企业软件。

You you cannot really prevent the browser from visiting a malicious site (since that includes visiting a trusted site with malicious ads). If these sites could effect arbitrary TCP connections, they could for example try out every IP address in the RFC 1918 private address ranges, or every IP address in the address range of the organization the client is coming from, and determine what devices you own. That alone would constitute a serious privacy breach, but imagine a malware site finding a printer and then printing out spamin your office once you visit it by accident.

您无法真正阻止浏览器访问恶意站点(因为这包括访问带有恶意广告的受信任站点)。如果这些站点可以影响任意 TCP 连接,例如,它们可以尝试RFC 1918 私有地址范围中的每个 IP 地址,或客户端所在组织的地址范围中的每个 IP 地址,并确定您拥有哪些设备。仅此一项就构成了严重的隐私泄露,但想象一下恶意软件站点找到了一台打印机,然后在您不小心访问它时在您的办公室打印垃圾邮件

What you can do is use a WebSocketserver to translate native TCP to WebSockets. Most Websocket implementations will have such a server as a demonstration app, but you can also use a standalone proxy.

您可以做的是使用WebSocket服务器将本机 TCP 转换为 WebSocket。大多数 Websocket 实现都有这样一个服务器作为演示应用程序,但您也可以使用独立代理