mateusz-wilk / thruway-bundle
Symfony2的WebSocket (WAMP2) 集成
Requires
- php: >=5.4
- jms/serializer-bundle: ~1.0.0
- react/child-process: ~0.4
- symfony/finder: >=2.4
- voryx/thruway: ~0.3
Requires (Dev)
- doctrine/orm: ~2.1
- phpunit/phpunit: ~4.0
- symfony/symfony: 2.*
- symfony/yaml: 2.*
This package is not auto-updated.
Last update: 2024-09-20 18:44:03 UTC
README
这是一个为Thruway提供的Symfony Bundle,Thruway是一个实现WAMP(Web应用消息协议)的PHP库。
注意:该项目正在经历大量变更,因此API将会改变。
使用Composer快速开始
安装Thruway Bundle
$ php composer.phar require "voryx/thruway-bundle":"dev-master"
更新AppKernel.php
$bundles = array( // ... new JMS\SerializerBundle\JMSSerializerBundle(), new Voryx\ThruwayBundle\VoryxThruwayBundle($this), // ... );
配置
#app/config/config.yml voryx_thruway: realm: 'realm1' url: 'ws://127.0.0.1:8081' #The url that the clients will use to connect to the router router: ip: '127.0.0.1' # the ip that the router should start on port: '8080' # public facing port. If authentication is enabled, this port will be protected trusted_port: '8081' # Bypasses all authentication. Use this for trusted clients. # authentication: false # true will load the AuthenticationManager locations: bundles: ["AppBundle"] # files: # - "Acme\\DemoBundle\\Controller\\DemoController" serializer: # allow to set JMS_Serializer parameters - for now only serialize_null serialize_null: true
如果你使用的是内存中的用户提供者,你需要将thruway
添加到安全防火墙中,并设置in_memory_user_provider
。
#app/config/security.yml security: firewalls: thruway: security: false
你也可以使用thruway.resource
标记服务,任何注释都将被拾取
<service id="some.service" class="Acme\Bundle\SomeService"> <tag name="thruway.resource"/> </service>
使用FOSUserBundle通过WampCRA进行身份验证
将密码编码器(对于现有站点来说可能很棘手)更改为master wamp challenge
#app/config/security.yml security: ... encoders: FOS\UserBundle\Model\UserInterface: algorithm: pbkdf2 hash_algorithm: sha256 encode_as_base64: true iterations: 1000 key_length: 32
设置voryx_thruway.user_provider为"fos_user.user_manager"
#app/config/config.yml voryx_thruway: user_provider: 'fos_user.user_manager'
WAMP-CRA服务已经配置好了,我们只需要向它添加一个标签,以便Bundle可以安装它
wamp_cra_auth: class: Thruway\Authentication\WampCraAuthProvider parent: voryx.thruway.wamp.cra.auth.client tags: - { name: thruway.internal_client }
自定义授权管理器
你可以设置自己的授权管理器以检查用户(通过其authid标识)是否有权发布 | 订阅 | 调用 | 注册
创建你的授权管理器服务,实现AuthorizationManagerInterface(请参阅Thruway文档以获取详细信息)
// src/ACME/AppBundle/Security/MyAuthorizationManager.php use Thruway\Authentication\AuthorizationManagerInterface; use Thruway\Message\ActionMessageInterface; use Thruway\Message\SubscribeMessage; use Thruway\Session; class MyAuthorizationManager implements AuthorizationManagerInterface { public function isAuthorizedTo(Session $session, ActionMessageInterface $actionMsg) { // set here the type of Action you want to check // Here it's only Subscribe if ($actionMsg instanceof SubscribeMessage) { // Here your own auth rule $topic = $actionMsg->getTopicName(); /* In this example sub patterns meet the following : * {userID}.{name} * we explode the topic name to get the userID */ $topic = explode('.', $topic); if(is_integer($topic[0])){ // UserID shall meet AuthID, else deny access if($topic[0] != $session->getMetaInfo()["authid"]){ return false; } } } return true; } }
注册你的授权管理器服务
my_authorization_manager: class: ACME\AppBundle\Security\MyAuthorizationManager
将你的服务名称插入到voryx_thruway配置中
#app/config/config.yml voryx_thruway: ... authorization: my_authorization_manager # insert the name of your custom authorizationManager ...
重新启动Thruway服务器;现在它将在发布 | 订阅 | 调用 | 注册时检查授权。记住在尝试订阅主题(或其他任何操作)时捕获错误,因为它现在可能被拒绝,并将以错误的形式返回。
用法
注册RPC
use Voryx\ThruwayBundle\Annotation\Register; /** * * @Register("com.example.add") * */ public function addAction($num1, $num2) { return $num1 + $num2; }
调用RPC
public function call($value) { $client = $this->container->get('thruway.client'); $client->call("com.myapp.add", [2, 3])->then( function ($res) { echo $res[0]; } ); }
订阅
use Voryx\ThruwayBundle\Annotation\Subscribe; /** * * @Subscribe("com.example.subscribe") * */ public function subscribe($value) { echo $value; }
发布
public function publish($value) { $client = $this->container->get('thruway.client'); $client->publish("com.myapp.hello_pubsub", [$value]); }
它使用JMS Serializer,因此可以序列化和反序列化实体
use Voryx\ThruwayBundle\Annotation\Register; /** * * @Register("com.example.addrpc", serializerEnableMaxDepthChecks=true) * */ public function addAction(Post $post) { //Do something to $post return $post; }
启动Thruway进程
你可以启动默认的Thruway工作进程(路由器和客户端工作进程),无需任何额外配置。
$ nohup php app/console thruway:process start &
默认情况下,路由器在ws://127.0.0.1:8080启动
##工作进程
Thruway Bundle将为路由器和每个定义的工作进程启动一个单独的进程。如果你没有定义任何工作进程,所有标记的调用和订阅都将启动在default
工作进程中。
将应用程序拆分成多个工作进程有两种主要方式。
-
在
Register
和Subscribe
注释上使用worker
属性。以下RPC将被添加到posts
工作进程中。use Voryx\ThruwayBundle\Annotation\Register; /** * @Register("com.example.addrpc", serializerEnableMaxDepthChecks=true, worker="posts") */ public function addAction(Post $post)
-
在类上使用
@Worker
注释。以下注释将创建一个名为chat
的工作进程,可以拥有最多5个实例。use Voryx\ThruwayBundle\Annotation\Worker; /** * @Worker("chat", maxProcesses="5") */ class ChatController
如果工作进程以除SIGTERM
之外的任何方式关闭,它将自动重新启动。
##更多命令
#####查看运行进程(工作进程)列表
$ php app/console thruway:process status
#####停止进程,例如default
$ php app/console thruway:process stop default
#####启动进程,例如default
$ php app/console thruway:process start default
JavaScript客户端
对于客户端,你可以使用AutobahnJS或任何其他WAMPv2兼容客户端。
以下是一些示例