参考 参考2

另外主要用到artisan

首先创建SwooleCommand.php

<?php

namespace App\Console\Commands;

use App\Http\Controllers\SwooleHandler;use App\Models\Logs;
use App\Traits\TcpServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\App;
use Symfony\Component\Console\Input\InputArgument; class SwooleCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'swoole {action=start}'; /**
* The console command description.
*
* @var string
*/
protected $description = 'this is command for swoole'; protected $serv;
/**
* Create a new command instance.
*
* @return void
*/ /**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->fire();
} public function fire(){
$action = $this->argument('action');
switch ($action){
case 'start':
$this->info("swoole observer started");
$this->start();
break;
case 'stop':
$this->info("stoped");
break;
case 'restart':
$this->info("restarted");
break;
default:
$this->error("unknown command");
} } private function start(){
Logs::truncate();//重启cli后清空数据库
$this->serv = new \Swoole\Server(env('SWOOLE_SERVICE_ADDRESS', '127.0.0.1'), env('SWOOLE_SERVICE_PORT', 9503), SWOOLE_PROCESS);
$this->serv->set([
'worker_num' => 1,
'heartbeat_check_interval' => 60, //心跳检测
'max_request' => 1000,
'log_file' => './log/swoole.log',
]);
$handler = new SwooleHandler();
$this->serv->on('connect', [$handler, 'onConnect']);
$this->serv->on('receive', [$handler, 'onReceive']);
$this->serv->on('workerStart', [$handler, 'onWorkerStart']);
$this->serv->on('close', [$handler, 'onClose']);
$this->serv->start();
} protected function getArguments()
{
return [[
'action', InputArgument::REQUIRED, 'start|stop|restart'
]];
}
}

SwooleHandler.php

<?php
/**
* Created by PhpStorm.
* User: liuning
* Date: 2018/4/9
* Time: 11:35
*/ namespace App\Http\Controllers;
use App\Models\Config;
use App\Models\Logs;use App\Modules\Meeting\Models\ReserveRecord;use App\Traits\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache; class SwooleHandler extends BaseController
{public function onConnect($server, $fd){
echo "建立连接通道ID:$fd\n";
} public function onWorkerStart($server, $workerId){
echo "worker启动了\n";
//进程开启时绑定定时器,只有workderId为0才添加定时器,避免重复添加
if($workerId == 0){
echo "定时开启\n";
swoole_timer_tick(10000, [$this, 'onTimer'], ['server'=>$server]);
}
} public function onReceive($server, $fd, $fromId, $data){
//收到指令后处理操作
} public function onTimer($timerId, $params){
//循环定时任务
echo "执行开门定时任务开始\n";
} public function onClose($server, $fd){
Logs::delLog($fd);//关闭通道
//添加警报
echo "断开连接通道: {$fd}\n";
} public function onTask($server, $task_id, $from_id, $data){
echo "任务开始\n";
} public function onFinish($server, $task_id, $data){
echo "任务结束\n";
}
}

在Kernel.php中新增命令

protected $commands = [
SwooleCommand::class,
];

这样就能在网站根目录打开tcp服务了

/usr/local/php/bin/php artisan swoole start
或以下命令后台运行
nohup /usr/local/php/bin/php artisan swoole start > dev/null 2>&1 &

如果想做指定用户推送数据就得另辟蹊径了,我创建了临时客户端与服务端建立连接。

同理先创建客户端命令

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ClientCommand extends Command
{
/**
* The name and signature of the console command.
* command+mac
* @var string
*/
protected $signature = 'send {ccc}'; /**
* The console command description.
*
* @var string
*/
protected $description = 'send...'; /**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
} /**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$command = $this->argument('ccc');
$client = new \Swoole\Client(SWOOLE_SOCK_TCP, SWOOLE_SOCK_ASYNC);
$client->on("connect", function($cli)use($command) {
$cli->send(fromateSendCode($command));
});
$client->on("receive", function($cli, $data){
//处理$data
//echo 输出
});
$client->on("error", function($cli){
echo "connect failed\n";
});
$client->on("close", function($cli){
//echo "connection close\n";
});
$client->connect(env('SWOOLE_CLIENT_ADDRESS', '127.0.0.1'), env('SWOOLE_SERVICE_PORT', 9503), 0.5);
} protected function getArguments()
{
return [[
'ccc', InputArgument::REQUIRED
]];
}
}

这样就能用命令与服务端建立链接了

/usr/local/php/bin/php artisan send 111

这样客户端会一直与服务器建立连接,直到服务端主动关闭连接。所以需要在处理完客户端请求后给客户端发送数据,客户端收到后主动与服务端断开连接。

也可以调用代码来建立连接

function doShell($command){
$proPath = env('PRO_PATH', '/mnt/hgfs/www/blog');
$phpPath = env('PHP_PATH', '/usr/local/php/bin/php');
return shell_exec('cd '.$proPath.'; '.$phpPath.' artisan send '.$command);
}

laravel使用swoole的更多相关文章

  1. laravel 整合 swoole ,并简单 ab 测试对比性能以及在 PHPstorm 中利用debug调试配置swoole服务中的PHP代码

    安装PHP 的 swoole 扩展 及 安装 laravel,就不描述了 整合 laravel 和 swoole 用了这个轮子,侵入性很小,一行代码搞定,推荐一下,今天刚用,不能预测未来是否会遇见坑 ...

  2. Laravel通过Swoole提升性能

    1.安装配置laravel 1.1.composer下载laravel composer create-project --prefer-dist laravel/laravel blog " ...

  3. PHP laravel+thrift+swoole打造微服务框架

    Laravel作为最受欢迎的php web框架一直广受广大互联网公司的喜爱. 笔者也参与过一些由laravel开发的项目.虽然laravel的性能广受诟病但是业界也有一些比较好的解决方案,比如堆机器, ...

  4. Laravel集成Swoole教程

    1.准备工作 安装 Laravel laravel new laravel-swoole     本人使用 valet 进行开发,可以使用 laravel-swoole.test     进行访问 2 ...

  5. laravel配合swoole使用总结

    最近对接硬件做了两个项目,用到了swoole 第一个是门禁系统,需要远程开门.离线报警.定时开门.离线刷卡等功能 1.远程开门: 目前用cli创建个临时客户端连接服务端发送命令,服务端处理完成后客户端 ...

  6. 基于Laravel+Swoole开发智能家居后端

    基于Laravel+Swoole开发智能家居后端 在上一篇<Laravel如何优雅的使用Swoole>中我已经大概谈到了Laravel结合Swoole的用法. 今天,我参与的智能家居项目基 ...

  7. Laravel如何优雅的使用Swoole

    背景 正在做一个智能家居的项目(钱低的吓死人怎么办),接收下位机(就是控制智能家居硬件模块的HUB)协议解析,Web端维护硬件状态,利用APP交互.由于下位机数据是发送到服务器的XXX端口,所以必须对 ...

  8. 使用 Swoole 来加速你的 Laravel 应用

    Swoole 是为 PHP 开发的生产级异步编程框架. 他是一个纯 C 开发的扩展, 他允许 PHP 开发者在 PHP 中写 高性能,可扩展的并发 TCP, UDP, Unix socket, HTT ...

  9. 使用 Swoole 来加速 Laravel应用

    Swoole 是为 PHP 开发的生产级异步编程框架. 他是一个纯 C 开发的扩展, 他允许 PHP 开发者在 PHP 中写 高性能,可扩展的并发 TCP, UDP, Unix socket, HTT ...

随机推荐

  1. UIView封装动画--iOS利用系统提供方法来做关键帧动画

    iOS利用系统提供方法来做关键帧动画 ios7以后才有用. /*关键帧动画 options:UIViewKeyframeAnimationOptions类型 */ [UIView animateKey ...

  2. Vue数据双向绑定探究

    前面的啰嗦话,写一点吧,或许就有点用呢 使用过vue的小伙伴都会感觉,哇,这个框架对开发者这么友好,简直都要笑出声了. 确实,使用过vue的框架做开发的人都会感觉到,以前写一大堆操作dom,bom的东 ...

  3. Python对象拷贝——深拷贝与浅拷贝

    对象赋值 浅拷贝 深拷贝 1. 对象赋值 对象的赋值实际上是对对象的引用.也就是说当把一个对象赋值给另一个对象时,只是拷贝了引用.如: >>> t1 = tuple('furzoom ...

  4. CentOS(Linux) - 安装软件笔记(一) - VPSMate(服务器可视化管理工具)安装

    更多详情点击参考官网说明 vpsmate官方推荐centos版本为6.2 64位 使用 SSH 连接工具,如 PuTTY.XShell.SecureCRT 等,连接到您的 Linux 服务器. 执行以 ...

  5. jmeter中的响应断言

    断言就类似LoadRunner中的检查点.对上一个请求返回的信息,做字符串.数据包大小.HTML.XML.图片等做判断,确保返回的信息的准确性. jmeter的断言有好多,下面是一个响应断言 新建一个 ...

  6. TCP连接过程

    TCP建立连接与释放连接  最近复习准备<计算机网络>考试,感觉TCP协议建立连接与释放连接这两个过程比较重要,所以把自己理解的部分写下来. 1.建立连接:(三次握手)   (1)客户端发 ...

  7. 【C/C++】产生随机数

    #include<iostream> #include<Ctime> #include<Cstdlib> using namespace std; //产生n个st ...

  8. starUML安装与破解

    安装包百度云: 链接:https://pan.baidu.com/s/1oF_DH7Xh6yun6fFUDB2H3w 密码:1z7e 破解步骤:1. 首先打开你的starUML安装目录,并找到Lice ...

  9. SQL 维护计划(自动删除备份文件)

    --开启修改配置功能 exec sp_configure 'allow updates', 0 --启用功能 sp_configure 'xp_cmdshell',1; reconfigure --临 ...

  10. WeFlow 简单使用教程

    一.前言 WeFlow 是什么?一个高效.强大.跨平台的前端开发工作流工具.(官网定义),下载那些你们都知道,我就不一 一介绍了.下面我说一下简单使用: 二.使用教程 首先,我们使用 WeFlow 是 ...