我可以使用 JavaScript(客户端)创建 websocket 服务器吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18157863/
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-27 10:55:10  来源:igfitidea点击:

Can I create a websocket server using JavaScript (client-side)?

javascriptwebsocket

提问by Oriol

I have just seen that the Websockets referenceMDN article says

我刚刚看到Websockets 参考MDN 文章说

WebSocketServer

Used for opening a new WebSocket server. Required information is (port, origin, location).

WebSocketServer

用于打开一个新的 WebSocket 服务器。所需信息是(港口、来源、位置)。

Then, does it mean I can create a websocket server client-side?

那么,这是否意味着我可以创建一个 websocket 服务器客户端?

If that's it, does it mean I can turn this... (each arrow is a websocket connection)

如果是这样,是否意味着我可以转动这个...(每个箭头都是一个 websocket 连接)

enter image description here

在此处输入图片说明

...into this?

……进入这个?

enter image description here

在此处输入图片说明

But, do browsers have the power of doing that without any router/firewall configuration?

但是,浏览器是否有能力在没有任何路由器/防火墙配置的情况下做到这一点?

And how can I use it? The WebSocketServerlinkis broken. And I have tried searching it but I haven't found anything.

我该如何使用它?该WebSocketServer链接被打破。我试过搜索它,但我什么也没找到。

采纳答案by redct

It looks like WebSocketServer is something currently under development by Mozilla without much support or anything of the like. I searched through some of their repositoriesand couldn't find any references, except for in some testing code for normal WebSockets.

看起来 WebSocketServer 是 Mozilla 目前正在开发的东西,没有太多支持或类似的东西。我搜索了他们的一些存储库,找不到任何参考资料,除了一些普通 WebSockets 的测试代码。

If what you're looking for is some form of P2P WebSockets, I don't think that's possible without some work right now. You need, as others have said, a server endpoint for the connection, something that is most popularly implemented in languages like Python and node.js

如果您正在寻找某种形式的 P2P WebSockets,我认为现在不做一些工作是不可能的。正如其他人所说,您需要一个用于连接的服务器端点,它是最常用的语言(如 Python 和 node.js)实现的

回答by hesson

You need to have a server-side endpoint for the WebSocket. A web socket is an open connection between the server and a client - how could you possible achieve this without a server? I don't quite understand what you are trying to convey with the diagrams, but you need to set up special configurations on the server-side to create a web socket resource like this: ws://example.com/resource so you can't arbitrarily create websockets between two clients and such.

您需要有一个用于 WebSocket 的服务器端端点。网络套接字是服务器和客户端之间的开放连接——没有服务器你怎么可能实现这一点?我不太明白你想用图表表达什么,但你需要在服务器端设置特殊的配置来创建一个像这样的网络套接字资源:ws://example.com/resource 这样你就可以不要在两个客户端之间任意创建 websockets 等。