如何在workerman和gateway中使用swoole协程?

wenken

https://wenda.workerman.net/question/3128 我看了这个问题,但是大大只给出了如何开启swoole事件驱动,如何在workerman中使用缺没给出实例。
不知道swoole那些类或函数能够使用
难道是直接在workerman中像这样写代码就行吗?

function send($phone, $message){
    go(function () use($phone, $message){
        $sms = new Sms();
        $sms->send($phone, $message);
    });
    return true;
}

如何是这么用的话,是必须开启swoole事件驱动才能用,还是说不需要开启就可以这么使用?
 
我刚接触这方面的知识,还有很多地方不明白,麻烦大大指点下。。。不胜感激!

9808 5 8
5个回答

walkor 打赏

Worker::runAll(); 运行前设置
Worker::$eventLoopClass = Workerman\Events\Swoole:class
 开swoole事件驱动后就可以用swoole的类或者函数了

  • wenken 2018-12-20

    谢谢大大,按照你的方式测试通过了,感谢!

wenken

贴上代码,帮助其他phper
服务端:

<?php

use Workerman\Worker;

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

$http_worker = new Worker("tcp://0.0.0.0:2345");

// 启动4个进程对外提供服务
$http_worker->count = 4;

$http_worker->onMessage = function ($connection, $data) {
    for ($i = 0; $i < 10; $i++) {
        go(function () use ($connection, $i) {
            Co::sleep(1);
            $connection->send(microtime(true) . ": hello" . $i);
        });
    };
    $connection->send($data);
};

// 启用swoole的事件驱动
Worker::$eventLoopClass = 'Workerman\Events\Swoole';

Worker::runAll();

客户端:

<?php

use \Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;
require_once __DIR__ . '/../vendor/autoload.php';

$task = new Worker();
$task->onWorkerStart = function($task)
{
    $connection_to_baidu = new AsyncTcpConnection('tcp://127.0.0.1:2345');
    $connection_to_baidu->onConnect = function($connection_to_baidu)
    {
        $connection_to_baidu->send("ping");
    };
    $connection_to_baidu->onMessage = function($connection_to_baidu, $http_buffer)
    {
        echo $http_buffer."\n";
    };
    $connection_to_baidu->onClose = function($connection_to_baidu)
    {
        echo "connection closed\n";
    };
    $connection_to_baidu->onError = function($connection_to_baidu, $code, $msg)
    {
        echo "Error code:$code msg:$msg\n";
    };
    $connection_to_baidu->connect();
};

// 运行worker
Worker::runAll();
caiqy

mark..........

  • 暂无评论
varphp

Workerman\Events\Swoole 这个是怎么来的 不是很了解

  • phpcreeper 2019-08-09

    1、swoole不仅仅是作为一个异步网络通信引擎存在的PHP扩展,同时也是一个异步事件驱动
    库。
    2、Workerman\Events\Swoole 是对swoole事件驱动的封调用装实现,其源代码在workerman源码中就能找到。

  • kenny534 2019-10-10

    Workerman\Events\Swoole.php
    里的 use Swoole\Event; use Swoole\Timer; 这两个东东在哪里?

kkiissooo

Worker::$eventLoopClass = 'Workerman\Events\Swoole';

启用swoole事件驱动后,用php start.php stop停止workerman,会报错:

Fatal error: Uncaught Swoole\ExitException: swoole exit in /Users/....../GatewayWorker/vendor/workerman/workerman/Worker.php:1808
Stack trace:
#0 /Users/....../GatewayWorker/vendor/workerman/workerman/Worker.php(1120): Workerman\Worker::stopAll()
#1 [internal function]: Workerman\Worker::signalHandler(2, Array)
#2 /Users/....../GatewayWorker/vendor/workerman/workerman/Events/Swoole.php(52): pcntl_signal_dispatch()
#3 {main}
  thrown in /Users/....../GatewayWorker/vendor/workerman/workerman/Worker.php on line 1808

请问下怎么解决呢

年代过于久远,无法发表回答
×
🔝