Skip to content

Commit 5fbd283

Browse files
committed
脱离框架
1 parent f795345 commit 5fbd283

File tree

5 files changed

+53
-71
lines changed

5 files changed

+53
-71
lines changed

src/Logger.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,17 @@
77
namespace xiaochengfu\swoole;
88

99

10-
class Logger extends SwooleBase
10+
class Logger
1111
{
12+
/**
13+
* 配置对象
14+
* @var array
15+
*/
16+
private $settings = [];
17+
18+
function __construct($settings){
19+
$this->settings = $settings;
20+
}
1221

1322
public function info($msg,$logfile='') {
1423
$settings = $this->settings;

src/Swoole.php

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,41 @@
77
namespace xiaochengfu\swoole;
88
use Curl\Curl;
99

10-
class Swoole extends SwooleBase
10+
class Swoole
1111
{
1212

13-
const NORMAL = 1;
14-
const TYPE_SOCKET = 'socket';//socket请求
13+
/**
14+
* 配置对象
15+
* @var array
16+
*/
17+
private $settings = [];
18+
19+
function __construct($settings){
20+
$this->settings = $settings;
21+
}
1522

1623
/**
24+
* Description: 主动推送
25+
* Author: hp <xcf-hp@foxmail.com>
1726
* @param $fd
18-
* @param $data
19-
* @return mixed
20-
* websocket消息推送
21-
* 格式为:
22-
* 'fd' => xx,//客户端id
23-
* 'data' => [],//消息体
27+
* @param array $data
28+
* @return bool
29+
* @throws \ErrorException
2430
*/
25-
public function pushMsg($fd,$data){
26-
$t1 = microtime(true);
27-
(new Logger())->info('[ 消息发送触发开始 '.date('Y-m-d H:i:s' ,time()).']-data为'.json_encode($data));
31+
public function pushMsg($fd,array $data){
2832
$settings = $this->settings;
29-
$datas['type'] = self::TYPE_SOCKET;
30-
$datas['data'] = json_encode($data);
31-
$datas['fd'] = $fd;
33+
$t1 = microtime(true);
34+
$logger = new Logger($settings);
35+
$logger->info('[ 消息发送触发开始 '.date('Y-m-d H:i:s' ,time()).']-data为'.json_encode($data));
3236
$curl = new Curl();
33-
$datas['command'] = $data['command'];
34-
$curl->post($settings['swoole_http'],$datas);
37+
$body = [
38+
'fd'=>$fd,
39+
'data'=>$data,
40+
'type'=>'socket'
41+
];
42+
$curl->post($settings['swoole_http'],$body);
3543
$t2 = microtime(true);
36-
(new Logger())->info('[ 消息发送触发结束 '.date('Y-m-d H:i:s', time()).']-执行耗时:'.round($t2-$t1, 3));
44+
$logger->info('[ 消息发送触发结束 '.date('Y-m-d H:i:s', time()).']-执行耗时:'.round($t2-$t1, 3));
3745
return true;
3846
}
3947

src/SwooleBase.php

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/SwooleService.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,10 @@ class SwooleService{
1414
*/
1515
private $settings = [];
1616

17-
/**
18-
* 框架全局对象
19-
* @var null
20-
*/
21-
private $app = null;
2217

23-
function __construct($settings,$app){
18+
function __construct($settings){
2419
$this->settings = $settings;
2520
$this->check();
26-
$this->app = $app;
2721
}
2822

2923
/**
@@ -47,8 +41,8 @@ private function check(){
4741
* 检查命令 lsof 命令是否存在
4842
*/
4943
exec("whereis lsof", $out);
50-
if (strpos($out[0], "/usr/sbin/lsof") === false ) {
51-
exit('error:找不到lsof命令,请确保lsof在/usr/sbin下' . PHP_EOL);
44+
if (strpos($out[0], "lsof") === false ) {
45+
exit('error:找不到lsof命令' . PHP_EOL);
5246
}
5347
/**
5448
* 检查目录是否存在并赋予权限
@@ -66,7 +60,7 @@ private function check(){
6660
*/
6761
private function bindPort($port) {
6862
$res = [];
69-
$cmd = "/usr/sbin/lsof -i :{$port}|awk '$1 != \"COMMAND\" {print $1, $2, $9}'";
63+
$cmd = "lsof -i :{$port}|awk '$1 != \"COMMAND\" {print $1, $2, $9}'";
7064
//eg: php 7935 localhost:9512
7165
exec($cmd, $out);
7266
if ($out) {
@@ -119,7 +113,7 @@ public function serviceStart(){
119113
}
120114

121115
//启动
122-
$server = new SwooleSetWebSocket($this->settings,$this->app);
116+
$server = new SwooleSetWebSocket($this->settings);
123117
$server->run();
124118

125119
}

src/SwooleSetWebSocket.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,13 @@ class SwooleSetWebSocket{
1717
*/
1818
private $settings = [];
1919

20-
/**
21-
* 框架全局 对象
22-
* @var array
23-
*/
24-
private $app = null;
2520

2621
/**
2722
* SwooleSetWebSocket constructor.
2823
* @param $settings
29-
* @param $app
3024
*/
31-
public function __construct($settings,$app){
25+
public function __construct($settings){
3226
$this->settings = $settings;
33-
$this->app = $app;
3427
}
3528

3629
/**
@@ -192,20 +185,23 @@ public function onRequest($request, $response){
192185
$status = $this->server->connection_info($requestData['fd']);
193186
if($status['websocket_status'] == WEBSOCKET_STATUS_FRAME){
194187
$t1 = microtime(true);
195-
(new Logger())->info('[ 消息发送开始 '.date('Y-m-d H:i:s', time()).']-id为'.$requestData['fd']);
196-
$result = $this->server->push($requestData['fd'],$requestData['data']);
188+
$logger = new Logger($this->settings);
189+
$logger->info('[ 消息发送开始 '.date('Y-m-d H:i:s', time()).']-id为'.$requestData['fd']);
190+
$result = $this->server->push($requestData['fd'],json_encode($requestData['data']));
197191
echo $result. PHP_EOL;
198-
echo time(). PHP_EOL;
199192
echo date('Y-m-d H:i:s'). PHP_EOL;
200193
$t2 = microtime(true);
201-
(new Logger())->info('[ 消息发送结束 '.date('Y-m-d H:i:s', time()).']-id为'.$requestData['fd'].' 执行耗时:'.round($t2-$t1, 3));
202-
} else
203-
$this->onClose($this->server, $requestData['fd']);
194+
$logger->info('[ 消息发送结束 '.date('Y-m-d H:i:s', time()).']-id为'.$requestData['fd'].' 执行耗时:'.round($t2-$t1, 3));
195+
$response->end('success');
196+
} else{
197+
$this->onClose($this->server, $requestData['fd']);
198+
}
204199
}
205200
}
206201
}
202+
}else{
203+
$response->end(json_encode($this->server->stats()));
207204
}
208-
$response->end(json_encode($this->server->stats()));
209205
}
210206

211207

@@ -236,7 +232,7 @@ private function parseData($data){
236232
*/
237233
public function onTask($serv, $task_id, $src_worker_id, $data){
238234
//$task_id和$src_worker_id组合起来才是全局唯一的
239-
(new Logger())->info('[task data] '.$data);
235+
(new Logger($this->settings))->info('[task data] '.$data);
240236
$data = $this->parseData($data);
241237
if($data === false){
242238
return false;

0 commit comments

Comments
 (0)