我目前正在使用Ratchet PHP Websockets示例代码(http://socketo.me/docs/hello-world),当我尝试运行Websocket时,它不输出任何内容,但确实表明它是“实时的”。
在我的网站上,我使用与示例相同的JS代码,但使用wss://
的原因是我的网站正在通过Cloudflare的SSL。现在我的问题是,当我尝试连接到Websocket时,在我的Chrome开发人员工具中,它一直持续显示挂起状态,并且如果我关闭websocket将会终止。
我找不到调试它发生的事情的方法,我正在使用Cloudflare允许使用的端口(即:8443),并且我正在计划Cloudflare支持Websocket。但是我似乎找不到关于通过cloudflare使用Websockets的任何答案,而且当我打开error_reporting
时,它似乎也没有输出任何东西。
Cloudflare有用吗?当我输入php runwebsocket.php
时,是否应该输出什么?
当我将此脚本用于Websockets(https://github.com/ghedipunk/PHP-Websockets)时,如果客户端已连接到套接字,它将输出,但仍然会给我一个同样的问题,即它实际上并未完全连接到套接字。
编辑:当我尝试连接到套接字时,进入Websocket Echo测试站点时,它也不输出任何内容。
**编辑#2:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require('vendor/autoload.php'); // composer require cboden/ratchet
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use AppSocket\Notifications;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Notifications()
)
),
8443
);
$server->run();
AppSocket\Notifications
的代码
<?php
namespace AppSocket;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Notifications implements MessageComponentInterface {
protected $clients;
public function __construct() {
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
$this->clients->attach($conn);
echo "New connection! ({$conn->resourceId})\n";
}
public function onMessage(ConnectionInterface $from, $msg) {
foreach ($this->clients as $client) {
if ($from !== $client) {
$client->send($msg);
}
}
echo "Yay";
}
public function onClose(ConnectionInterface $conn) {
$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();
}
}