doufei5315 2019-02-04 18:51
浏览 148

向Ratchet PHP Websocket服务器的客户端发送消息

I am using this server script to create a Websocket server (server.php). This works fine.

<?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 . ")
";

    }

    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 "
";

    }

    public function onClose(ConnectionInterface $conn) {

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

        echo "Closed
";

    }

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

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

        $conn->close();

    }

}

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

$server->run();

I am saving the ID of $resourceId = $conn->resourceId; to a database.

What I would like to do is have another php script, currently test.php that could send messages to the clients via Ratchet.

Here is 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);

Error:

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

This does not work and I do not know if this is the correct approach. I don't want to reply to an incoming message (that currently works), rather ultimately send a message from a form at test.php.

The Ratchet examples (Hello world) basically use the server to relay the messages between clients. In my case, I only want the server to communicate back and forth to individual clients. Clients never talk to each other. Also, the form at test.php does not create a client, nor does it expect an answer.

Can someone steer me in the right direction to send messages directly to clients?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug
    • ¥15 关于#python#的问题:自动化测试