Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ratchet: Still Connecting State

I'm beginner about this websocket and I'm trying this Ratchet for my first project..

I've done the install tutorial in http://socketo.me by executing this command in command prompt

composer require cboden/ratchet

after that, it automatically generates vendor folder with a couple of libaries there and on home path a composer.json and composer.lock

Then I made a chat.php file and copied the code from the quick example on ratchet git which is :

<?php
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;

    // Make sure composer dependencies have been installed
    require __DIR__ . '/vendor/autoload.php';

/**
 * chat.php
 * Send any incoming messages to all connected clients (except sender)
 */
class MyChat implements MessageComponentInterface {
    protected $clients;

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

    public function onOpen(ConnectionInterface $conn) {
        $this->clients->attach($conn);
    }

    public function onMessage(ConnectionInterface $from, $msg) {
        foreach ($this->clients as $client) {
            if ($from != $client) {
                $client->send($msg);
            }
        }
    }

    public function onClose(ConnectionInterface $conn) {
        $this->clients->detach($conn);
    }

    public function onError(ConnectionInterface $conn, \Exception $e) {
        $conn->close();
    }
}

    // Run the server application through the WebSocket protocol on port 8080
    $app = new Ratchet\App('localhost', 8080);
    $app->route('/chat', new MyChat);
    $app->route('/echo', new Ratchet\Server\EchoServer, array('*'));
    $app->run();

Then I execute this command in command prompt: php chat.php

I still having error in my client side saying:

Chrome Uncaught InvalidStateError: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.

Firefox InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable

My folderization (on XAMPP):

Client

htdocs/public/chat/index.php with a common.js intact that contains

var conn = new WebSocket('ws://localhost:8080/echo');
    conn.onmessage = function(e) { console.log(e.data); };
    conn.send('Hello Me!');

Server

htdocs/public/chatserver/chat.php
htdocs/public/chatserver/vendor/<some libraries>
htdocs/public/chatserver/composer.json
htdocs/public/chatserver/composer.lock

Am I missing something?

like image 818
Roi Avatar asked Mar 17 '15 05:03

Roi


2 Answers

please try it like this:

var conn = new WebSocket('ws://localhost:8080/echo');
conn.onmessage = function(e) { console.log(e.data); };
conn.onopen = function(e) {
    console.log("Connection established!");
    conn.send('Hello Me!');
};

you should be able to send when the connection is open. It seems to be the case that you try it before the connection is established.

like image 121
steven Avatar answered Sep 19 '22 05:09

steven


I had same problem and fixed it with Troubleshooting A1: You missed a step. I missed indeed this part in my server php

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new Chat()
        )
    ),
    8080
);
like image 43
vladkras Avatar answered Sep 20 '22 05:09

vladkras