'businessWorker' => [
'eventHandler' => 'app\controller\Events'
],
然后在Events中 源代碼是這樣寫(xiě)的,結(jié)合workman的自定義文檔 請(qǐng)問(wèn)這里怎么調(diào)用自定義協(xié)議代碼
public static function onMessage($client_id, $data)
{
}
此處是文檔中的方法,但是結(jié)合我現(xiàn)有代碼無(wú)法引入進(jìn)來(lái),謝謝
$json_worker = new Worker('JsonNL://0.0.0.0:1234');
$json_worker->onMessage = function(TcpConnection $connection, $data) {
// $data就是客戶(hù)端傳來(lái)的數(shù)據(jù),數(shù)據(jù)已經(jīng)經(jīng)過(guò)JsonNL::decode處理過(guò)
echo $data;
// $connection->send的數(shù)據(jù)會(huì)自動(dòng)調(diào)用JsonNL::encode方法打包,然后發(fā)往客戶(hù)端
$connection->send(array('code'=>0, 'msg'=>'ok'));
};
使用composer require workerman/gateway-worker 安裝Linux版本的gateway
創(chuàng)建一個(gè)自定義協(xié)議類(lèi)
<?php
namespace app\gateway\protocols;
use Workerman\Connection\ConnectionInterface;
class Custom {
public static function input($buffer, ConnectionInterface $connection) {}
public static function decode($buffer, ConnectionInterface $connection) {}
public static function encode($data) {}
}
再創(chuàng)建啟動(dòng)服務(wù)的文件Run.php
<?php
namespace app\gateway\controller;
use think\Controller;
use Workerman\Worker;
use GatewayWorker\Register;
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
class Run {
public function __construct()
{
// 初始化register
new Register('text://0.0.0.0:1236');
//初始化 bussinessWorker 進(jìn)程
$worker = new BusinessWorker();
$worker->name = 'RoomBusinessWorker';
$worker->count = 4;
$worker->registerAddress = '127.0.0.1:1236';
// 設(shè)置處理業(yè)務(wù)的類(lèi),此處制定Events的命名空間
$worker->eventHandler = '\app\gateway\controller\Events';
// 初始化 gateway 進(jìn)程
$gateway = new Gateway("websocket://0.0.0.0:7272");
$gateway->name = 'RoomGateway';
$gateway->protocol = Custom::class; //應(yīng)用層協(xié)議,這里用上面的那個(gè)文件
$gateway->count = 4;
$gateway->lanIp = '127.0.0.1';
$gateway->startPort = 2900;
$gateway->registerAddress = '127.0.0.1:1236';
// 運(yùn)行所有Worker;
Worker::runAll();
}
}
發(fā)送消息處理Events.php
<?php
namespace app\gateway\controller;
use GatewayWorker\Lib\Gateway;
use think\Controller;
class Events extends Controller
{
/**
* 有消息時(shí)
* @param integer $client_id 連接的客戶(hù)端
* @param mixed $message
* @return void
*/
public static function onMessage($client_id, $message){}
/**
* 當(dāng)客戶(hù)端斷開(kāi)連接時(shí)
* @param integer $client_id 客戶(hù)端id
*/
public static function onClose($client_id) {}
/**
* 當(dāng)用戶(hù)連接時(shí)觸發(fā)的方法
* @param integer $client_id 連接的客戶(hù)端
* @return void
*/
public static function onConnect($client_id) { }
/**
* 當(dāng)用戶(hù)斷開(kāi)連接時(shí)觸發(fā)的方法
* @param integer $client_id 斷開(kāi)連接的客戶(hù)端
* @return void
*/
// public static function onClose($client_id)
// {
// Gateway::sendToAll("client[$client_id] logout\n");
// }
/**
* 當(dāng)進(jìn)程啟動(dòng)時(shí)
* @param integer $businessWorker 進(jìn)程實(shí)例
*/
public static function onWorkerStart($businessWorker) {
echo "WorkerStart\n";
}
/**
* 當(dāng)進(jìn)程關(guān)閉時(shí)
* @param integer $businessWorker 進(jìn)程實(shí)例
*/
public static function onWorkerStop($businessWorker) {
echo "WorkerStop\n";
}
}
最后再根目錄創(chuàng)建gateway.php啟動(dòng)方便啟動(dòng)
<?php
// [ 應(yīng)用入口文件 ]
// 定義應(yīng)用目錄
define('APP_PATH', __DIR__ . '/application/');
define('BIND_MODULE','gateway/Run');
// 加載框架引導(dǎo)文件
require __DIR__ . '/thinkphp/start.php';
根目錄cmd執(zhí)行:php gateway.php start啟動(dòng)服務(wù)
代碼對(duì)齊有點(diǎn)亂 , 將就著看吧