是否可以无缝重定向websockets? [英] Is it possible to seamlessly redirect websockets?

查看:762
本文介绍了是否可以无缝重定向websockets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道可以通过反向代理(如Nginx,HAproxy等)传递请求,但我需要将请求重定向到同一域后缀中的另一个公共服务器。即从 wss://example.com wss://ws1.example.com

以下是一个示例:

I know that it is possible to pass requests through the reverse proxy (like Nginx, HAproxy and so on) but I need to redirect requests to another public server in the same domain suffix. I.e. from wss://example.com to wss://ws1.example.com.
Here is an example:

我需要重定向来自Nginx或Java的请求。
是否可以组织?我是否需要在客户端处理重定向或此代码是否足够?

I need to redirect requests from Nginx or Java. Is to possible to organize? Do I need to handle redirects on a client side or this code is enough?

var socket = new WebSocket("wss://example.com");

socket.onopen = function() {
  alert("Connection established.");
};

socket.onclose = function(event) {
  alert('Connection closed');
};

socket.onmessage = function(event) {
  alert("Data: " + event.data);
};

socket.onerror = function(error) {
  alert("Error " + error.message);
};


推荐答案

根据 webSocket规范


一旦客户端打开握手在发送任何进一步的数据之前,客户端必须等待服务器的响应。客户端必须按如下方式验证服务器的响应:

Once the client's opening handshake has been sent, the client MUST wait for a response from the server before sending any further data. The client MUST validate the server's response as follows:


  1. 如果从服务器收到的状态代码不是101,则
    客户端处理每个HTTP [RFC2616]过程的响应。特别是
    ,如果
    收到401状态代码,客户端可能会执行身份验证;服务器可能使用3xx状态代码重定向客户端
    (但客户不需要遵循
    )等等。


因此,它是否完全取决于客户是否要支持重定向,显然不是您可以依赖的东西,除非您在广泛的测试中发现所有相关客户都支持它(他们显然没有)。

So, it's purely up to the client whether they want to support redirects or not and is clearly not something you can rely on unless you find in extensive testing that all relevant clients support it (which they apparently do not).

你要么必须使用服务器端代理或客户端方案来手动将连接移动到另一个服务器。

You will either have to go with something like a server-side proxy or a client-side scheme to manually move the connection to another server.

这篇关于是否可以无缝重定向websockets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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