如何使用socket.io进行后备工作? [英] How does fallback work with socket.io?

查看:234
本文介绍了如何使用socket.io进行后备工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Java 中使用 WebSocket 。问题是,我的服务器通过无法配置的代理与客户端分离。我一直在寻找带有后退的 WebSocket 的实现,例如 long-polling 。我发现 socket.io 但不知道后备是如何工作的。

I'd like to use WebSocket with Java. Problem is, my server is separated from the client by a proxy that cannot be configured. I've been searching for implementations of WebSocket with fallbacks such as long-polling. I've found socket.io but don't know how the fallback works.

在哪种情况下它会替换 WebSocket 以及如何?

Under which case does it replace WebSocket and how?

是否有其他库,如socket.io和回退实现?我想在 Java 中找到一个,但我只找到 Jetty

Are there other libraries like socket.io with fallbacks implementations? I would like to find one in Java, but I only found Jetty.

编辑:回退是否仅取决于浏览器与WebSocket的兼容性?如果失败的原因是代理配置错误,那么socket.io会将其检测为兼容性故障,从而切换到长轮询(或其他技术)吗?

does the fallback only depend on the browser's compatibility with WebSocket? What if the cause of failure is a proxy badly configured, is socket.io going to detect it as a compatibilty failure and thus switch to long-polling (or another technique)?

答案:自v1起,socket.io包含 engine.io ,它带来以下功能:

Answer: since v1, socket.io includes engine.io, which brings the following features:

< img src =https://i.stack.imgur.com/JR5vK.pngalt =在此处输入图像说明>

推荐答案

Socket.io是websockets协议的几个实现之一,它的主要卖点(IMO)易于使用:您不需要编写保持活动机制或决定哪个传输是最好的,它是为你做的。

Socket.io is one of several implementations for the websockets protocol and its main selling point (IMO) is its easy of use: you don't need to code keep-alive mechanisms or decide which transport is best, it does it for you.

所以,为了说清楚,socket.io不会取代websocket协议,它是一个为你实现它的包。

So, to make it clear, socket.io doesn't replace the websocket protocol, it's a package that implements it for you.

你提到了长轮询。这是socket.io使用的传输之一。长轮询是基于HTTP的,它基本上是请求 - >等待 - >响应并且等待时间不是很长,因为它可以被EOF上的负载平衡器或过时连接丢弃。然而,当websockets协议(基于TCP)不可用时,它仍然有用,socket.io会自动为您重新建立连接。请注意,websockets是一个相对较新的协议,于2011年批准,因此旧版浏览器不支持它。好吧,socket.io检测到然后转向长轮询,所以你不必担心它。

You mentioned long-polling. That is one of the transports used by socket.io. Long Polling is HTTP based and it's basically request --> wait --> response and the wait isn't very long, as it can be dropped by load balancers on EOF or stale connections. Nevertheless, it's still useful when the websockets protocol (TCP based) isn't available and socket.io automatically re-establishes the connection for you. Notice that websockets is a relatively new protocol, ratified in 2011, so older browsers don't support it. Well, socket.io detects that and then resorts to long polling, so you don't have to "worry" about it.

一个websocket连接以HTTP开头,正在监听在同一个港口。例如, http:// localhost:8080 (只是一个愚蠢的例子)。然后,当可能的时候,socket.io会为你切换到ws:// localhost:8080。

A websocket connection starts with HTTP, listening on the same port. For example, http://localhost:8080 (just a silly example). Then, when it's possible, socket.io switches to ws://localhost:8080 for you.

使用socket.io时我从未遇到网络拓扑挑战的问题,因为当HTTP端口可用并且使用长轮询/ websockets时,它对我来说很有用。

I never had problems with network topology challenges when using socket.io, as when the HTTP port is available and using long polling / websockets is possible, it just worked for me.

正如你所提到的,其中一个带回退实现的库是netty-socket.io。请注意它如何配置两个传输:

One of the libraries with fallback implementation, as you mentioned, is netty-socket.io. Notice how it configures the two transports:

public class Configuration {

    private ExceptionListener exceptionListener = new DefaultExceptionListener();

    private String context = "/socket.io";

    private List<Transport> transports = Arrays.asList(Transport.WEBSOCKET, Transport.POLLING);

    private int bossThreads = 0; // 0 = current_processors_amount * 2
    private int workerThreads = 0; // 0 = current_processors_amount * 2

可以找到完整的代码这里

Node JS还有用于websockets的库,我在这里提到它只是为了澄清长轮询和websockets不是唯一的两个可用传输(可能是Java中唯一的传输):

Node JS has also libraries for websockets, and I mention it here just to clarify that long polling and websockets aren't the only two available transports (might be the only ones in Java):

io.set('transports', [                     // enable all transports (optional if you want flashsocket)
            'websocket'
          , 'flashsocket'
          , 'htmlfile'
          , 'xhr-polling'
          , 'jsonp-polling'
        ]);

简而言之,socket.io试图让事情变得尽可能简单,包括没有担心要使用什么传输,因为它是为你做的,但如果你愿意,仍然可以配置。

In a nutshell, socket.io attempts to make things as easy as possible for you, including not having to worry about what transports to use, as it's done under the hood for you, yet still configurable if you want.

我希望这个简短的解释可以帮到你!

I hope this brief explanation helps you!

这篇关于如何使用socket.io进行后备工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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