php使用swoole实现TCP服务

教程下载地址: 网赚博客http://www.piaodoo.com/创业项目排行榜前十名http://www.piaodoo.com/


这里以在Yii框架下示例

一:swoole配置TCP

'swoole' => [
    // 日志文件路径
    'log_file' => '@console/log/swoole.log',
    // 设置swoole_server错误日志打印的等级,范围是0-5。低于log_level设置的日志信息不会抛出
    'log_level' => 1,
    // 进程的PID存储文件
    'pid_file' => '@console/log/swoole.server.pid',
// HTTP协议配置
'http' => [
    'host' => '0.0.0.0',
    'port' => '8889',

    // 异步任务的工作进程数量
    'task_worker_num' => 4,
],
// TCP协议配置
'tcp' => [
    'host' => '0.0.0.0',
    'port' => '14000',

    // 异步任务的工作进程数量
    'task_worker_num' => 4,

    // 启用TCP-Keepalive死连接检测
    'open_tcp_keepalive' => 1,
    // 单位秒,连接在n秒内没有数据请求,将开始对此连接进行探测
    'tcp_keepidle' => 5 * 60,
    // 探测的次数,超过次数后将close此连接
    'tcp_keepcount' => 3,
    // 探测的间隔时间,单位秒
    'tcp_keepinterval' => 60,

    // 心跳检测,此选项表示每隔多久轮循一次,单位为秒
    'heartbeat_check_interval' => 2 * 60,
    // 心跳检测,连接最大允许空闲的时间
    'heartbeat_idle_time' => 5 * 60,
]

],

二:swoole实现TCP服务基类

<?php
/**
 * @link http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司
 * @license http://www.u-bo.com/license/
 */

namespace console\swoole;

use Yii;
use yii\helpers\Console;
use yii\helpers\ArrayHelper;

/*

  • Swoole Server基类

  • @author wangjian

  • @since 0.1
    /
    abstract class BaseServer
    {
    /
    *

    • @var Swoole\Server
      /
      public $swoole;
      /
      *
    • @var boolean DEBUG
      */
      public $debug = false;

    /**

    • __construct
      */
      public function __construct($httpConfig, $tcpConfig, $config = [])
      {
      h t t p H o s t = A r r a y H e l p e r : : r e m o v e ( httpHost = ArrayHelper::remove( httpHost=ArrayHelper::remove(httpConfig, ‘host’);
      h t t p P o r t = A r r a y H e l p e r : : r e m o v e ( httpPort = ArrayHelper::remove( httpPort=ArrayHelper::remove(httpConfig, ‘port’);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole = new…httpHost, $httpPort);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole-&gt;s…config, $httpConfig));

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole-&gt;o…this, ‘onStart’]);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole-&gt;o…this, ‘onRequest’]);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole-&gt;o…this, ‘onWorkerStart’]);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole-&gt;o…this, ‘onWorkerStop’]);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole-&gt;o…this, ‘onTask’]);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole-&gt;o…this, ‘onTaskFinish’]);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole-&gt;o…this, ‘onShutdown’]);

      t c p H o s t = A r r a y H e l p e r : : r e m o v e ( tcpHost = ArrayHelper::remove( tcpHost=ArrayHelper::remove(tcpConfig, ‘host’);
      t c p P o r t = A r r a y H e l p e r : : r e m o v e ( tcpPort = ArrayHelper::remove( tcpPort=ArrayHelper::remove(tcpConfig, ‘port’);
      $tcpServer = KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;swoole-&gt;l…tcpHost, $tcpPort, SWOOLE_SOCK_TCP);
      KaTeX parse error: Expected 'EOF', got '&' at position 11: tcpServer-&̲gt;set(tcpConfig);
      KaTeX parse error: Expected 'EOF', got '&' at position 11: tcpServer-&̲gt;on('connect'…this, ‘onConnect’]);
      KaTeX parse error: Expected 'EOF', got '&' at position 11: tcpServer-&̲gt;on('receive'…this, ‘onReceive’]);
      KaTeX parse error: Expected 'EOF', got '&' at position 11: tcpServer-&̲gt;on('close', …this, ‘onClose’]);
      }

    /*

    • 启动server
      */
      public function run()
      {
      $this->swoole->start();
      }

    /**

    • Server启动在主进程的主线程时的回调事件处理

    • @param swoole_server $server
      */
      public function onStart(\swoole_server $server)
      {
      $startedAt = $this->beforeExec();

      $this->stdout(“Server Start\n”, Console::FG_GREEN);
      $this->stdout("master_pid: “);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{server->master_pid}\n”, Console::FG_BLUE);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;onStartHandl…server);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;afterExec(startedAt);
      }

    /**

    • 客户端与服务器建立连接后的回调事件处理
    • @param swoole_server $server
    • @param integer $fd
    • @param integer $reactorId
      */
      abstract public function onConnect(\swoole_server $server, int $fd, int $reactorId);

    /**

    • 当服务器收到来自客户端的数据时的回调事件处理
    • @param swoole_server $server
    • @param integer $fd
    • @param integer $reactorId
    • @param string $data
      */
      abstract public function onReceive(\swoole_server $server, int $fd, int $reactorId, string $data);

    /**

    • 当服务器收到来自客户端的HTTP请求时的回调事件处理
    • @param swoole_http_request $request
    • @param swoole_http_response $response
      */
      abstract public function onRequest(\swoole_http_request $request, \swoole_http_response $response);

    /**

    • Worker进程/Task进程启动时发生
    • @param swoole_server $server
    • @param integer $worker_id
      */
      abstract public function onWorkerStart(\swoole_server $server, int $worker_id);

    /**

    • Worker进程/Task进程终止时发生
    • @param swoole_server $server
    • @param integer $worker_id
      */
      abstract public function onWorkerStop(\swoole_server $server, int $worker_id);

    /**

    • 异步任务处理
    • @param swoole_server $server
    • @param integer $taskId
    • @param integer $srcWorkerId
    • @param mixed $data
      */
      abstract public function onTask(\swoole_server $server, int $taskId, int $srcWorkerId, mixed $data);

    /**

    • 异步任务处理完成
    • @param swoole_server $server
    • @param integer $taskId
    • @param mixed $data
      */
      abstract public function onTaskFinish(\swoole_server $server, int $taskId, mixed $data);

    /**

    • 客户端与服务器断开连接后的回调事件处理
    • @param swoole_server $server
    • @param integer $fd
      */
      abstract public function onClose(\swoole_server $server, $fd);

    /**

    • Server正常结束时的回调事件处理

    • @param swoole_server $server
      */
      public function onShutdown(\swoole_server $server)
      {
      $startedAt = $this->beforeExec();

      $this->stdout(“Server Stop\n”, Console::FG_GREEN);
      $this->stdout("master_pid: “);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{server->master_pid}\n”, Console::FG_BLUE);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;onShutdownHa…server);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;afterExec(startedAt);
      }

    /**

    • Server启动在主进程的主线程时的自定义事件处理
    • @param swoole_server $server
      */
      protected function onStartHandle(\swoole_server $server)
      {

    }

    /**

    • Server正常结束时的自定义事件处理
    • @param swoole_server $server
      */
      protected function onShutdownHandle(\swoole_server $server)
      {

    }

    /**

    • 获取请求路由
    • @param swoole_http_request $request
      */
      protected function getRoute(\swoole_http_request KaTeX parse error: Expected '}', got 'EOF' at end of input: … return ltrim(request->server[‘request_uri’], ‘/’);
      }

    /**

    • 获取请求的GET参数
    • @param swoole_http_request $request
      */
      protected function getParams(\swoole_http_request $request)
      {
      return $request->get;
      }

    /**

    • 解析收到的数据
    • @param string d a t a ∗ / p r o t e c t e d f u n c t i o n d e c o d e D a t a ( data */ protected function decodeData( data/protectedfunctiondecodeData(data)
      {
      return json_decode($data, true);
      }

    /**

    • Before Exec
      */
      protected function beforeExec()
      {
      $startedAt = microtime(true);
      $this->stdout(date(‘Y-m-d H:i:s’) . “\n”, Console::FG_YELLOW);
      return $startedAt;
      }

    /**

    • After Exec
      */
      protected function afterExec($startedAt)
      {
      $duration = number_format(round(microtime(true) - $startedAt, 3), 3);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{duration} s\n\n", Console::FG_YELLOW);
      }

    /**

    • Prints a string to STDOUT.
      */
      protected function stdout($string)
      {
      if (Console::streamSupportsAnsiColors(\STDOUT)) {
      a r g s = f u n c g e t a r g s ( ) ; a r r a y s h i f t ( args = func_get_args(); array_shift( args=funcgetargs();arrayshift(args);
      s t r i n g = C o n s o l e : : a n s i F o r m a t ( string = Console::ansiFormat( string=Console::ansiFormat(string, KaTeX parse error: Expected 'EOF', got '}' at position 9: args); }̲ return Consol…string);
      }
      }

三:swoole操作类(继承swoole基类)

<?php
/**
 * @link http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司
 * @license http://www.u-bo.com/license/
 */

namespace console\swoole;

use Yii;
use yii\db\Query;
use yii\helpers\Console;
use yii\helpers\VarDumper;
use apps\sqjc\models\WaterLevel;
use apps\sqjc\models\WaterLevelLog;
use common\models\Bayonet;
use common\models\Device;
use common\models\DeviceCategory;

/**

  • Swoole Server测试类

  • @author wangjian

  • @since 1.0
    /
    class Server extends BaseServer
    {
    /
    *

    • @inheritdoc
      */
      public function onConnect($server, $fd, $reactorId)
      {
      $startedAt = $this->beforeExec();

      $this->stdout(“Connection Open\n”, Console::FG_GREEN);
      $this->stdout("fd: “);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{fd}\n”, Console::FG_BLUE);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;afterExec(startedAt);
      }

    /**

    • @inheritdoc
      */
      public function onReceive($server, $fd, $reactorId, $data)
      {
      $startedAt = $this->beforeExec();

      $this->stdout(“Received Message\n”, Console::FG_GREEN);

      $this->stdout("fd: “);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{fd}\n”, Console::FG_BLUE);

      $this->stdout("data: “);//接收的数据
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{data}\n”, Console::FG_BLUE);

      $result = KaTeX parse error: Expected 'EOF', got '&' at position 8: server-&̲gt;send(fd, ‘回复消息’);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;afterExec(startedAt);
      }

    /**

    • @inheritdoc
      */
      public function onRequest($request, $response)
      {
      $startedAt = $this->beforeExec();

      $this->stdout(“HTTP Request\n”, Console::FG_GREEN);

      $this->stdout("fd: “);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{request->fd}\n”, Console::FG_BLUE);

      $response->status(200);
      $response->end(‘success’);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;afterExec(startedAt);
      }

    /**

    • @inheritdoc
      */
      public function onClose($server, $fd)
      {
      $startedAt = $this->beforeExec();

      $this->stdout(“Connection Close\n”, Console::FG_GREEN);

      $this->stdout("fd: “);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{fd}\n”, Console::FG_BLUE);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;afterExec(startedAt);
      }

    /**

    • @inheritdoc
      */
      public function onTask($server, $taskId, $srcWorkerId, $data)
      {
      $startedAt = $this->beforeExec();

      $this->stdout(“New AsyncTask: “);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{taskId}\n”, Console::FG_BLUE);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{data}\n”, Console::FG_BLUE);

      KaTeX parse error: Expected 'EOF', got '&' at position 8: server-&̲gt;finish(data);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;afterExec(startedAt);
      }

    /**

    • @inheritdoc
      /
      public function onWorkerStop($server, KaTeX parse error: Expected '}', got 'EOF' at end of input: …id) { // Yii::app->db->close();
      }
      /
      *
    • @inheritdoc
      */
      public function onWorkerStart($server, KaTeX parse error: Expected '}', got 'EOF' at end of input: …id) { // Yii::app->db->open();
      }

    /**

    • @inheritdoc
      */
      public function onTaskFinish($server, $taskId, $data)
      {
      $startedAt = $this->beforeExec();

      $this->stdout("AsyncTask finished: “);
      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;stdout("{taskId}\n”, Console::FG_BLUE);

      KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;afterExec(startedAt);
      }
      }

四:操作TCP服务

<?php
/**
 * @link http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司
 * @license http://www.u-bo.com/license/
 */

namespace console\controllers;

use Yii;
use yii\helpers\Console;
use yii\helpers\FileHelper;
use yii\helpers\ArrayHelper;
use console\swoole\Server;

/**

  • WebSocket Server controller.

  • @see https://github.com/tystudy/yii2-swoole-websocket/blob/master/README.md

  • @author wangjian

  • @since 1.0
    /
    class SwooleController extends Controller
    {
    /
    *

    • @var string 监听IP
      /
      public $host;
      /
      *
    • @var string 监听端口
      /
      public $port;
      /
      *
    • @var boolean 是否以守护进程方式启动
      /
      public $daemon = false;
      /
      *
    • @var boolean 是否启动测试类
      */
      public $test = false;

    /**

    • @var array Swoole参数配置项
      */
      private $_params;

    /**

    • @var array Swoole参数配置项(HTTP协议)
      /
      private $_http_params;
      /
      *
    • @var array Swoole参数配置项(TCP协议)
      */
      private $_tcp_params;

    /**

    • @inheritdoc
      */
      public function beforeAction(KaTeX parse error: Expected '}', got 'EOF' at end of input: …::beforeAction(action)) {
      //判断是否开启swoole拓展
      if (!extension_loaded(‘swoole’)) {
      return false;
      }

       //获取swoole配置信息
       if (!isset(Yii::$app-&gt;params['swoole'])) {
           return false;
       }
       $this-&gt;_params = Yii::$app-&gt;params['swoole'];
       $this-&gt;_http_params = ArrayHelper::remove($this-&gt;_params, 'http');
       $this-&gt;_tcp_params = ArrayHelper::remove($this-&gt;_params, 'tcp');
      
       foreach ($this-&gt;_params as &amp;$param) {
           if (strncmp($param, '@', 1) === 0) {
               $param = Yii::getAlias($param);
           }
       }
      
       $this-&gt;_params = ArrayHelper::merge($this-&gt;_params, [
           'daemonize' =&gt; $this-&gt;daemon
       ]);
      
       return true;
      

      } else {
      return false;
      }
      }
      /**

    • 启动服务
      */
      public function actionStart()
      {
      if ($this->getPid() !== false) {
      $this->stdout(“WebSocket Server is already started!\n”, Console::FG_RED);
      return self::EXIT_CODE_NORMAL;
      }
      s e r v e r = n e w S e r v e r ( server = new Server( server=newServer(this->_http_params, $this->_tcp_params, $this->_params);
      $server->run();
      }

    /**

    • 停止服务
      */
      public function actionStop()
      {
      $pid = KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;getPid(); i…pid === false) {
      $this->stdout(“Tcp Server is already stoped!\n”, Console::FG_RED);
      return self::EXIT_CODE_NORMAL;
      }

      \swoole_process::kill($pid);
      }

    /**

    • 清理日志文件
      */
      public function actionClearLog()
      {
      l o g F i l e = Y i i : : g e t A l i a s ( logFile = Yii::getAlias( logFile=Yii::getAlias(this->_params[‘log_file’]);
      FileHelper::unlink($logFile);
      }

    /**

    • 获取进程PID

    • @return false|integer PID
      */
      private function getPid()
      {
      $pidFile = KaTeX parse error: Expected 'EOF', got '&' at position 6: this-&̲gt;_params['pid…pidFile)) {
      return false;
      }

      p i d = f i l e g e t c o n t e n t s ( pid = file_get_contents( pid=filegetcontents(pidFile);
      if (empty($pid)) {
      return false;
      }

      p i d = i n t v a l ( pid = intval( pid=intval(pid);
      if (\swoole_process::kill($pid, 0)) {
      return KaTeX parse error: Expected 'EOF', got '}' at position 7: pid; }̲ else { Fi…pidFile);
      return false;
      }
      }

    /**

    • @inheritdoc
      */
      public function options(KaTeX parse error: Expected '}', got 'EOF' at end of input: …arent::options(actionID), [
      ‘daemon’,
      ‘test’
      ]);
      }

    /**

    • @inheritdoc
      */
      public function optionAliases()
      {
      return ArrayHelper::merge(parent::optionAliases(), [
      ‘d’ => ‘daemon’,
      ‘t’ => ‘test’,
      ]);
      }
      }

以上就是php使用swoole实现TCP服务的详细内容,更多关于php swoole实现TCP服务的资料请关注网赚博客http://www.piaodoo.com/其它相关文章!

                        友情连接:  

茂名一技http://www.szsyby.net/


茂名一技http://www.enechn.com/


美文集http://www.tpyjn.cn/


手游排行前十名http://www.bjkhrx.com/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值