websocket后台新订单通知——
Laravel5.8workmanPHPSocket.IO教程,PHP跨平台实时通讯框架
PHPSocket.IO是PHP版本的Socket.IO服务端实现,基于workerman开发,⽤于替换node.js版本Socket.IO服务端。
PHPSocket.IO底层采⽤websocket协议通讯,如果客户端不⽀持websocket协议, 则会⾃动采⽤http长轮询的⽅式通讯。
环境
Laravel 5.8
PHPSocket.IO 1.1
安装依赖
composer require workerman/phpsocket.io
composer require guzzlehttp/guzzle
启动程序整合到artisan命令中
创建⽂件命令 php artisan make:command WorkermanCommand
app/Console/Commands/WorkermanCommand.php
workman PHPSocket.Io服务命令⾏启动程序
<?php
namespace App\Console\Commands;
use Workerman\Worker;
use PHPSocketIO\SocketIO;
use Illuminate\Console\Command;
//
class WorkermanCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'workman
{action=start : start | restart | reload(平滑重启) | stop | status | connetions}
{--d : deamon or debug}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'workman';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// 如果你在windows启动命令⾏,需要注释这⼀⾏服务才能启动
global $argv;
/
/启动php脚本所需的命令⾏参数
$argv[0] = 'workman';
$argv[1] = $this->argument('action'); // start | restart | reload(平滑重启) | stop | status | connetions        $argv[2] = $this->option('d') ? '-d' : ''; // 守护进程模式或调试模式启动
// https需要加上证书验证
if (env('APP_HEADER', 'http') == 'https') {
$context = array(
'ssl' => array(
'local_cert' => '/etc/nginx/ssl/fullchain_all.pem',
'local_pk' => '/etc/nginx/ssl/privkey_all.pem',
'verify_peer' => false,
)
);
$io = new SocketIO(6001, $context);
} else {
$io = new SocketIO(6001);
}
$io->on('workerStart', function () use ($io) {
// 同时启动⼀个worker监听
$inner_http_worker = new Worker('0.0.0.0:6002');
$inner_http_worker->onMessage = function ($http_connection, $request) use ($io) {
// 接受推送过来的数据
$get = $request->get();
if (isset($get['type']) && !empty($get['type'])) {
// 向客户端发送响应事件请求,第⼆参数可以携带参数
$io->emit($get['type']);
}
if ($io) {
return $http_connection->send('ok');
}
return $httpConnection->send('fail');
};
$inner_http_worker->listen();
});
Worker::runAll();
}
}
workman PHPSocket.Io服务命令⾏启动程序
启动PHPSocket.Io服务
#以守护进程模式启动
php artisan workman start -d
#以调式模式启动
php artisan workman start
#终⽌
php artisan workman stop
#平滑重启
php artisan workman reload
web页⾯
<script src='//cdn.bootcss/socket.io/1.3.7/socket.io.js'></script>
<script>
// 连接服务端,如果是https请求则必须使⽤域名请求,证书是要验证域名的
var socket = io('127.0.0.1:6001');
// 触发服务端的chat message事件
// 服务端通过emit('chat message from server', $msg)触发客户端的chat message from server事件('chat message from server', function(msg){
console.log('get message:' + msg + ' from server');
});
</script>
打开web页⾯看看socket是否连接成功
推送消息
在laravel api层新建了⼀个NoticeController.php ⽤来发送响应事件
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
/*
* 消息提醒通知
* @author niuben
*/
class NoticeController extends Controller
{
const URL = "127.0.0.1:6002";
/*
* 发送消息通知
* @request post
* @param type
*
*/
public function setNotices(Request $request)
{
$post = $request->post();
if (!isset($post['type']) || empty($post['type'])) {
return ['code' => 200404, 'msg' => '参数缺失'];
}
$message_request =  $this->curl_get(self::URL . '?type='.$post['type']);
if($message_request !='ok'){
return ['code' => 5324, 'msg' => '提醒发送失败'];
}
return ['code' => 200, 'msg' => '成功'];
}
/*
* curl get请求
*/
public function curl_get($url)
{
$header = array(
'Accept: application/json',
)
;
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头⽂件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 0);
// 超时设置,以秒为单位
curl_setopt($curl, CURLOPT_TIMEOUT, 1);
// 超时设置,以毫秒为单位
// curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
// 设置请求头
curl命令发送post请求带参数
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
//设置获取的信息以⽂件流的形式返回,⽽不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//执⾏命令
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
}
这时候请求api,传⼊ type=chat message from server ,看看客户端是否打印出数据

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。