PHPSocket.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 Workerman
app/Console/Commands/WorkermanCommand.php
namespace App\Console\Commands;useWorkerman\Worker;usePHPSocketIO\SocketIO;useIlluminate\Console\Command;class WorkermanCommand extendsCommand
{/**
* 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 functionhandle()
{//如果你在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('http://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 msg-push start -d
#以调式模式启动
php artisan msg-push start
#终止
php artisan msg-push stop
#平滑重启
php artisan msg-push reload
web页面
//连接服务端,如果是https请求则必须使用域名请求,证书是要验证域名的
varsocket=io('http://127.0.0.1:6001');//触发服务端的chat message事件
socket.emit('chat message','这个是消息内容...');//服务端通过emit('chat message from server', $msg)触发客户端的chat message from server事件
socket.on('chat message from server',function(msg){
console.log('get message:' +msg+ 'from server');
});
打开web页面看看socket是否连接成功
推送消息
在laravel api层新建了一个NoticeController.php 用来发送响应事件
namespace App\Http\Controllers;useApp\Http\Controllers\Controller;useIlluminate\Support\Facades\DB;useIlluminate\Http\Request;useIlluminate\Support\Facades\Storage;/** 消息提醒通知
* @author niuben*/
class NoticeController extendsController
{const URL = "http://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_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;
}
}
View Code
这时候请求api,传入 type=chat message from server ,看看客户端是否打印出数据