AWS EC2实例WebSocket连接失败:连接建立错误:net :: ERR_CONNECTION_TIMED_OUT [英] An Issue with an AWS EC2 instance WebSocket connection failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

查看:1997
本文介绍了AWS EC2实例WebSocket连接失败:连接建立错误:net :: ERR_CONNECTION_TIMED_OUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从连接到MySQL数据库(通过WebSocket用PHP编码)的本地主机运行聊天应用程序时,它是成功的。



当我尝试运行时从PuTTY终端登录到SSH凭据,它显示为服务器启动,端口号为8080

  ubuntu @ ec3-193- 123-96:/home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server$ php websocket_server.php 
PHP致命错误:未捕获的React\Socket\ConnectionException:无法绑定到tcp://0.0.0.0:8080:/home/admin/web/ec3-193-123-96.eu-central-1中已在使用的地址。 compute.amazonaws.com/public_html/application/libraries/vendor/react/socket/src/Server.php:29
堆栈跟踪:
#0 / home / admin / web / ec3-193-123 -96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php(70):React\Socket\Server-> listen(8080,'0.0.0.0')
#1 / home / admin / we b / ec3-193-123-96.eu-central-1.compute.amazonaws.com / public_html / application / libraries / server / websocket_server.php(121):Ratchet\Server\IoServer :: factory(Object( Ratchet\Http\HttpServer),8080)
#2 {main}
放在/home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws中.com / public_html / application / libraries / vendor / react / socket / src / Server.php在第29行
ubuntu @ ec3-193-123-96:/ home / admin / web / ec3-193-123- 96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server

所以我尝试将端口8080更改为端口8282,它成功了

  ubuntu @ ec3-193-123- 96:/home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server$ php websocket_server.php 

继续运行Shell脚本,打开几个Web浏览器窗口,并打开Javascript控制台或包含以下Javascript的页面:

  var c onn = new WebSocket(’ws://0.0.0.0:8282’); 
conn.onopen = function(e){
console.log(连接已建立!);
};

conn.onmessage = function(e){
console.log(e.data);
};

从浏览器控制台结果:


与'ws://5.160.195.94:8282 /'的WebSocket连接失败:
连接建立错误:net :: ERR_CONNECTION_TIMED_OUT


websocket_server.php

 <?php 
使用Ratchet\Server\IoServer;
使用Ratchet\Http\HttpServer;
使用Ratchet\WebSocket\WsServer;
使用MyApp\Chat;

需要dirname(__ DIR__)。 ‘/vendor/autoload.php’;

$ server = IoServer :: factory(
新的HttpServer(
新的WsServer(
新的Chat()

),
8282
);
$ server-> run();

我什至尝试分配公共IP和私有IP,但结果不好



这是在执行并添加src文件夹 $ composer要求cboden / ratchet之后生成的composer文件 code>



composer.json(在AmazonWebServer上)

  {
autoload:{
psr-4:{
MyApp\\: src
}
},
要求:{
cboden / ratchet: ^ 0.4.1
}
}

composer.json(在本地主机上)

  {
autoload:{
psr-4:{
MyApp\\: src
}
},
要求:{
cboden / ratchet: ^ 0.4.3
}
}

我如何解决/克服从WebSocket连接时的问题?来自托管服务器的域名,例如


  • 在出站选项卡下



  • 解决方案

    当涉及到EC2的连接性问题时,您几乎不需要检查什么即可找到根本原因。




    • SSH进入应用程序正在运行的EC2实例,并确保您可以从EC2实例中访问它。如果可行,则需要解决与网络相关的问题。

    • 如果第1步成功。现在,您已经确定要解决此问题是网络问题,您需要检查以下内容。


      • 检查是否已创建Internet网关并将其连接到您的VPC。

      • 接下来检查您的子网路由表是否具有指向互联网网关的默认路由。检查此链接即可完成此操作和

      • 检查您的子网网络ACL 规则,以查看端口是否未被阻塞

      • 最后,您希望检查显示的实例安全性组。




    如果您需要通过EC2 dns访问,则需要在<一个href = https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Scenario2.html rel = nofollow noreferrer>公共子网并分配一个弹性IP



    如果问题仍然存在,请检查 EC2状态检查通过,或尝试设置新实例。


    As I tried to run the chat app from localhost connected to MySQL database which had been coded with PHP via WebSocket it was successful.

    Also when I tried to run from the PuTTY terminal logged into SSH credentials, it was displaying as Server Started with the port# 8080

    ubuntu@ec3-193-123-96:/home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server$ php websocket_server.php
    PHP Fatal error: Uncaught React\Socket\ConnectionException: Could not bind to tcp://0.0.0.0:8080: Address already in use in /home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/vendor/react/socket/src/Server.php:29
    Stack trace:
    #0 /home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/vendor/cboden/ratchet/src/Ratchet/Server/IoServer.php(70): React\Socket\Server->listen(8080, '0.0.0.0')
    #1 /home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server/websocket_server.php(121): Ratchet\Server\IoServer::factory(Object(Ratchet\Http\HttpServer), 8080)
    #2 {main}
    thrown in /home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/vendor/react/socket/src/Server.php on line 29
    ubuntu@ec3-193-123-96:/home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server$
    

    So I tried to change the port#8080 to port# 8282, it was successful

    ubuntu@ec3-193-123-96:/home/admin/web/ec3-193-123-96.eu-central-1.compute.amazonaws.com/public_html/application/libraries/server$ php websocket_server.php
    

    Keeping the shell script running, open a couple of web browser windows, and open a Javascript console or a page with the following Javascript:

    var conn = new WebSocket('ws://0.0.0.0:8282');
    conn.onopen = function(e) {
        console.log("Connection established!");
    };
    
    conn.onmessage = function(e) {
        console.log(e.data);
    };
    

    From the browser console results:

    WebSocket connection to 'ws://5.160.195.94:8282/' failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

    websocket_server.php

    <?php
    use Ratchet\Server\IoServer;
    use Ratchet\Http\HttpServer;
    use Ratchet\WebSocket\WsServer;
    use MyApp\Chat;
    
    require dirname(__DIR__) . '/vendor/autoload.php';
    
    $server = IoServer::factory(
       new HttpServer(
         new WsServer(
              new Chat()
        )
       ),
       8282
    );
    $server->run();
    

    I even tried to assign Public IP and Private IP, but with no good it resulted in the same old result?

    This was the composer files generated after executing and adding src folder $composer require cboden/ratchet

    composer.json(On AmazonWebServer)

    {
        "autoload": {
            "psr-4": {
                "MyApp\\": "src"
            }
        },
        "require": {
            "cboden/ratchet": "^0.4.1"
        }
    }
    

    composer.json(On localhost)

    {
        "autoload": {
            "psr-4": {
                "MyApp\\": "src"
            }
        },
        "require": {
            "cboden/ratchet": "^0.4.3"
        }
    }
    

    How am I suppose to resolve/overcome while connecting it from the WebSocket especially from the hosted server with the domain name such as http://ec3-193-123-96.eu-central-1.compute.amazonaws.com/

    var conn = new WebSocket('ws://localhost:8282');
    

    From the Security Group

    • Under Inbound tab

    • Under Outbound tab

    解决方案

    When it comes to a connectivity issue with an EC2 there are few things you need to check to find the root cause.

    • SSH into the EC2 instance that the application is running and make sure you can access it from within the EC2 instance. If it works then its a network related issue that we need to solve.
    • If step 1 was successful. You have now identified it is a network issue to solve this you need to check the following.
      • Check if an Internet Gateway is created and attached to your VPC.
      • Next check if your subnets routing table has its default route pointing to the internet gateway. check this link to complete this and the above step.
      • Check your subnets Network ACLs rules to see if ports are not blocked
      • finally, you would want to check your Instances Security group as you have shown.

    If you need access via a EC2 dns you will need to provision your ec2 instance in a public subnet and assign an elastic IP

    If an issue still exists check if the EC2 status checks pass, or try provisioning a new instance.

    这篇关于AWS EC2实例WebSocket连接失败:连接建立错误:net :: ERR_CONNECTION_TIMED_OUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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