向Ratchet PHP Websocket Server的客户端发送消息

时间:2019-02-04 18:51:03

标签: php websocket ratchet

我正在使用此服务器脚本创建Websocket服务器(server.php)。效果很好。

<?php

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\WebSocket\WsServerInterface;

require 'vendor/autoload.php';
require_once 'db.php';

class MyClass implements MessageComponentInterface, WsServerInterface {

    protected $clients;

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

    public function onOpen(ConnectionInterface $conn) {

        $this->clients->attach($conn);

        $name = str_replace('/', '', $conn->httpRequest->getUri()->getPath());

        $resourceId = $conn->resourceId;

        $stmt = $db->prepare("INSERT INTO clients (id, resourceId, name) VALUES (null, :resourceId, :name)");

        $stmt->bindParam(':resourceId', $resourceId, PDO::PARAM_STR);

        $stmt->bindParam(':name', $name, PDO::PARAM_STR);

        $stmt->execute();

        ConnectDB::closeConnection($db);

        echo "Connected (" . $resourceId . ")\n";

    }

    public function onMessage(ConnectionInterface $from, $msg) {

        $name = str_replace('/', '', $conn->httpRequest->getUri()->getPath());

        $request = json_decode($msg);

        if (json_last_error() === JSON_ERROR_NONE) {

            require_once 'process.php';

            $response = processRequest($msg);

            if ($response !== false) {

                $from->send($response);

            }

        } else {

            $from->send('JSON error');
            $ActionType = 'Connect';

        }

            echo "Incoming message - " $name "\n";

    }

    public function onClose(ConnectionInterface $conn) {

        $this->clients->detach($conn);

        echo "Closed\n";

    }

    public function onError(ConnectionInterface $conn, \Exception $e) {

        echo "Error: " . $e->getMessage() . "\n";

        $conn->close();

    }

}

$server = IoServer::factory(
    new HttpServer(
        new WsServer(
            new MyClass()
        )
    ),
    8080
);

$server->run();

我正在将$resourceId = $conn->resourceId;的ID保存到数据库中。

我想做的是拥有另一个php脚本,当前test.php可以通过Ratchet向客户端发送消息。

这里是test.php

<?php

use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Ratchet\WebSocket\WsServerInterface;

require 'vendor/autoload.php';
require_once 'db.php';

$to = 119; // hard-coded id, not using database yet

$msg = json_encode(array("test" => "test"));

class Sender implements MessageComponentInterface, ConnectionInterface {

    public function send(ConnectionInterface $to, $msg) {
        $client = $this->clients[$to];
        $client->send($msg);
    }

}

Sender::send($to, $msg);

错误:

Fatal error: Declaration of Sender::send() must be compatible with Ratchet\ConnectionInterface::send($data) in test.php on line 20

这不起作用,我不知道这是否是正确的方法。我不想回复传入的消息(当前有效),而是最终从test.php处的表单发送消息。

Ratchet示例(Hello world)基本上使用服务器在客户端之间中继消息。就我而言,我只希望服务器与各个客户端进行来回通信。客户之间永远不会互相交谈。另外,test.php处的表单不会创建客户,也不会期望得到答案。

有人可以指引我正确的方向直接向客户发送消息吗?

0 个答案:

没有答案