Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

amazon web services - An Issue with an AWS EC2 instance WebSocket connection failed: Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT

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 ReactSocketConnectionException: 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): ReactSocketServer->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): RatchetServerIoServer::factory(Object(RatchetHttpHttpServer), 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 RatchetServerIoServer;
use RatchetHttpHttpServer;
use RatchetWebSocketWsServer;
use MyAppChat;

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 enter image description here

  • Under Outbound tab enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...