问题描述
限时送ChatGPT账号..我有一个 java 客户端,我需要用(客户端)javascript 重写它.我像这样打开java Socket:
I have a java client and I need to rewrite it in (client-side) javascript. I open the java Socket like this:
socket socket = new Socket("127.0.0.1", 5015);
Socket socket = new Socket("127.0.0.1", 5015);
所以我尝试在 javascript 中使用 websocket:let socket = new WebSocket("http://127.0.0.1:5015");
So I tried to use websocket in javascript: let socket = new WebSocket("http://127.0.0.1:5015");
但是这里我有一个 js 错误:
but here I have a js error:
未捕获的 DOMException:无法构建WebSocket":
Uncaught DOMException: Failed to construct 'WebSocket':
URL 的方案必须是ws"或wss".不允许使用http".
The URL's scheme must be either 'ws' or 'wss'. 'http' is not allowed.
我也尝试使用 'ws' 或 'wss' 协议,但服务器不想与此类协议握手.
I tried also to use the 'ws' or 'wss' protocol but the server didn't want to handshake with such protocols.
有没有办法在客户端javascript中建立这样的套接字连接,或者绝对禁止?
Is there a way to make such socket connection in client-side javascript or it's definitely prohibited?
推荐答案
不,您不能在任何浏览器中从网页建立任意 TCP 连接.
No, you can't make an arbitrary TCP connection from a web page in any browser.
Web 套接字与 TCP 套接字有着根本的不同……它们本质上是无关的.它们是 HTTP 之上的一个薄层以及一个客户端 API,允许 Web Socket 客户端和支持 Web Socket 的服务器之间进行双向通信.
Web Sockets are fundamentally different than TCP sockets... they're essentially unrelated. They're a thin layer on top of HTTP along with a client API which allows bidirectional communication between a Web Socket client and a server supporting Web Sockets.
您可以运行代理服务器,允许通过它们进行连接以建立 TCP 连接,但这当然是服务器功能,而不是您可以单独在浏览器中执行的操作.
There are proxy servers you can run that allow connecting through them to make TCP connections, but this of course is a server feature and not something you can do in-browser alone.
这篇关于在客户端 javascript 中创建 TCP/IP 套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论