workman配置

Linux x

我用的框架是thinkphp5.1

workman运用的json框架 :

composer require workerman/workerman
"workerman/gateway-worker" : "^3.0",

在根目录创建运行的start.php

<?php
/**
 * run with command 
 * php start.php start
 */

    ini_set('display_errors', 'on');
    use Workerman\Worker;

    if(strpos(strtolower(PHP_OS), 'win') === 0)
    {
        exit("start.php not support windows, please use start_for_win.bat\n");
    }

    // 检查扩展
    if(!extension_loaded('pcntl'))
    {
        exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
    }

    if(!extension_loaded('posix'))
    {
        exit("Please install posix extension. See http://doc3.workerman.net/appendices/install-extension.html\n");
    }

    // 标记是全局启动
    define('GLOBAL_START', 1);

require_once __DIR__ . '/vendor/autoload.php';

    // 加载所有Applications/*/start.php,以便启动所有服务
    //application/workman/controller/*/start*.php' 这个是我的workman的一个模块
    foreach(glob(__DIR__.'/application/workman/controller/*/start*.php') as $start_file)
    {
        require_once $start_file;
    }
    // 运行所有服务
    $work = new Worker();
    $work ::runAll();



具体的文档可以看文档
http://doc.workerman.net/appendices/install-extension.html

在workman/controller中创建 Events.php start_businessworker.php  start_gateway.php start_register.php
    Events是负责业务逻辑模块,负责整个异步逻辑的处理,监听,发送数据 
根据相关的配置进行填写
 

比较重要的一点:

使用这个脚本检查是否有禁用函数。命令行运行curl -Ss http://www.workerman.net/check.php | php

很多人可能会出现这个错误:

Class 'GatewayWorker\BusinessWorker' not found start_businessworker.php:28        

那么可以将你的start.php文件放入项目根目录,

然后 foreach(glob(__DIR__.'/你的模块,创建的 Events.php start_businessworker.php  start_gateway.php start_register.php 所在目录/*/start*.php') as $start_file)

然后以

php start.php start尝试启动,查看是否正常启动

最后ctrl+c取消,以php start.php start -d 保持运行这个处理


最后的一个配置:

composer require workerman/gatewayclient
/**
 * === 指定registerAddress表明与哪个GatewayWorker(集群)通讯。===
 * GatewayWorker里用Register服务来区分集群,即一个GatewayWorker(集群)只有一个Register服务,
 * GatewayClient要与之通讯必须知道这个Register服务地址才能通讯,这个地址格式为 ip:端口 ,
 * 其中ip为Register服务运行的ip(如果GatewayWorker是单机部署则ip就是运行GatewayWorker的服务器ip),
 * 端口是对应ip的服务器上start_register.php文件中监听的端口,也就是GatewayWorker启动时看到的Register的端口。
 * GatewayClient要想推送数据给客户端,必须知道客户端位于哪个GatewayWorker(集群),
 * 然后去连这个GatewayWorker(集群)Register服务的 ip:端口,才能与对应GatewayWorker(集群)通讯。
 * 这个 ip:端口 在GatewayClient一侧使用 Gateway::$registerAddress 来指定。
 * 
 * === 如果GatewayClient和GatewayWorker不在同一台服务器需要以下步骤 ===
 * 1、需要设置start_gateway.php中的lanIp为实际的本机内网ip(如不在一个局域网也可以设置成外网ip),设置完后要重启GatewayWorker
 * 2、GatewayClient这里的Gateway::$registerAddress的ip填写填写上面步骤1lanIp所指定的ip,端口
 * 3、需要开启GatewayWorker所在服务器的防火墙,让以下端口可以被GatewayClient所在服务器访问,
 *    端口包括Rgister服务的端口以及start_gateway.php中lanIp与startPort指定的几个端口
 *
 * === 如果GatewayClient和GatewayWorker在同一台服务器 ===
 * GatewayClient和Register服务都在一台服务器上,ip填写127.0.0.1及即可,无需其它设置。
 **/

Gateway::$registerAddress = '127.0.0.1:1236';

// GatewayClient支持GatewayWorker中的所有接口(Gateway::closeCurrentClient Gateway::sendToCurrentClient除外)
Gateway::sendToAll($data);
Gateway::sendToClient($client_id, $data);
Gateway::closeClient($client_id);
Gateway::isOnline($client_id);
Gateway::bindUid($client_id, $uid);
Gateway::isUidOnline($uid);
Gateway::getClientIdByUid($client_id);
Gateway::unbindUid($client_id, $uid);
Gateway::sendToUid($uid, $dat);
Gateway::joinGroup($client_id, $group);
Gateway::sendToGroup($group, $data);
Gateway::leaveGroup($client_id, $group);
Gateway::getClientCountByGroup($group);
Gateway::getClientSessionsByGroup($group);
Gateway::getAllClientCount();
Gateway::getAllClientSessions();
Gateway::setSession($client_id, $session);
Gateway::updateSession($client_id, $session);
Gateway::getSession($client_id);
```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值