框架 laravel5.6版本
1.安装gateway扩展
在 composer.json 件的 require 引入以下三行并执行 composer update
"workerman/gateway-worker": "^3.0",
"workerman/gatewayclient": "^3.0",
"workerman/workerman": "^3.5"
2.创建一个 Command
使用命令:php artisan make:command GatewayWorkerServer
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use GatewayWorker\BusinessWorker;
use Workerman\Worker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
class GatewayWorkerServer extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'gateway-worker:server {action} {--daemon}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'tart a GatewayWorker Server.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
//
global $argv;
if (!in_array($action = $this->argument('action'), ['start', 'stop', 'restart'])) {
$this->error('Error Arguments');
exit;
}
$argv[0] = 'gateway-worker:server';
$argv[1] = $action;
$argv[2] = $this->option('daemon') ? '-d' : '';
$this->start();
}
private function start()
{
$this->startGateWay();
$this->startBusinessWorker();
$this->startRegister();
Worker::runAll();
}
private function startBusinessWorker()
{
$worker = new BusinessWorker();
$worker->name = 'BusinessWorker'; #设置BusinessWorker进程的名称
$worker->count = 1; #设置BusinessWorker进程的数量
$worker->registerAddress = '127.0.0.1:12360'; #注册服务地址
$worker->eventHandler = \App\Http\Controllers\Home\EventsController::class; #设置使用哪个类来处理业务,业务类至少要实现onMessage静态方法,onConnect和onClose静态方法可以不用实现
}
private function startGateWay()
{
$gateway = new Gateway("websocket://0.0.0.0:23460");
$gateway->name = 'Gateway'; #设置Gateway进程的名称,方便status命令中查看统计
$gateway->count = 1; #进程的数量
$gateway->lanIp = '127.0.0.1'; #内网ip,多服务器分布式部署的时候需要填写真实的内网ip
$gateway->startPort = 2300; #监听本机端口的起始端口
$gateway->pingInterval = 30;
$gateway->pingNotResponseLimit = 1; #服务端主动发送心跳
$gateway->pingData = '{"mode":"heart"}';
$gateway->registerAddress = '127.0.0.1:12360'; #注册服务地址
}
private function startRegister()
{
new Register('text://0.0.0.0:12360');
}
}
3.创建监听文件
<?php
namespace App\GatewayWorker;
use GatewayWorker\Lib\Gateway;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Hash;
class Events
{
public static function onWorkerStart($businessWorker)
{
echo "SUCCESS";
}
public static function onConnect($client_id)
{
//dump($client_id);
Gateway::sendToClient($client_id, json_encode(['type' => 'init', 'client_id' => $client_id]));
}
public static function onWebSocketConnect($client_id, $data)
{
}
public static function onMessage($client_id, $message)
{
$response = ['errcode' => 0, 'msg' => 'ok', 'data' => []];
$message = json_decode($message);
$str = Hash::make('cola77kk123');
echo $str;
// dump($message);
switch ($message->tag) {
case 'name':
$arr['tag'] = 'name';
$arr['content'] = $message->content;
$response['data'] = json_encode($arr);
break;
case "msg":
$arr['tag'] = 'content';
$arr['content'] = $message->content;;
$arr['name'] = $message->name;;
$response['data'] = json_encode($arr);
break;
default:
# code...
break;
}
//Gateway::setSession($client_id,['name'=>'name_'.$client_id]);
//dump(Gateway::getSession($client_id));
// echo "111";
//dump(Gateway::getAllClientIdList());
// echo "222";
Gateway::sendToClient($client_id,json_encode($response));
// Gateway::sendToAll(json_encode($response));
//Gateway::isOnline();//判断客户端是否在线
//
}
public static function onClose($client_id)
{
Log::info('close connection' . $client_id);
}
}
4.在 App\Console\Kernel 引入 GatewayWorkerServer::class
use App\Console\Commands\GatewayWorkerServer;
protected $commands = [
//
GatewayWorkerServer::class
];
最后执行
composer dump-autoload
5.开启服务
php artisan gateway-worker:server start