客户端套接字。io没有节点。js服务器

时间:2022-02-10 16:07:53

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

使用套接字。在客户端io,通常我们启动一个节点。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时都去服务器?


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.js so that next time we use:

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

<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.

我正在编写客户端来连接其他开发人员的Socket服务器,它是用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.

从我所做的测试来看,这种方法似乎有效,但我真的不知道幕后发生了什么。

3 个解决方案

#1


24  

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.

显然可以托管套接字。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.

要理解为什么,您需要了解什么插座。io真的是在幕后工作;客户端库只是其中的一小部分。

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.

套接字。io实际上定义并实现了自己的协议,用于浏览器和服务器之间的实时通信。它以一种支持多种传输的方式进行:例如,如果用户的浏览器或代理不支持WebSockets,它可以返回到长轮询。

What the socket.io client actually does is:

套接字。io客户端实际上是:

  1. Makes a XHR GET request for /socket.io/1. The server responds with a session ID, configured timeouts, and supported transports.
  2. 请求/socket.io/1。服务器使用会话ID、配置的超时和支持的传输进行响应。
  3. The client chooses the best transport that the user browser supports. In modern browsers, it will use WebSockets.
  4. 客户端选择用户浏览器支持的最佳传输。在现代浏览器中,它将使用WebSockets。
  5. 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>.
  6. 如果支持WebSockets,它会创建一个新的WebSocket来启动一个WebSocket连接(HTTP GET with Upgrade: WebSocket header)到一个特殊的URL - /socket。io / 1 / websocket / <会话id> 。
  7. 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.
  8. 如果浏览器不支持WebSockets,或者连接失败(在野生环境中有很多中介体,比如代理、过滤器、网络安全设备等等,这些都不支持WebSocket请求),那么这个库就会退回到XHR长轮询,并向/socket发出XHR请求。io / 1 / xhr-polling / <对话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.

套接字。io的服务器组件处理的是另一端的混乱。它处理/socket下的所有url。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.

没有套接字提供的所有服务。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 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."

您的服务器可能实现了套接字。io协议——有一些废弃的Java项目可以做到这一点——但这不太可能。与服务器的开发人员交谈,了解他是如何实现“套接字服务器”的。

#2


4  

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 file socket.io-client.js found at the root of this repository.

套接字的独立构建。io-client自动由套接字公开。io服务器/ socket . io / socket.io.js。或者,您也可以为file socket.io-client提供服务。在此存储库的根目录中找到的js。

https://github.com/LearnBoost/socket.io-client

https://github.com/LearnBoost/socket.io-client

I have a module called shotgun-client that 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。我需要为客户端脚本和套接字提供服务。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 :)

我发现,当安装时,您可以从套接字中服务生成的客户端脚本。io通过读取文件/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js来实现io。因此,我的模块为它自己的URL添加一个侦听器,当它服务于我的自定义客户端脚本时,它也服务于套接字。带有它的io客户端脚本。中提琴!我的模块用户只有一个脚本引用:)

#3


1  

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).

适当的缓存(套接字。IO已)将返回未修改的304(并且不会在每次加载页面时重新发送JS文件)。

#1


24  

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.

显然可以托管套接字。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.

要理解为什么,您需要了解什么插座。io真的是在幕后工作;客户端库只是其中的一小部分。

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.

套接字。io实际上定义并实现了自己的协议,用于浏览器和服务器之间的实时通信。它以一种支持多种传输的方式进行:例如,如果用户的浏览器或代理不支持WebSockets,它可以返回到长轮询。

What the socket.io client actually does is:

套接字。io客户端实际上是:

  1. Makes a XHR GET request for /socket.io/1. The server responds with a session ID, configured timeouts, and supported transports.
  2. 请求/socket.io/1。服务器使用会话ID、配置的超时和支持的传输进行响应。
  3. The client chooses the best transport that the user browser supports. In modern browsers, it will use WebSockets.
  4. 客户端选择用户浏览器支持的最佳传输。在现代浏览器中,它将使用WebSockets。
  5. 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>.
  6. 如果支持WebSockets,它会创建一个新的WebSocket来启动一个WebSocket连接(HTTP GET with Upgrade: WebSocket header)到一个特殊的URL - /socket。io / 1 / websocket / <会话id> 。
  7. 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.
  8. 如果浏览器不支持WebSockets,或者连接失败(在野生环境中有很多中介体,比如代理、过滤器、网络安全设备等等,这些都不支持WebSocket请求),那么这个库就会退回到XHR长轮询,并向/socket发出XHR请求。io / 1 / xhr-polling / <对话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.

套接字。io的服务器组件处理的是另一端的混乱。它处理/socket下的所有url。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.

没有套接字提供的所有服务。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 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."

您的服务器可能实现了套接字。io协议——有一些废弃的Java项目可以做到这一点——但这不太可能。与服务器的开发人员交谈,了解他是如何实现“套接字服务器”的。

#2


4  

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 file socket.io-client.js found at the root of this repository.

套接字的独立构建。io-client自动由套接字公开。io服务器/ socket . io / socket.io.js。或者,您也可以为file socket.io-client提供服务。在此存储库的根目录中找到的js。

https://github.com/LearnBoost/socket.io-client

https://github.com/LearnBoost/socket.io-client

I have a module called shotgun-client that 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。我需要为客户端脚本和套接字提供服务。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 :)

我发现,当安装时,您可以从套接字中服务生成的客户端脚本。io通过读取文件/node_modules/socket.io/node_modules/socket.io-client/dist/socket.io.js来实现io。因此,我的模块为它自己的URL添加一个侦听器,当它服务于我的自定义客户端脚本时,它也服务于套接字。带有它的io客户端脚本。中提琴!我的模块用户只有一个脚本引用:)

#3


1  

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).

适当的缓存(套接字。IO已)将返回未修改的304(并且不会在每次加载页面时重新发送JS文件)。