@@ -18,7 +18,7 @@ class SwooleSetWebSocket{
1818 private $ settings = [];
1919
2020 /**
21- * Yii::$app 对象
21+ * 框架全局 对象
2222 * @var array
2323 */
2424 private $ app = null ;
@@ -34,8 +34,9 @@ public function __construct($settings,$app){
3434 }
3535
3636 /**
37- * 设置swoole进程名称
38- * @param string $name swoole进程名称
37+ * Description: 设置swoole进程名称,mac无法设置
38+ * Author: hp <xcf-hp@foxmail.com>
39+ * @param $name
3940 */
4041 private function setProcessName ($ name ){
4142 if (PHP_OS != 'Darwin ' ){
@@ -52,8 +53,9 @@ private function setProcessName($name){
5253 }
5354
5455 /**
55- * 运行服务
56- * @return [type] [description]
56+ * Description: 运行服务
57+ * Author: hp <xcf-hp@foxmail.com>
58+ * @return mixed
5759 */
5860 public function run (){
5961 $ this ->server = new \swoole_websocket_server ($ this ->settings ['host ' ], $ this ->settings ['port ' ]);
@@ -91,39 +93,36 @@ public function run(){
9193 /**
9294 * Description: onStart
9395 * Author: hp <xcf-hp@foxmail.com>
94- * Updater:
9596 * @param $server
9697 * @return bool
9798 */
9899 public function onStart ($ server ){
99- echo '[ ' . date ('Y-m-d H:i:s ' ) . "] \t swoole_websocket_server master worker start \n" ;
100+ echo '[ ' . date ('Y-m-d H:i:s ' ) . "] \t pid: { $ server -> master_pid }\t { $ this -> settings [ ' process_name ' ]} master worker start \n" ;
100101 $ this ->setProcessName ($ this ->settings ['process_name ' ] . '-master ' );
101102 //记录进程id,脚本实现自动重启
102- $ pid = "{$ this -> server ->master_pid }\n{$ this -> server ->manager_pid }" ;
103+ $ pid = "{$ server ->master_pid }\n{$ server ->manager_pid }" ;//master pid 和manger pid
103104 file_put_contents ($ this ->settings ['pidfile ' ], $ pid );
104105 return true ;
105106 }
106107
107108 /**
108109 * Description: onManagerStart
109110 * Author: hp <xcf-hp@foxmail.com>
110- * Updater:
111- * @param $server
112111 */
113112 public function onManagerStart ($ server ){
114- echo '[ ' . date ('Y-m-d H:i:s ' ) . "] \t swoole_http_server manager worker start \n" ;
113+ echo '[ ' . date ('Y-m-d H:i:s ' ) . "] \t pid: { $ server -> manager_pid }\t { $ this -> settings [ ' process_name ' ]} manager worker start \n" ;
115114 $ this ->setProcessName ($ this ->settings ['process_name ' ] . '-manager ' );
116115 }
117116
118117 /**
119118 * Description: onWorkerStart
120119 * Author: hp <xcf-hp@foxmail.com>
121- * Updater:
122120 * @param $server
123121 * @param $workerId
124122 */
125- public function onWorkerStart ($ server , $ workerId ){
126- if ($ workerId >= $ this ->settings ['worker_num ' ]) {
123+ public function onWorkerStart ($ server ,$ workerId ){
124+ //$server->taskworker 返回true则代表是task进程
125+ if ($ server ->taskworker && $ workerId >= $ this ->settings ['worker_num ' ]) {
127126 $ this ->setProcessName ($ this ->settings ['process_name ' ] . '-task ' );
128127 } else {
129128 $ this ->setProcessName ($ this ->settings ['process_name ' ] . '-event ' );
@@ -133,18 +132,17 @@ public function onWorkerStart($server, $workerId){
133132 /**
134133 * Description: onWorkerStop
135134 * Author: hp <xcf-hp@foxmail.com>
136- * Updater:
137135 * @param $server
138136 * @param $workerId
139137 */
140138 public function onWorkerStop ($ server , $ workerId ){
141- echo '[ ' . date ('Y-m-d H:i:s ' ) ."] \t swoole_http_server[ {$ server ->settings ['process_name ' ]} worker: {$ workerId } shutdown \n" ;
139+ $ workName = $ server ->taskwork ?'task ' :'work ' ;
140+ echo '[ ' . date ('Y-m-d H:i:s ' ) ."] \t {$ this ->settings ['process_name ' ]} $ workName: {$ workerId } shutdown \n" ;
142141 }
143142
144143 /**
145144 * Description: 接收websocket客户端信息,并实时返回
146145 * Author: hp <xcf-hp@foxmail.com>
147- * Updater:
148146 * @param $server
149147 * @param $frame
150148 * @return mixed
@@ -196,12 +194,17 @@ public function onMessage($server, $frame){
196194 *
197195 */
198196 if (isset ($ requestData ['data ' ]['n ' ]) && $ requestData ['data ' ]['n ' ]) {
199- // thinkphp5的应用示例
200- app ($ requestData ['data ' ]['n ' ],[
201- ['server ' =>$ server ,'fid ' => $ frame ->fd ,'data ' =>$ requestData ['data ' ]['p ' ]]
202- ])->{$ requestData ['data ' ]['f ' ]}();
203- // yii2的应用示例
204- // \Yii::$app->runAction($requestData['data']['a'], [['server'=>$server,'fid'=> $frame->fd,'data'=>$requestData['data']['p']]]);
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+ }
205208 }else {
206209 $ server ->push ($ frame ->fd , "终于等到你啦! " );
207210 }
@@ -224,7 +227,7 @@ public function onRequest($request, $response){
224227 if ($ allFd ){
225228 if (in_array ($ requestData ['fd ' ],$ this ->server ->connection_list (0 ,100 ))){
226229 $ status = $ this ->server ->connection_info ($ requestData ['fd ' ]);
227- if ($ status ['websocket_status ' ] == 3 ){
230+ if ($ status ['websocket_status ' ] == WEBSOCKET_STATUS_FRAME ){
228231 $ t1 = microtime (true );
229232 (new Logger ())->info ('[ 消息发送开始 ' .date ('Y-m-d H:i:s ' , time ()).']-id为 ' .$ requestData ['fd ' ]);
230233 $ result = $ this ->server ->push ($ requestData ['fd ' ],$ requestData ['data ' ]);
@@ -246,7 +249,6 @@ public function onRequest($request, $response){
246249 /**
247250 * Description: 解析data对象
248251 * Author: hp <xcf-hp@foxmail.com>
249- * Updater:
250252 * @param $data
251253 * @return array|bool|mixed
252254 */
@@ -263,14 +265,14 @@ private function parseData($data){
263265 /**
264266 * Description: 任务处理
265267 * Author: hp <xcf-hp@foxmail.com>
266- * Updater:
267268 * @param $serv
268269 * @param $task_id
269- * @param $from_id
270+ * @param $src_worker_id
270271 * @param $data
271272 * @return array|bool|mixed
272273 */
273- public function onTask ($ serv , $ task_id , $ from_id , $ data ){
274+ public function onTask ($ serv , $ task_id , $ src_worker_id , $ data ){
275+ //$task_id和$src_worker_id组合起来才是全局唯一的
274276 (new Logger ())->info ('[task data] ' .$ data );
275277 $ data = $ this ->parseData ($ data );
276278 if ($ data === false ){
@@ -280,25 +282,9 @@ public function onTask($serv, $task_id, $from_id, $data){
280282 }
281283
282284
283- protected function httpGet ($ url ,$ data ){
284- if ($ data ) {
285- $ url .='? ' .http_build_query ($ data ) ;
286- }
287- $ curlObj = curl_init (); //初始化curl,
288- curl_setopt ($ curlObj , CURLOPT_URL , $ url ); //设置网址
289- curl_setopt ($ curlObj , CURLOPT_RETURNTRANSFER , 1 ); //将curl_exec的结果返回
290- curl_setopt ($ curlObj , CURLOPT_SSL_VERIFYPEER , FALSE );
291- curl_setopt ($ curlObj , CURLOPT_SSL_VERIFYHOST , FALSE );
292- curl_setopt ($ curlObj , CURLOPT_HEADER , 0 ); //是否输出返回头信息
293- $ response = curl_exec ($ curlObj ); //执行
294- curl_close ($ curlObj ); //关闭会话
295- return $ response ;
296- }
297-
298285 /**
299286 * Description: 解析onfinish数据
300287 * Author: hp <xcf-hp@foxmail.com>
301- * Updater:
302288 * @param $data
303289 * @return bool|string
304290 */
@@ -312,7 +298,6 @@ private function genFinishData($data){
312298 /**
313299 * Description: 任务结束回调函数
314300 * Author: hp <xcf-hp@foxmail.com>
315- * Updater:
316301 * @param $server
317302 * @param $taskId
318303 * @param $data
@@ -342,11 +327,10 @@ public function onOpen($server, $request){
342327 /**
343328 * Description: 客户端关闭后,服务端的消息回调
344329 * Author: hp <xcf-hp@foxmail.com>
345- * Updater:
346- * @param $ser
330+ * @param $server
347331 * @param $fd
348332 */
349- public function onClose ($ ser , $ fd ){
333+ public function onClose ($ server , $ fd ){
350334 echo "client {$ fd } closed \n" ;
351335 /**
352336 * 这里可以写你的回调处理
@@ -364,12 +348,13 @@ public function onClose($ser, $fd){
364348 */
365349 public function onReceive ($ server , $ fd , $ from_id , $ data ){
366350 if ($ data == 'stats ' ){
367- return $ this -> server ->send ($ fd ,var_export ($ this -> server ->stats (),true ),$ from_id );
351+ return $ server ->send ($ fd ,var_export ($ server ->stats (),true ),$ from_id );
368352 }
369- $ this -> server ->task ($ data );//非阻塞的,将任务扔到任务池,并及时返还
353+ $ server ->task ($ data );//非阻塞的,将任务扔到任务池,并及时返还
370354 return true ;
371355
372356 }
373357
358+
374359}
375360
0 commit comments