The project is a chat with php, Mysql and AJAX. I'm using a WebSocket with Ratchet to run in real time. It ran all right on localhost, but when hosting the project I could not open the connection again when I accessed the websocket url.
<script>websocket = new WebSocket('ws://minhaurl.teste:8080');</script>
Here are the websocket and server configuration class.
<?php
namespace ChatApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Chat implements MessageComponentInterface
{
protected $clients;
public function __construct(){
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn){
$this->clients->attach($conn);
echo "New Connection: ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg){
$numConns = count($this->clients) - 1;
$printConn = ($numConns <> 1) ? 'conexões' : 'conexão';
echo "Mensagem \"$msg\" enviada de ($from->resourceId), para $numConns ".$printConn."\n";
foreach($this->clients as $client){
if($client != $from) {
$client->send($msg);
}
}
}
public function onClose(ConnectionInterface $conn){
$this->clients->detach($conn);
echo "Connection ({$conn->resourceId}) has disconnected\n";
}
public function onError(ConnectionInterface $conn, \Exception $e){
echo "Error Occurred: {$e->getMessage()}\n";
$conn->close();
}
}
?>
<?php
set_time_limit(0);
use Ratchet\Server\IOServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use ChatApp\Chat;
require_once dirname(__DIR__).'/vendor/autoload.php';
require_once dirname(__DIR__).'/src/ChatApp/Chat.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
$server->run();
?>
I've been doing a lot of research on this, and I've already seen that I have to host the server via SSH, but I still have no experience with it. I would like to get tips on this or tell me another way to make my server work, if possible. Thanks in advance.