Skip to content

Commit f795345

Browse files
committed
更新回调逻辑
1 parent 10e1912 commit f795345

File tree

1 file changed

+4
-41
lines changed

1 file changed

+4
-41
lines changed

src/SwooleSetWebSocket.php

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -161,50 +161,13 @@ public function onMessage($server, $frame){
161161
$requestData = json_decode($frame->data,true);
162162
/**
163163
* 功能二:
164-
* 这里分别以thinkphp5、yii2为例
165164
* 扩展部分,根据客户端发来的命令{$frame->data}来做出相应的处理,这里根据自己的需求来写不做处理...
166-
* 推荐使用框架的全局调用方法来做扩展处理,来实现解耦,主要通过client发来的socket指令data来自定义区分逻辑控制器
167-
* thinkphp5推荐data协议指令:data=>['n'=>'app\common\logic\SwooleCallback','f'=>'test','p'=>['a'=>1]],n为名字空间,f为方法,p为参数
168-
* yii2推荐data协议指令:data=>['a'=>'test/test','p'=>['a'=>1]],a为控制器,p为参数
169-
*
170-
* thinkphp5你的回调类可能是这样的:
171-
* class SwooleCallback
172-
* {
173-
* public $server;
174-
* public $fid;
175-
* public $data;
176-
* public function __construct($args)
177-
* {
178-
* $this->server = $args['server'];
179-
* $this->fid = $args['fid'];
180-
* $this->data = $args['data'];
181-
* }
182-
*
183-
* public function test(){
184-
* //实时回应
185-
* $this->server->push($this->fid,json_encode($this->data));
186-
* }
187-
* }
188-
*
189-
* yii2你的控制器console/controllers可能是这样的:
190-
* public function actionTest($param){
191-
* $info = $param['data'];
192-
* $param['server']->push($param['fid'],json_encode($info));
193-
* }
165+
* 主要通过client发来的socket指令data来自定义区分逻辑控制器
166+
* 例如data协议指令:data=>['namespace'=>'app\\swoole\\SwooleCallback','function'=>'test','params'=>['a'=>1]],namespace为名字空间,function为方法,params为参数
194167
*
195168
*/
196-
if (isset($requestData['data']['n']) && $requestData['data']['n']) {
197-
switch ($this->settings['frame']??''){
198-
case 'thinkphp':
199-
app($requestData['data']['n'],[['server'=>$server,'fid'=> $frame->fd,'data'=>$requestData['data']['p']]])->{$requestData['data']['f']}();
200-
break;
201-
case 'yii2':
202-
\Yii::$app->runAction($requestData['data']['a'], [['server'=>$server,'fid'=> $frame->fd,'data'=>$requestData['data']['p']]]);
203-
break;
204-
default:
205-
$server->push($frame->fd, "请先设置所用框架!");
206-
break;
207-
}
169+
if (isset($requestData['data']['namespace']) && $requestData['data']['namespace']) {
170+
call_user_func_array([$requestData['data']['namespace'],$requestData['data']['function']],[$server,$frame->fd,$requestData['data']['params']]);
208171
}else{
209172
$server->push($frame->fd, "终于等到你啦!");
210173
}

0 commit comments

Comments
 (0)