AWS EC2实例WebSocket连接失败:连接建立错误:net :: ERR_CONNECTION_TIMED_OUT

时间:2018-10-17 06:35:52

标签: amazon-web-services amazon-ec2 websocket ratchet phpwebsocket

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

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

public int  bestAverageCalculator(String[][] scores) {

    // This HashMap maps student name to their list of scores
    Map<String,List<Integer>> scoreMap = new HashMap<String,List<Integer>>();
    for(String[] score:scores) {
        String name = score[0];
        int currentScore =Integer.parseInt(score[1]);

        if(scoreMap.containsKey(name)) {
            List<Integer> scoreList = scoreMap.get(name);
            scoreList.add(currentScore);
            scoreMap.put(name, scoreList);
        }
        else {
            List<Integer> scoreList = new ArrayList<Integer>();
            scoreList.add(currentScore);
            scoreMap.put(name, scoreList);
        }
    }
    //scoreMap will be {Charles=[63], Bob=[85], Mark=[100, 34]}
    //After Map is formed i am iterating though all the values and finding the best average as below
    int bestAverage = 0;
    for(List<Integer> value:scoreMap.values()) {
        int sum = 0;
        int count = 0;
        for(int i:value) {
            sum+=i;
            count++;
        }
        int average = (int)Math.floor(sum/count);
        if(average>bestAverage)
            bestAverage = average;
    }

    return bestAverage;// returns 85

}

所以我尝试将端口8080更改为端口8282,这是成功的

List<Integer>

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

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$

从浏览器控制台结果:

  

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

websocket_server.php

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

我什至尝试分配了公共IP和私有IP,但是效果不佳,结果还是一样?

这是在执行并添加src文件夹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); };

之后生成的作曲家文件

composer.json(在AmazonWebServer上)

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

composer.json(在本地主机上)

$composer require cboden/ratchet

在从WebSocket(尤其是从托管服务器使用域名)连接WebSocket时,我该如何解决/克服问题? http://ec3-193-123-96.eu-central-1.compute.amazonaws.com/

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

来自安全组

  • “入站”标签下 enter image description here

  • “出站”选项卡下 enter image description here

1 个答案:

答案 0 :(得分:0)

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

  • SSH进入应用程序正在运行的EC2实例,并确保您可以从EC2实例中访问它。如果可行,则需要解决与网络相关的问题。
  • 如果步骤1成功。现在,您已经确定要解决此问题是网络问题,您需要检查以下内容。
    • 检查是否已创建Internet网关并将其连接到您的VPC。
    • 接下来,检查您的子网路由表是否具有指向Internet网关的默认路由。选中此link,以完成此步骤和上述步骤。
    • 检查您的子网Network ACLs规则,以查看端口是否未被阻塞
    • 最后,您将要检查显示的Instances Security组。

如果您需要通过EC2 dns进行访问,则需要在public subnet中配置ec2实例并分配一个elastic IP

如果问题仍然存在,请检查EC2 status checks是否通过,或尝试置备新实例。