Run Websocket with the hosted application

Asked

Viewed 244 times

0

The project is a chat with php, Mysql and AJAX. I am using a Websocket with Ratchet to run in real time. It ran all right on the localhost, but when hosting the project I could no longer open the connection by accessing the websocket url.

<script>websocket = new WebSocket('ws://minhaurl.teste:8080');</script>

Here are the websocket configuration class and server.

<?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 researching a lot about this and I’ve seen that I have to host the server via SSH, but I don’t have experience yet. I would like to receive tips on this or to indicate another way to make my server work, if possible. I thank you in advance.

  • How you set up your server to receive such a connection?

  • I edited the post, are above.

  • You checked if the server port 8080 is open?

  • You can tell me how?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.