简体   繁体   中英

Javascript TCP socket connection

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.

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

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

JavaScript can not open 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.

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 spam in your office once you visit it by accident.

What you can do is use a WebSocket server 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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