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

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

问题描述

我有一个Elastic Beanstalk应用程序,该应用程序最初配置为使用经典负载均衡器.我发现这在通过WebSocket连接时导致错误.因此,我被配置为使用应用程序负载平衡器代替应用程序,因为有人告诉我ALB支持WebSockets.但是,似乎它们并没有:尝试通过WebSocket连接到我的ALB时出现完全相同的错误.

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.

ALB是否真正支持WebSocket? AWS文档与此矛盾. 此页面说,它仅支持HTTP和HTTPS.没有设置ALB以支持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.

推荐答案

我能够使WebSocket与新的应用程序负载平衡器(ALB)一起使用.

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

首先,为您的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.

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

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.

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

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);

WebSocket.js所在的位置

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.');
            });
        });
    }
};

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

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();
    }
})();

WebSocket服务:

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
        };
    }
})();

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

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