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

0

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事件驱动才能用,还是说不需要开启就可以这么使用?
 
我刚接触这方面的知识,还有很多地方不明白,麻烦大大指点下。。。不胜感激!

已邀请:

wenken

赞同来自: varphp 郑木又

贴上代码,帮助其他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();

walkor

赞同来自: lobtao

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

caiqy

赞同来自:

mark..........

varphp - php真强大

赞同来自:

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

要回复问题请先登录注册