棘轮:仍在连接状态
Ratchet: Still Connecting State
我是这个 websocket 的初学者,我正在为我的第一个项目尝试这个 Ratchet..
我已经完成了 http://socketo.me 中的安装教程,方法是在命令提示符下执行此命令
composer require cboden/ratchet
之后,它会自动生成 vendor
文件夹,里面有几个库,主路径上有 composer.json
和 composer.lock
然后我制作了一个 chat.php
文件并从棘轮 git 上的快速示例中复制了代码,即:
<?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();
然后我在命令提示符下执行这个命令:php chat.php
我的客户端仍然有错误说:
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
我的文件夹化(在 XAMPP 上):
客户
htdocs/public/chat/index.php
,其中 common.js
完好无损,其中包含
var conn = new WebSocket('ws://localhost:8080/echo');
conn.onmessage = function(e) { console.log(e.data); };
conn.send('Hello Me!');
服务器
htdocs/public/chatserver/chat.php
htdocs/public/chatserver/vendor/<some libraries>
htdocs/public/chatserver/composer.json
htdocs/public/chatserver/composer.lock
我是不是漏掉了什么?
请这样尝试:
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!');
};
您应该可以在连接打开时发送。好像是你在没有建立连接之前试一下。
我遇到了同样的问题并用 Troubleshooting A1 修复了它:
You missed a step。我确实在我的服务器中错过了这部分 php
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);
我是这个 websocket 的初学者,我正在为我的第一个项目尝试这个 Ratchet..
我已经完成了 http://socketo.me 中的安装教程,方法是在命令提示符下执行此命令
composer require cboden/ratchet
之后,它会自动生成 vendor
文件夹,里面有几个库,主路径上有 composer.json
和 composer.lock
然后我制作了一个 chat.php
文件并从棘轮 git 上的快速示例中复制了代码,即:
<?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();
然后我在命令提示符下执行这个命令:php chat.php
我的客户端仍然有错误说:
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
我的文件夹化(在 XAMPP 上):
客户
htdocs/public/chat/index.php
,其中 common.js
完好无损,其中包含
var conn = new WebSocket('ws://localhost:8080/echo');
conn.onmessage = function(e) { console.log(e.data); };
conn.send('Hello Me!');
服务器
htdocs/public/chatserver/chat.php
htdocs/public/chatserver/vendor/<some libraries>
htdocs/public/chatserver/composer.json
htdocs/public/chatserver/composer.lock
我是不是漏掉了什么?
请这样尝试:
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!');
};
您应该可以在连接打开时发送。好像是你在没有建立连接之前试一下。
我遇到了同样的问题并用 Troubleshooting A1 修复了它: You missed a step。我确实在我的服务器中错过了这部分 php
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8080
);