Ratchet: Still Connecting State
Solution 1
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.
Solution 2
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
);
Comments
-
Roi almost 2 years
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 acomposer.json
andcomposer.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 acommon.js
intact that containsvar 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?