0

I am using cboden/ratchet package of websocket on laravel. This is most frustrating thing on earth.

How do I even get this connected ?

It keeps showing me this error : Connection closed before receiving a handshake response

enter image description here

// This is my test command to start the server

<?php
    
    namespace App\Console\Commands;
    
    use Illuminate\Console\Command;
    use Ratchet\Server\IoServer;
    use Ratchet\Http\HttpServer;
    use Ratchet\WebSocket\WsServer;
    use App\Classes\Socket\ChatSocket as Socket;
    
    class TestCommand extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'ratched:serve';
    
        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Just For Testing';
    
        /**
         * Create a new command instance.
         *
         * @return void
         */
        public function __construct()
        {
            parent::__construct();
        }
    
        /**
         * Execute the console command.
         *
         * @return int
         */
        public function handle()
        {
            $server = IoServer::factory(
                new HttpServer(
                    new WsServer(
                        new Socket()
                    )
                ),
                8090 
            );
            $server->run();
        }
    }

Then this is my client side

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <h2>Testing RATCHET</h2>
    <div class="container">
        <div class="content">
            <div class="title">Laravel</div>
            <button onclick="send()">Submit</button>
        </div>
    </div>
    <script>
        var ws = new WebSocket("ws://localhost:8090/");
        ws.onopen = function () {
            // Websocket is connected
            console.log("Websocket connected");
            ws.send("Hello World");
            console.log("Message sent");
        };
        ws.onmessage = function (event) {
            // Message received
            console.log("Message received = " + event.data);
        };
        ws.onclose = function () {
            // websocket is closed.
            console.log("Connection closed");
        };
    </script>
</body>
</html>

//Logic for Connection

<?php

namespace App\Classes\Socket;
// use App\Classes\Socket\Base\BaseSocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class ChatSocket 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();
    }
}

I have tried changing the port, there is no luck

Thank you.

desh
  • 627
  • 1
  • 7
  • 19
  • If you are using Microsoft Windows, have you opened a command window and tried to start the Websocket server? On Laravel you can type the command to start the websocket server in the command window (or shell) using the artisan tool: C:\app\myWebApp\>php artisan ratched:serve If there is any error on your ws server, it will be thrown there. In your ChatSocket class you can add php echo sentences to inspect/debug your class members/properties. Once the WS Socket is up and running it will be easy to connect a web browser with javascript as you have done. – jgarcias Aug 18 '21 at 09:28
  • In your ChatSocket class, you can add a: `echo "Websocket server started..."` in your __constructor method, just to let you know it is working. – jgarcias Aug 18 '21 at 09:33

0 Answers0