没有 node.js 服务器的客户端 socket.io [英] Client-side socket.io without a node.js server

查看:24
本文介绍了没有 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>

问题是:

socket.io.js 是否需要使用 node.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:

我正在编写客户端以连接到其他开发人员的用 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. /socket.io/1 发出 XHR GET 请求.服务器使用会话 ID、配置的超时和支持的传输进行响应.
  2. 客户端选择用户浏览器支持的最佳传输方式.在现代浏览器中,它将使用 WebSockets.
  3. 如果支持 WebSockets,它会创建一个新的 WebSocket 启动到特殊 URL 的 WebSocket 连接(带有 Upgrade: websocket 标头的 HTTP GET)/socket.io/1/websocket/.
  4. 如果浏览器不支持 WebSockets 或无法连接(有很多中间人,如代理、过滤器、网络安全设备等不支持 WebSocket 请求),库会回退到 XHR 长轮询,并向 /socket.io/1/xhr-polling/ 发出 XHR 请求.在新消息可用或达到超时之前,服务器不会响应请求,此时客户端会重复 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."

这篇关于没有 node.js 服务器的客户端 socket.io的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆