【问题标题】:Push message to websockets with ratchet php and without ZeroMQ使用棘轮 php 将消息推送到 websockets 而没有 ZeroMQ
【发布时间】:2018-08-17 11:41:31
【问题描述】:

我尝试用棘轮和棘爪制作 websocket 服务器。 我用 ZeroMQ http://socketo.me/docs/push 阅读了文档棘轮 但不能用 Pawl https://github.com/ratchetphp/Pawl 运行它

我创建客户端:

<script>
    var ws = new WebSocket('ws://site.ll:8008/?user=tester01');
    ws.onmessage = function(evt) { console.log(evt.data); };
</script>

创建工人:

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Pusher.php';

$loop   = React\EventLoop\Factory::create();
$pusher = new Pusher;

// Set up our WebSocket server for clients wanting real-time updates
$webSock = new React\Socket\Server('0.0.0.0:8008', $loop);

$webServer = new Ratchet\Server\IoServer(
    new Ratchet\Http\HttpServer(
        new Ratchet\WebSocket\WsServer(
            new Ratchet\Wamp\WampServer(
                $pusher
            )
        )
    ),
    $webSock
);

$loop->run();

Pusher 类的用法与教程http://socketo.me/docs/push#zeromq_messages中的一样

使用post.php通过ws发送消息:

$localsocket = 'tcp://127.0.0.1:1235';
$user = 'tester01';
$message = 'test';

$instance = stream_socket_client($localsocket);
fwrite($instance, json_encode(['user' => $user, 'message' => $message])  . "\n");

但是还是不明白如何在没有 ZeroMQ 的情况下创建 tcp 服务器,比如这部分:

$context = new React\ZMQ\Context($loop);
$pull = $context->getSocket(ZMQ::SOCKET_PULL);
$pull->bind('tcp://127.0.0.1:1235');
$pull->on('message', array($pusher, 'onBlogEntry'));

【问题讨论】:

    标签: php websocket ratchet reactphp


    【解决方案1】:

    如果你不想使用 ZeroMQ,你可以使用与 Ratchet 相同的 React 机制。

    require __DIR__ . '/vendor/autoload.php';
    require __DIR__ . '/Pusher.php';
    
    use React\EventLoop\Factory;
    use React\Socket\ConnectionInterface;
    use React\Socket\Server;
    
    $loop   = Factory::create();
    $pusher = new Pusher;
    
    // Set up our WebSocket server for clients wanting real-time updates
    $webSock = new React\Socket\Server('0.0.0.0:8080', $loop);
    $webServer = new Ratchet\Server\IoServer(
        new Ratchet\Http\HttpServer(
            new Ratchet\WebSocket\WsServer(
                new Ratchet\Wamp\WampServer(
                    $pusher
                )
            )
        ),
        $webSock
    );
    
    // Set up an incoming TCP socket to receive the messages to push
    $inSocket = new Server('127.0.0.1:5555', $loop);
    $inSocket->on('connection', function (ConnectionInterface $conn) use ($pusher) {
        // New data arrives at the socket
        $conn->on('data', function ($data) use ($conn, $pusher) {
            // Push the new blog entry update
            $pusher->onBlogEntry($data);
            // Close the incoming connection when the message is consumed
            $conn->close();
        });
    });
    
    $loop->run();
    

    【讨论】:

      【解决方案2】:

      接下来使用 Pawl 并在 github 上加载: https://github.com/Shkarbatov/WebSocketPHPRatchet

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-15
        • 1970-01-01
        • 2013-08-19
        • 2021-07-23
        • 1970-01-01
        • 1970-01-01
        • 2020-07-16
        • 2022-11-20
        相关资源
        最近更新 更多