没有node.js服务器的客户端socket.io

问题描述:

要在客户端使用socket.io,通常我们启动一个node.js服务器并按如下所示:

To use socket.io on the client side, usually we start a node.js server and go like this:

<script src="/socket.io/socket.io.js"></script>

或特定港口:

<script src="http://localhost:3700/socket.io/socket.io.js"></script>



问题是:



是否有必要使用node.js服务器来提供socket.io.js?

Question is:

is it necessary to use node.js server to serve socket.io.js ?

制作socket.io.js的本地副本而不是每次我们需要socket.io去服务器吗?

make a local copy of socket.io.js instead of goes to server every single time we need socket.io?

喜欢,我们去查看源代码并复制我们从脚本标记源获得的所有内容,

like, we go to view source and copy everything we got from the source of script tag,

粘贴并将其保存为 socket.io-local。 js 以便我们下次使用:

paste and save it as socket.io-local.js so that next time we use:

<script src="socket.io-local.js"></script>

会有效吗?

感谢大家的好评,

我在问这是因为在我参与的情况下,我实际上无法访问服务器:

I'm asking this because in the case I'm involved, I don't actually have access to the server:

我正在编写客户端以连接到其他开发人员的Socket Sever这是用Java编写的。

I am writing the client-side to connect to other developer's Socket Sever which is written in 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.

您显然可以在任何地方托管 socket.io客户端库并将其拉入页面。但是,使用基于Java的服务器几乎肯定会无法正常工作

You obviously can host the socket.io client library anywhere and pull it in to a page. However, it will almost certainly not work with your Java-based server.

要了解原因,您需要了解socket.io是什么真的在幕后做;客户端库只是其中的一小部分。

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实际上定义并实现了自己的协议,用于浏览器和服务器之间的实时通信。它以支持多个传输的方式实现:例如—例如—用户的浏览器或代理不支持 WebSockets ,它可以回退到长轮询

Socket.io actually defines and implements its own protocol for 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客户端实际做的是:

What the socket.io client actually does is:


  1. 使XHR GET 请求 /socket.io/1 。服务器使用会话ID,配置的超时和支持的传输进行响应。

  2. 客户端选择用户浏览器支持的最佳传输。在现代浏览器中,它将使用WebSockets。

  3. 如果支持WebSockets,则会创建一个新的 WebSocket 启动WebSocket连接(HTTP GET ,带升级:websocket 标题)到特殊网址– /socket.io/1/websocket/<session id>

  4. 如果浏览器不支持WebSockets或失败连接(有许多中间人,如代理,过滤器,网络安全设备等不支持WebSocket请求),库回退到XHR长轮询,并向 /socket.io/1/xhr-polling/<sesion id> 。在新消息可用或达到超时之前,服务器不响应请求,此时客户端重复XHR请求。

  1. Makes a XHR GET request for /socket.io/1. The server responds with a session ID, configured timeouts, and supported transports.
  2. The client chooses the best transport that the user browser supports. In modern browsers, it will use WebSockets.
  3. If WebSockets are supported, it creates a new WebSocket to initiate a WebSocket connection (HTTP GET with Upgrade: websocket header) to a special URL – /socket.io/1/websocket/<session id>.
  4. 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.

Socket.io的服务器组件处理该混乱的另一端。它处理 /socket.io / 下的所有URL,设置会话,解析WebSocket升级,实际发送消息以及一堆其他簿记。

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发出XHR请求。

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.

我的猜测是你的基于Java的服务器只是实现了WebSockets协议。您可以使用浏览器提供的WebSocket API 直接连接到它。

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.

可能你的服务器确实实现了socket.io协议–有一些被遗弃的Java项目可以做到这一点–但那不太可能。与服务器的开发人员交谈,了解他是如何实现套接字服务器的。

It is possible that 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."