javascript 没有 node.js 服务器的客户端 socket.io
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19506735/
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
Client-side socket.io without a node.js server
提问by Lin Ti-Wen
To use socket.io on the client side, usually we start a node.js server and go like this:
要在客户端使用 socket.io,通常我们启动一个 node.js 服务器并像这样:
<script src="/socket.io/socket.io.js"></script>
or with specific port:
或使用特定端口:
<script src="http://localhost:3700/socket.io/socket.io.js"></script>
Question is:
问题是:
is it necessary to use node.js server to serve socket.io.js ?
是否有必要使用 node.js 服务器来为 socket.io.js 提供服务?
...or is it possible to
……或者有没有可能
make a local copy of socket.io.js instead of goes to server every single time we need socket.io?
创建 socket.io.js 的本地副本,而不是每次我们需要 socket.io 时都去服务器?
like, we go to view source and copy everything we got from the source of script tag,
就像,我们去查看源代码并复制我们从脚本标签的源代码中获得的所有内容,
paste and save it as socket.io-local.jsso that next time we use:
粘贴并保存为socket.io-local.js以便我们下次使用:
<script src="socket.io-local.js"></script>
will that work ?
那行得通吗?
Updates
更新
Thanks for everyone's great response,
谢谢大家的热烈响应,
I'm asking this because in the case I'm involved, I don't actually have access to the server:
我问这个是因为在我参与的情况下,我实际上没有访问服务器的权限:
I am writing the client-side to connect to other developer's Socket Sever which is written in Java.
我正在编写客户端以连接到其他开发人员的用 Java 编写的套接字服务器。
Therefore I'll have to think a way to work around the fact that I don't have a server there for me.
因此,我必须想办法解决我没有服务器这一事实。
from what I've been testing, this way seems to work but I really don't know what's happening behind the scene.
从我一直在测试的内容来看,这种方式似乎有效,但我真的不知道幕后发生了什么。
回答by josh3736
You obviously can hostthe socket.io client library anywhere and pull it in to a page. However, it will almost certainly not workwith your Java-based server.
显然,您可以在任何地方托管socket.io 客户端库并将其拉入页面。但是,它几乎肯定不会与您的基于 Java 的服务器一起使用。
To understand why, you need to understand what socket.io is really doing behind the scenes; the client library is only a small part of it.
要理解其中的原因,您需要了解 socket.io 在幕后真正做了什么;客户端库只是其中的一小部分。
Socket.io actually defines and implements its own protocolfor realtime communication between a browser and a server. It does so in a way that supports multiple transports: if—for example—a user's browser or proxy doesn't support WebSockets, it can fall back to long polling.
Socket.io 实际上定义并实现了自己的协议,用于浏览器和服务器之间的实时通信。它以一种支持多种传输的方式这样做:例如,如果用户的浏览器或代理不支持WebSockets,它可以回退到长轮询。
What the socket.io client actually does is:
socket.io 客户端实际做的是:
- Makes a XHR
GET
request for/socket.io/1
. The server responds with a session ID, configured timeouts, and supported transports. - The client chooses the best transport that the user browser supports. In modern browsers, it will use WebSockets.
- If WebSockets are supported, it creates a new
WebSocket
to initiate a WebSocket connection (HTTPGET
withUpgrade: websocket
header) to a special URL –/socket.io/1/websocket/<session id>
. - If WebSockets aren't supported by the browser or fail to connect (there are lots of intermediaries in the wild like proxies, filters, network security devices, and so forth that don't support WebSocket requests), the library falls back to XHR long polling, and makes a XHR request to
/socket.io/1/xhr-polling/<sesion id>
. The server does not respond to the request until a new message is available or a timeout is reached, at which point the client repeats the XHR request.
- 发出 XHR
GET
请求/socket.io/1
。服务器使用会话 ID、配置的超时和支持的传输进行响应。 - 客户端选择用户浏览器支持的最佳传输。在现代浏览器中,它将使用 WebSockets。
- 如果支持 WebSockets,它会创建一个新的
WebSocket
来启动一个 WebSocket 连接(GET
带有Upgrade: websocket
标头的HTTP )到一个特殊的 URL –/socket.io/1/websocket/<session id>
. - 如果浏览器不支持 WebSockets 或无法连接(有很多中间人,如代理、过滤器、网络安全设备等不支持 WebSocket 请求),库会回退到 XHR 长轮询,并向
/socket.io/1/xhr-polling/<sesion id>
. 在新消息可用或达到超时之前,服务器不会响应请求,此时客户端会重复 XHR 请求。
Socket.io's server component handles the other end of that mess. It handles all the URLs under /socket.io/
, setting up sessions, parsing WebSocket upgrades, actually sending messages, and a bunch of other bookkeeping.
Socket.io 的服务器组件处理了混乱的另一端。它处理 下的所有 URL /socket.io/
,设置会话,解析 WebSocket 升级,实际发送消息,以及一堆其他簿记。
Without all of the services provided by the socket.io server, the client library is pretty useless. It will just make a XHR request to a URL that doesn't exist on your server.
如果没有 socket.io 服务器提供的所有服务,客户端库就毫无用处。它只会向服务器上不存在的 URL 发出 XHR 请求。
My guess is that your Java-based server just implements the WebSockets protocol. You can connect directly to it using the browser-provided WebSocket APIs.
我的猜测是您的基于 Java 的服务器只是实现了 WebSockets 协议。您可以使用浏览器提供的 WebSocket API直接连接到它。
It is possiblethat your server does implement the socket.io protocol – there are a few abandoned Java projects to do that – but that's unlikely. Talk with the developer of your server to find out exactly how he's implemented a "socket server."
这是可能的,你的服务器确实实现了socket.io协议-有一些废弃的Java项目要做到这一点-但那是不可能的。与您的服务器的开发人员交谈,了解他是如何实现“套接字服务器”的。
回答by Chev
A standalone build of socket.io-client is exposed automatically by the socket.io server as
/socket.io/socket.io.js
. Alternatively you can serve the filesocket.io-client.js
found at the root of this repository.
socket.io-client 的独立构建由 socket.io 服务器自动公开为
/socket.io/socket.io.js
. 或者,您可以提供在此存储库socket.io-client.js
根目录中找到的文件。
https://github.com/LearnBoost/socket.io-client
https://github.com/LearnBoost/socket.io-client
I have a module called shotgun-clientthat actually wraps socket.io. I needed to serve a custom client script as well as the socket.io client script, but I didn't want every user of my module to have to include multiple script references on their pages.
我有一个名为shotgun-client的模块,它实际上包装了 socket.io。我需要提供自定义客户端脚本以及 socket.io 客户端脚本,但我不希望我的模块的每个用户都必须在他们的页面上包含多个脚本引用。
I found that, when installed, you can serve the generated client script from socket.io by reading the file /node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js
. So my module adds a listener for its own URL and when it serves my custom client script it also serves the socket.io client script with it. Viola! Only a single script reference for the users of my module :)
我发现,安装后,您可以通过读取文件/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js
.io 为 socket.io 生成的客户端脚本提供服务。所以我的模块为它自己的 URL 添加了一个监听器,当它为我的自定义客户端脚本提供服务时,它也为 socket.io 客户端脚本提供服务。中提琴!我的模块的用户只有一个脚本参考:)
回答by SomeKittens
While this is technically possible, I don't see why you'd need to do that. If you're concerned about reducing the data that goes over the wire, this change won't actually do much beyond the few characters saved in the shorter src
tag. Simply changing the location of the JS file on the server won't actually improve performance - the JS has to be sent.
虽然这在技术上是可行的,但我不明白您为什么需要这样做。如果您担心减少通过线路传输的数据,那么除了保存在较短src
标签中的几个字符之外,此更改实际上不会做太多事情。简单地更改 JS 文件在服务器上的位置实际上不会提高性能 - 必须发送 JS。
Proper caching (which Socket.IO has) will return a 304 Not Modified (and not re-send the JS file every time you load a page).
正确的缓存(Socket.IO 具有)将返回 304 Not Modified(并且不会在每次加载页面时重新发送 JS 文件)。