因?yàn)槭褂胠aravel, 所以使用artisan編寫服務(wù),
也可以使用其他文件編寫, 直接用php 命令執(zhí)行, 參照博客
PHP使用workerman實(shí)現(xiàn)后端消息推送前端, 完成微信登錄
建議先讀一下上邊這邊博客, 有助于讀者理解整個(gè)推送過程
- 代碼如下
app/Console/Commands/WebsocketServer
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Workerman\Worker;
use Log;
class WebsocketServer extends Command
{
private $worker = null;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'websocket {action}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'start websocket server for accept client connect';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
global $argv;
$arg = $this->argument('action');
$argv[1] = $argv[2];
$argv[2] = isset($argv[3]) ? "-{$argv[3]}" : '';
switch ($arg) {
case 'start':
$this->start();
break;
case 'stop':
break;
case 'restart':
break;
case 'reload':
break;
case 'status':
break;
case 'connections':
break;
}
}
private function start(){
// 使用websocket協(xié)議, 監(jiān)聽端口, 等待客戶端發(fā)起websocket連接
$this->worker = new Worker('websocket://0.0.0.0:18888');
// 保存客戶端連接
$this->worker->uidConnections = [];
$this->worker->count = 1;
$this->worker->onWorkerStart = function ($worker) {
// 創(chuàng)建內(nèi)部連接, 用于接收掃碼后, php服務(wù)器推送的消息
$inner_text_worker = new Worker('tcp://0.0.0.0:18889');
$inner_text_worker->onMessage = function ($connection, $buffer) {
Log::alert('workerman inner accept message : ' . $buffer);
$data = json_decode($buffer, true);
$uid = $data['scene'];
$token = $data['token'];
// 向?yàn)g覽器推送用戶標(biāo)識(shí)
$res = $this->sendMessageByUid($uid, $token);
Log::alert('workerman reply message to uid : '. $uid .'..$openid..'. $token);
$mess = $res ? 'ok' : 'fail';
Log::alert('workerman reply message : ' . $mess);
// 通知php客戶端推送結(jié)果
$connection->send($mess);
};
$inner_text_worker->listen();
};
$this->worker->onMessage = function ($connection, $data) {
Log::alert('workerman accept browser data : ' . $data);
if (!isset($connection->uid)) {
$connection->uid = $data;
$this->worker->uidConnections[$connection->uid] = $connection;
return;
}
};
$this->worker->onClose = function ($connection) {
if (isset($connection->uid)) {
Log::alert('workerman close browser : ' . $connection->uid);
unset($this->worker->uidConnections[$connection->uid]);
}
};
Worker::runAll();
}
/**
* 消息廣播
* @param $message
*/
private function broadCast($message)
{
global $worker;
foreach ($worker->uidConnections as $connection) {
$connection->send($message);
}
}
private function sendMessageByUid($uid, $message)
{
Log::alert(json_encode($this->worker->uidConnections));
if (isset($this->worker->uidConnections[$uid])) {
$connection = $this->worker->uidConnections[$uid];
$connection->send($message);
return true;
}
return false;
}
}
在app/Console/Kernel.php中注冊
protected $commands = [
//
WebsocketServer::class,
];
- 進(jìn)入到項(xiàng)目根目錄, 啟動(dòng), 等待連接
php artisan websocket start