gatewayworker自定义协议

h0r1z0n

看了workerman手册的这个章节
定制通讯协议
http://doc3.workerman.net/protocols/how-protocols.html
以文中MyApp/Protocols/JsonNL.php的实现,
放在gatewayworker中,是不是在GatewayWorker\Applications\下新建Protocols文件夹,然后建立JsonNL.php,这样是不是项目名称变成了Protocols,还是在具体在Applications\xxx项目\下建立Protocols?
GatewayWorker使用workerman自定义协议,应该把自定义的协议文件放在哪个目录呢?谢谢

5941 13 0
13个回答

walkor

在Applications\xxx项目\下建立Protocols

  • 暂无评论
h0r1z0n
----------------------- WORKERMAN -----------------------------
Workerman version:3.2.5          PHP version:5.5.15
------------------------ WORKERS -------------------------------
worker               listen                 processes status
Register             text://0.0.0.0:1238     1        
SolarGateway         JsonNL://0.0.0.0:8282   4        
SolarBusinessWorker  none                    4        
----------------------------------------------------------------
Press Ctrl-C to quit. Start success.

Fatal error: Uncaught exception 'Exception' with message 'class \Protocols\JsonN
L not exist' in D:\workerman\GatewayWorker\Workerman\Worker.php:649
Stack trace:
#0 D:\workerman\GatewayWorker\Workerman\Worker.php(426): Workerman\Worker->liste
n()
#1 D:\workerman\GatewayWorker\Workerman\Worker.php(345): Workerman\Worker::runAl
lWorkers()
#2 D:\workerman\GatewayWorker\Applications\Solar\start_gateway.php(64): Workerma
n\Worker::runAll()
#3 {main}
  thrown in D:\workerman\GatewayWorker\Workerman\Worker.php on line 649
process D:\workerman\GatewayWorker\Applications\Solar\start_gateway.php terminat
ed and try to restart
  • 暂无评论
walkor

请贴目录结构和JsonNL.php内容

  • 暂无评论
h0r1z0n

gatewayworker下载github的最新版本
目录结构

.
├── Applications // 这里是所有开发者应用项目
│   └── Solar      // 自定义项目
│       ├── Event.php // 开发者只需要关注这个文件
│       ├── start_gateway.php // gateway进程启动脚本
│       ├── start_businessworker.php // businessWorker进程启动脚本
│       └── start_register.php // 注册服务启动脚本
│   ├── Protocols 
│   └──JsonNL.php //粘贴自手册
├── GatewayWorker // GatewayWorker框架代码
│   ├── BusinessWorker.php  // BusinessWorker进程实现
│   ├── Gateway.php         // Gateway进程实现
│   ├── Register.php        // 注册服务进程实现
│   ├── Lib
│   │   ├── Context.php     // Gateway与BusinessWorker通信上下文
│   │   ├── DbConnection.php// 一个数据库连接类
│   │   ├── Db.php          // 数据库连接管理类
│   │   └── Gateway.php     // Gateway通信接口类,给Event.php调用
│   └──── Protocols
│         └── GatewayProtocol.php // Gateway与BusinessWorker通信协议
│
├── Workerman // workerman内核目录
│
└── start.php // 全局启动脚本

JsonNL.php代码

namespace Protocols;
class JsonNL
{
    /**
     * 检查包的完整性
     * 如果能够得到包长,则返回包的在buffer中的长度,否则返回0继续等待数据
     * 如果协议有问题,则可以返回false,当前客户端连接会因此断开
     * @param string $buffer
     * @return int
     */
    public static function input($buffer)
    {
        // 获得换行字符"\n"位置
        $pos = strpos($buffer, "\n");
        // 没有换行符,无法得知包长,返回0继续等待数据
        if($pos === false)
        {
            return 0;
        }
        // 有换行符,返回当前包长(包含换行符)
        return $pos+1;
    }

    /**
     * 打包,当向客户端发送数据的时候会自动调用
     * @param string $buffer
     * @return string
     */
    public static function encode($buffer)
    {
        // json序列化,并加上换行符作为请求结束的标记
        return json_encode($buffer)."\n";
    }

    /**
     * 解包,当接收到的数据字节数等于input返回的值(大于0的值)自动调用
     * 并传递给onMessage回调函数的$data参数
     * @param string $buffer
     * @return string
     */
    public static function decode($buffer)
    {
        // 去掉换行,还原成数组
        return json_decode(trim($buffer), true);
    }
}
//windows下测试最后修改了Applications \Solar\start_gateway.php
$gateway = new Gateway("JsonNL://0.0.0.0:8282");
  • 暂无评论
walkor

JsonNL.php 应该放到新建的Protocos文件夹

  • 暂无评论
h0r1z0n

没明白 ,我在Applications 下新建了Protocos,里面保存了JsonNL.php 有什么不对吗?

  • 暂无评论
walkor
├── Applications // 这里是所有开发者应用项目
│ └── Solar // 自定义项目
│ ├── Event.php // 开发者只需要关注这个文件
│ ├── start_gateway.php // gateway进程启动脚本
│ ├── start_businessworker.php // businessWorker进程启动脚本
│ └── start_register.php // 注册服务启动脚本
│ ├── Protocols 
│ └──JsonNL.php //粘贴自手册

你的目录结构看着 JsonNL.php 和 Protocols 目录是平行的,JsonNL.php并没有在Protocols目录下

  • 暂无评论
h0r1z0n

我粘贴上显示的有些问题

├── Applications // 这里是所有开发者应用项目
│ └── Solar // 自定义项目
│          ├── Event.php // 开发者只需要关注这个文件
│          ├── start_gateway.php // gateway进程启动脚本
│          ├── start_businessworker.php // businessWorker进程启动脚本
│          └── start_register.php // 注册服务启动脚本
│ ├── Protocols 
│          └──JsonNL.php //粘贴自手册
  • 暂无评论
walkor

你可以把代码包上来,我给你看下

  • 暂无评论
h0r1z0n

我覆盖了wm,win版本那个 其他没动

  • 暂无评论
walkor

请在Applications\xxx项目\下建立Protocols

  • 暂无评论
h0r1z0n

不好意思犯了一个很低级的错误,谢谢

  • 暂无评论
walkor

不客气

  • 暂无评论
年代过于久远,无法发表回答
🔝