3

I try to make a simple chat page on my symfony project by WebSocket. First I used React-php library, it perfectly works on terminal but when I try to connect it to browser I faced this error on chrome:

Uncaught Error: INVALID_STATE_ERR: DOM Exception 11

and on firefox

Firefox can't establish a connection to the server at ws://localhost:8000/.

Next I used Ratchet library and follow the tutorial but still the same problem, work on terminal, error on browser. I use telnet localhost 8000 on terminal and javascript on browser is

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

the server code for React

require __DIR__.'/../vendor/autoload.php';
$loop = React\EventLoop\Factory::create();
$socket = new React\Socket\Server($loop);
$conns = new \SplObjectStorage();
$socket->on('connection', function ($conn) use ($conns) {
    $conns->attach($conn);
    $conn->on('data', function ($data) use ($conns, $conn) {
        foreach ($conns as $current) {
            if ($conn === $current) {
                continue;
            }
            $current->write($data);
        }
    });
    $conn->on('end', function () use ($conns, $conn) {
        $conns->detach($conn);
    });
});
$socket->listen(8000);
$loop->run();

and server code for Ratchet

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/chat.php';
use Ratchet\Server\IoServer;
use Ratchet\WebSocket\WsServer;
use MyApp\Chat;
$server = IoServer::factory( new WsServer( new Chat() ), 8000);
$server->run();

Another thing is client page url is localhost/X/chat and server localhost/X/server, I tried ws://localhost:8000/X/server but still doesn't work

igorw
  • 27,759
  • 5
  • 78
  • 90
Erfan
  • 1,132
  • 15
  • 21

3 Answers3

1

You cannot call send before the connection is established. You need to bind to the onopen event:

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

This should fix your issue.

igorw
  • 27,759
  • 5
  • 78
  • 90
  • If connection be right, `conn.send()` is usable. However I tried your code but still doesn't work. – Erfan Oct 31 '12 at 08:42
  • What do you mean by "Doesn't work"? Do you get a new error? If so, I answered your question. – igorw Oct 31 '12 at 11:17
1

The problem was in cURL ext. solved.

Erfan
  • 1,132
  • 15
  • 21
  • how did you solved it? what exactly did you to get it works? – Adrian Covaci Apr 24 '20 at 06:59
  • after 7 years i have no clue what have i done :) based on comment, maybe i have installed curl extension for php – Erfan Apr 24 '20 at 07:48
  • I imagined that you don't remember, but I said to try anyway .. I'm desperate that my websocket doesn't connect and it's not a problem from cURL from what I notice .. – Adrian Covaci Apr 24 '20 at 14:39
0

As I understand it, React\Socket opens a plaintext TCP / IP connection.

You can use any plaintext TCP/IP client to connect to your server, such as telnet or any other TCP/IP library.

If you want to use a WebSocket client, such as the one provided by your browser, you will have to use a WebSocket server. Ratchet is a WebSocket server that builds on top of this project.

Look this https://github.com/reactphp/socket/issues/142