当我尝试从连接到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"
}
}
来自安全组
答案 0 :(得分:0)
当涉及到EC2的连接性问题时,您几乎不需要进行任何检查即可找到根本原因。
如果您需要通过EC2 dns进行访问,则需要在public subnet中配置ec2实例并分配一个elastic IP
如果问题仍然存在,请检查EC2 status checks是否通过,或尝试置备新实例。