Socket.io 1.x:仅使用WebSockets? [英] Socket.io 1.x: use WebSockets only?

查看:100
本文介绍了Socket.io 1.x:仅使用WebSockets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开发一个仅在现代浏览器(IE10 +)上运行的Web应用程序,原因各不相同。

We are developing a web application that will run only on modern browsers (IE10+) for different reasons.

我们实现的功能之一是Socket.io 1。 X。但是,默认情况下,Socket.io客户端会尝试支持较旧的浏览器,因此它会启动与长轮询的连接,然后将其更新到WebSockets。这是浪费时间和资源,因为我们肯定知道浏览器支持WS。

One of the features we implemented is Socket.io 1.x. However, by default the Socket.io client tries to support older browsers, so it starts a connection with long polling and then updates that to WebSockets. This is a waste of time and resources, given we know for sure the browser supports WS.

我一直在搜索,我只能找到这个wiki页面,但是,这是关于Socket.io 0.9。

I've searched around, and I can only find this wiki page which, however, is about Socket.io 0.9.

最后,我找到了 engine.io-client的文档(在哪个Socket上) .io-client基于1.x分支)。这是我写的代码,似乎正在工作。但是,我想知道它是否正确或者我做错了什么:

Eventually, I found the documentation for engine.io-client (on which Socket.io-client is based on the 1.x branch). This is the code that I wrote and seems to be working. However, I would like to know if it's correct or if I'm doing something wrong:

io.connect('https://...', {
    upgrade: false,
    transports: ['websocket']
})

奇怪的是,只需将 transports 属性设置为一个包含 websockets 的数组只是还不够;我还必须禁用 upgrade 。这是正确的吗?

Weirdly, just setting the transports property to an array with websockets only wasn't enough; I also had to disable upgrade. Is this correct?

我做了一些新的发现。

只有 transports 设置为 ['websocket'] ,它没有任何区别是否已启用升级。这是正常的吗?

With transports set to ['websocket'] only, it doesn't make any difference wether upgrade is enabled or not. Is that normal?

推荐答案

socket.io发生了两种类型的升级。首先(在socket.io 1.0+中),socket.io启动所有与http轮询请求的连接,它实际上可能只用一个http请求交换一些初始数据。然后,在此之后的某个时刻,它将尝试实际启动webSocket连接。 webSocket连接是通过发送指定 upgrade:websocket 标头的特定类型的http请求完成的,然后服务器可以适当地响应它是否支持websocket。如果服务器同意升级,则该特定http连接被升级为webSocket协议。此时,客户端知道webSocket受支持并且它停止使用轮询http请求,从而完成其升级到webSocket。

There are two types of "upgrades" happening with socket.io. First (in socket.io 1.0+), socket.io starts all connections with an http polling request and it may actually exchange some initial data with just an http request. Then, at some point after that, it will try to actually initiate a webSocket connection. the webSocket connection is done by sending a particular type of http request that specifies an upgrade: websocket header and the server can then respond appropriately whether it supports websocket or not. If the server agrees to the upgrade, then that particular http connection is "upgraded" to the webSocket protocol. At that point, the client then knows that webSocket is supported and it stops using the polling http requests, thus completing its upgrade to webSocket.

您可以通过在客户端上执行此操作来完全阻止初始http轮询:

You can prevent the initial http polling entirely by doing this on the client:

var socket = io({transports: ['websocket'], upgrade: false});

这将阻止来自您自己的合作客户的轮询连接。如果您想阻止任何客户端使用轮询,那么您可以将其添加到服务器:

This will prevent polling connections from your own cooperating clients. If you want to prevent any clients from ever using polling, then you can add this to the server:

io.set('transports', ['websocket']);

但是,如果在服务器上设置此项,那么最初使用http轮询连接的socket.io客户端根本不会工作。因此,这应该只与客户端中的正确设置匹配,以便客户端永远不会以轮询开始。

But, if you set this on the server, socket.io clients that are initially connecting with http polling will not work at all. So, this should only be matched with the right settings in the client such that the client never starts with polling.

这将告诉两端您只想使用webSockets和socket.io将在开头跳过额外的http轮询。公平的警告,这样做需要webSocket支持,所以这排除了与尚未支持webSocket的旧版IE兼容。如果你想保留兼容性,那么就让socket.io最初用几个http请求做这件事。

This will tell both ends that you only want to use webSockets and socket.io will skip the extra http polling at the beginning. Fair warning, doing this requires webSocket support so this rules out compatible with older versions of IE that didn't yet support webSocket. If you want to retain compatibility, then just let socket.io do it's thing with a couple http requests initially.

这里是有关协议从http升级到webSocket的更多信息。

Here's more info on the protocol upgrade from http to webSocket.

webSockets协议使用HTTP连接启动每个webSocket。这就是所有webSockets的工作方式。该HTTP连接包含一些标题,表明浏览器喜欢升级到webSockets协议。如果服务器支持该协议,则它会响应告知客户端它将升级到webSocket协议,然后该套接字将从HTTP协议切换到webSocket协议。这就是webSocket连接的设计方式。因此,您看到以HTTP连接开头的webSocket连接的事实是100%正常。

The webSockets protocol initiates EVERY webSocket with an HTTP connection. That's the way all webSockets work. That HTTP connection contains some headers on it that indicate that the browser would "like" to upgrade to the webSockets protocol. If the server support that protocol, then it responds telling the client that it will upgrade to the webSocket protocol and that very socket then switches from the HTTP protocol to the webSocket protocol. This is how a webSocket connection is designed to work. So, the fact that you see your webSocket connection starting with an HTTP connection is 100% normal.

您可以将socket.io配置为永远不会使用长轮询,如果这让你感觉更好,但这不会改变这样一个事实,即webSocket连接仍将以HTTP连接开始,然后升级到webSocket协议,并且它不会提高支持webSockets的现代浏览器的操作效率。但是,它会使您的连接无法在旧版浏览器中使用。

You can configure socket.io to NEVER use long polling if that makes you feel better, but this will not change the fact that the webSocket connection will still start with an HTTP connection that is then upgraded to the webSocket protocol and it will not improve the efficiency of operation in modern browsers that support webSockets. It will, however make it so that your connection will not work in older browsers.

这篇关于Socket.io 1.x:仅使用WebSockets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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