Skip to content

Commit 10e1912

Browse files
committed
更新遗留问题
1 parent 49e28fd commit 10e1912

File tree

2 files changed

+64
-77
lines changed

2 files changed

+64
-77
lines changed

src/SwooleService.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
namespace xiaochengfu\swoole;
88

99
class SwooleService{
10+
1011
/**
1112
* 配置对象
1213
* @var array
1314
*/
1415
private $settings = [];
16+
1517
/**
16-
* Yii::$app
18+
* 框架全局对象
1719
* @var null
1820
*/
1921
private $app = null;
@@ -25,8 +27,8 @@ function __construct($settings,$app){
2527
}
2628

2729
/**
28-
* [check description]
29-
* @return [type] [description]
30+
* Description: check
31+
* Author: hp <xcf-hp@foxmail.com>
3032
*/
3133
private function check(){
3234
/**
@@ -57,30 +59,33 @@ private function check(){
5759
}
5860

5961
/**
60-
* 获取指定端口的服务占用列表
61-
* @param [type] $port 端口号
62-
* @return [type] [description]
62+
* Description: 获取指定端口的服务占用列表
63+
* Author: hp <xcf-hp@foxmail.com>
64+
* @param $port
65+
* @return array
6366
*/
6467
private function bindPort($port) {
6568
$res = [];
6669
$cmd = "/usr/sbin/lsof -i :{$port}|awk '$1 != \"COMMAND\" {print $1, $2, $9}'";
70+
//eg: php 7935 localhost:9512
6771
exec($cmd, $out);
6872
if ($out) {
6973
foreach ($out as $v) {
7074
$a = explode(' ', $v);
7175
list($ip, $p) = explode(':', $a[2]);
7276
$res[$a[1]] = [
7377
'cmd' => $a[0],
74-
'ip' => $ip,
78+
'ip' => $ip === 'localhost'?'127.0.0.1':$ip,
7579
'port' => $p,
7680
];
7781
}
7882
}
7983
return $res;
8084
}
85+
8186
/**
82-
* 启动服务
83-
* @return [type] [description]
87+
* Description: 启动服务
88+
* Author: hp <xcf-hp@foxmail.com>
8489
*/
8590
public function serviceStart(){
8691

@@ -105,7 +110,6 @@ public function serviceStart(){
105110
}
106111

107112
$bind = $this->bindPort($port);
108-
109113
if ($bind) {
110114
foreach ($bind as $k => $v) {
111115
if ($v['ip'] == '*' || $v['ip'] == $host) {
@@ -121,10 +125,8 @@ public function serviceStart(){
121125
}
122126

123127
/**
124-
* 查看服务状态
125-
* @param [type] $host host
126-
* @param [type] $port port
127-
* @return [type] [description]
128+
* Description: 查看服务状态
129+
* Author: hp <xcf-hp@foxmail.com>
128130
*/
129131
public function serviceStats(){
130132
$client = new \swoole_http_client($this->settings['host'],$this->settings['port']);
@@ -141,8 +143,8 @@ public function serviceStats(){
141143
}
142144

143145
/**
144-
* 查看进程列表
145-
* @return [type] [description]
146+
* Description: 查看进程列表
147+
* Author: hp <xcf-hp@foxmail.com>
146148
*/
147149
public function serviceList(){
148150

@@ -163,10 +165,8 @@ public function serviceList(){
163165
}
164166

165167
/**
166-
* 停止服务
167-
* @param [type] $host host
168-
* @param [type] $port port
169-
* @return [type] [description]
168+
* Description: 停止服务
169+
* Author: hp <xcf-hp@foxmail.com>
170170
*/
171171
public function serviceStop(){
172172

@@ -203,9 +203,10 @@ public function serviceStop(){
203203
}
204204

205205
/**
206-
* [error description]
207-
* @param [type] $msg [description]
208-
* @return [type] [description]
206+
* Description: msg
207+
* Author: hp <xcf-hp@foxmail.com>
208+
* @param $msg
209+
* @param bool $exit
209210
*/
210211
private function msg($msg,$exit=false){
211212

@@ -214,11 +215,12 @@ private function msg($msg,$exit=false){
214215
}else{
215216
echo $msg . PHP_EOL;
216217
}
217-
}
218+
}
219+
218220
/**
219-
* [error description]
220-
* @param [type] $msg [description]
221-
* @return [type] [description]
221+
* Description: error
222+
* Author: hp <xcf-hp@foxmail.com>
223+
* @param $msg
222224
*/
223225
private function error($msg){
224226
exit("[error]:".$msg . PHP_EOL);

src/SwooleSetWebSocket.php

Lines changed: 35 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)