应用程序负载均衡器是否支持 WebSockets? [英] Does an Application Load Balancer support WebSockets?

查看:25
本文介绍了应用程序负载均衡器是否支持 WebSockets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个最初配置为使用 Classic Load Balancer 的 Elastic Beanstalk 应用程序.我发现这在通过 WebSocket 连接时会导致错误.因此,我将应用程序配置为使用应用程序负载均衡器,因为有人告诉我 ALB 支持 WebSockets.但是,他们似乎没有:尝试通过 WebSocket 连接到我的 ALB 时,我遇到了完全相同的错误.

ALB 真的支持 WebSocket 吗?AWS 文档对此自相矛盾.

接下来,将新的侦听器规则添加到您的 ALB.此规则必须有一个路径来路由 WebSocket 设置 --/socket.io.此外,将目标组名称设置为您刚刚创建的目标组.

我的服务器使用 Node/Hapi/Socket.io(在源自 Amazon Linux AMI 的实例上运行).基本设置是:

const hapi = require('hapi');const websocket = require('./WebSocket');var server = new hapi.Server();server.connection(config.Application);websocket.Initialize(server.listener);

WebSocket.js 在哪里

var io = null;模块.出口 = {初始化:函数(http){io = require('socket.io')(http);io.on('connection', function (socket) {console.log('Websocket' + socket.id + '已连接.');socket.on('断开', 函数 () {console.log('Websocket' + socket.id + '断开连接.');});});}};

我的客户端使用 Angular 1.5x,带有 socket.io-client.如下配置 WebSocket 客户端选项很重要,否则您将无法连接.

(函数(){'使用严格';有角的.module('XXXXX', []).run(runHandler);runHandler.$inject = ['WebSocketService'];函数运行处理器(WebSocketService){WebSocketService.Initialize();}})();

WebSocket 服务:

(函数(){'使用严格';有角的.module('XXXXX').factory('WebSocketService', WebSocketService);WebSocketService.$inject = [];函数 WebSocketService() {var socket = null;函数初始化(){var url = 'http://' + ALB_URL + ':5800';socket = io(url, {transsports: ['websocket'], upgrade: false});socket.on('connect', function () {console.log('套接字已连接');});socket.on('断开', 函数 () {console.log('套接字断开');});}返回 {初始化:初始化};}})();

I have an Elastic Beanstalk application which was initially configured to use a Classic Load Balancer. I found that this caused errors when connecting via WebSocket. Because of this, I configured the application to use an Application Load Balancer instead, because I was told that ALBs support WebSockets. However, it seems that they do not: I get exactly the same error when attempting to connect to my ALB via WebSocket.

Do ALBs actually support WebSocket? The AWS documentation is contradictory on this. This page says it only supports HTTP and HTTPS. There are no guides to setting up an ALB to support WebSocket.

解决方案

I was able to get WebSockets working with the new Application Load Balancer (ALB).

First, create a new Target Group for your ALB. This Target Group should use the same port as your application, and will need to have health checks configured. However, the main difference is that you must enable Stickiness.

Next, add a new Listener Rule to your ALB. This rule must have a Path to route the WebSocket setup -- /socket.io. Also, set the Target Group Name to the Target Group you just created.

I am using Node/Hapi/Socket.io for my server (running on instance derived from Amazon Linux AMI). Basic setup is:

const hapi = require('hapi');
const websocket = require('./WebSocket');

var server = new hapi.Server();
server.connection(config.Application);
websocket.Initialize(server.listener);

where WebSocket.js is

var io = null;

module.exports = {

    Initialize: function (http) {

        io = require('socket.io')(http);

        io.on('connection', function (socket) {
            console.log('Websocket ' + socket.id + ' connected.');

            socket.on('disconnect', function () {
                console.log('Websocket ' + socket.id + ' disconnected.');
            });
        });
    }
};

I am using Angular 1.5x for my client, with socket.io-client. It is important to configure the WebSocket client options as follows, or you will not be able to connect.

(function () {

    'use strict';

    angular
        .module('XXXXX', [])
        .run(runHandler);

    runHandler.$inject = ['WebSocketService'];

    function runHandler(WebSocketService) {
       WebSocketService.Initialize();
    }
})();

The WebSocket service:

(function () {

    'use strict';

    angular
        .module('XXXXX')
        .factory('WebSocketService', WebSocketService);

    WebSocketService.$inject = [];

    function WebSocketService() {

        var socket = null;

        function initialize() {

            var url = 'http://' + ALB_URL + ':5800';

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

            socket.on('connect', function () {
                console.log('Socket connected');
            });

            socket.on('disconnect', function () {
                console.log('Socket disconnected');
            });
        }

        return {
            Initialize: initialize
        };
    }
})();

这篇关于应用程序负载均衡器是否支持 WebSockets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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