Ratchet icon indicating copy to clipboard operation
Ratchet copied to clipboard

How to deploy this to Live ubuntu server

Open humanyu opened this issue 3 years ago • 2 comments

Hi everyone, @thrashr888 @pborreli @igorw @joelwurtz @nervo @al0mie @samizdam

I started the server and try to load chat page but showing error in console WebSocket connection to 'ws://myliveserverip:8080/' failed:

what should I do? please help me out

Thanks

humanyu avatar Dec 21 '21 16:12 humanyu

  1. Please don't solicit users when your question has 0 contextual relevance to any of them
  2. There's no where near enough information here to assist. What's the error? Where's your code base? What did you do to generate the error?

cboden avatar Dec 21 '21 17:12 cboden

@cboden getting this error --------------- (index):3 WebSocket connection to 'ws://xx.xx.xx.xx:8080/' failed:

server.php-----------

require DIR . '/vendor/autoload.php'; use Ratchet\Server\IoServer; use Ratchet\Http\HttpServer; use Ratchet\WebSocket\WsServer; include 'chat.php';

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Chat()
        )
    ),
    8080,'0.0.0.0'
);

$server->run();

chat.php---------------

require DIR . '/vendor/autoload.php'; use Ratchet\MessageComponentInterface; use Ratchet\ConnectionInterface;

class Chat implements MessageComponentInterface { protected $clients;

public function __construct() {
    $this->clients = new \SplObjectStorage;
}

public function onOpen(ConnectionInterface $conn) {
    // Store the new connection to send messages to later
    $this->clients->attach($conn);

    echo "New connection! ({$conn->resourceId})\n";
}

public function onMessage(ConnectionInterface $from, $msg) {
    $numRecv = count($this->clients) - 1;
    echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "\n"
        , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');

    foreach ($this->clients as $client) {
        //if ($from !== $client) {
            // The sender is not the receiver, send to each client connected
            $client->send($msg);
        //}
    }
}

public function onClose(ConnectionInterface $conn) {
    // The connection is closed, remove it, as we can no longer send it messages
    $this->clients->detach($conn);

    echo "Connection {$conn->resourceId} has disconnected\n";
}

public function onError(ConnectionInterface $conn, \Exception $e) {
    echo "An error has occurred: {$e->getMessage()}\n";

    $conn->close();
}

}

index.php----------------

var conn = new WebSocket('ws://xx.xx.xx.xx:8080'); conn.onopen = function(e) { console.log("Connection established!"); conn.send('Hello World!'); };

conn.onmessage = function(e) { console.log(e.data); };

humanyu avatar Dec 27 '21 07:12 humanyu