ad3n/ratchet-bundle

Symfony 的 WebSocket 扩展包

安装: 73

依赖者: 0

建议者: 0

安全性: 0

星标: 10

关注者: 2

分支: 0

公开问题: 2

类型:symfony-bundle

v0.6 2020-06-09 06:22 UTC

README

安装

composer req ad3n/ratchet-bundle

激活 Bundle(可选)

在您的 AppKernel.php 中添加 new Ihsan\RatchetBundle\IhsanRatchetBundle()

创建您的消息处理器

<?php

namespace YourBundle\Message\Processor;

use Ihsan\RatchetBundle\Message\Message;
use Ihsan\RatchetBundle\Processor\MessageProcessorInterface;
use Ratchet\ConnectionInterface;

/**
 * @author Muhamad Surya Iksanudin <surya.kejawen@gmail.com>
 */
class MyOwnMessageProcessor implements MessageProcessorInterface
{
    /**
     * @param ConnectionInterface $connection
     * @param Message $message
     *
     * @return Message
     */
    public function process(ConnectionInterface $connection, Message $message): Message
    {
        //$message is message send by the client
        //Your Own Logic
        
        return $message;//return original message or
        
        //return new Message(json_encode(['message' => 'Hello', 'more_data_to_expose' => $data]));
    }
}

创建服务

您必须添加标签 ihsan_ratchet.message_processor 来注册您自己的消息处理器

    YourBundle\Message\Processor\MyOwnMessageProcessor:
        tags:
            - { name: 'ihsan_ratchet.message_processor' }

添加配置键

WEB_SOCKET_PORT=7777

或者

ihsan_ratchet:
    web_socket_port: 7777

启动服务器

运行 php bin/console ihsan:server:start

完整文档

Socketo.me