swoole

 1 <?php
 2 ini_set('default_socket_timeout', -1);
 3 include_once '139mail.class.php';
 4 class serverEmail
 5 {
 6         private $serv;
 7         private $redis;
 8         private $mailTo139;
 9         private $client;
10 
11         public function __construct()
12         {
13                 $this->serv = new swoole_server('0.0.0.0', 9501);
14                 $this->serv->set([
15                         'worker_num' => 1,
16                         'task_worker_num' => 12,
17                         // 'dispatch_mode' => 2,
18                 ]);
19 
20                 $this->serv->on('receive',[$this,'onReceive']);
21                 $this->serv->on('connect',[$this,'onConnect']);
22                 $this->serv->on('workerstart', [$this, 'onWorkerStart']);
23                 $this->serv->on('task', [$this, 'onTask']);
24                 $this->serv->on('finish', [$this, 'onFinish']);
25                 $this->serv->start();
26         }
27 
28 
29         public function onReceive(swoole_server $serv, $fd, $from_id, $data)
30         {
31                 // $client->subscribe('sendmail');
32 
33 
34         }
35         public function onTask(swoole_server $serv, $task_id, $from_id, $data)
36         {
37                 //error_log(print_r($data,1)."/n",3,'/opt/aspire/product/cmdn/apache2/htdocs/sendmail/logs/sendmail_'.date('Ymd').'.log');
38                 $message = explode('|',$data);
39                 if ($message[0] != '1') {
40                 $time1=date('Y-m-d H:i:s');
41                 $resultSendmail = $this->mailTo139->send('这是title',array('email'=>'[email protected]','name'=>'中国移动开发者社区'), array('email'=>$message[0],'name'=>$message[1]), '', '', $message[3]);
42                 $resultToLog = $message[4].' | '.$message[0].' | '.$message[1].' | '.$message[2].' | '.$resultSendmail.' | before:'.$time1.' | after:'.date('Y-m-d H:i:s')."\n";
43                 error_log($resultToLog,3,'/opt/aspire/product/cmdn/apache2/htdocs/sendmail/logs/sendmail_'.date('Ymd').'.log');
44                 //$serv->finish($resultToLog);
45                 }
46         }
47 
48         public function onFinish(swoole_server $serv, $task_id, $msg)
49         {
50                 swoole_async_writefile('/opt/aspire/product/cmdn/apache2/htdocs/sendmail/logs/sendmail_'.date('Ymd').'.log', $file_content = $msg, function($filename) {
51                         //echo '发送邮件中...';
52                 }, $flags = -1);
53         }
54 
55         public function onWorkerStart(swoole_server $serv, $task_id)
56         {
57                 $this->mailTo139 = new AdminCMMail();
58                 if($this->client == null){
59                         if($this->serv->worker_id == 0){
60                                 $redis = new Redis();
61                                 $redis->pconnect('10.101.118.2', 6379);
62                                 $redis->auth('fmtfLq!I:5Ah');
63                                 $redis->subscribe(array('mailUserChannel'), function($instance, $channelName, $result) {
64                                         //echo $channelName;
65                                         //print_r($result);
66                                         $this->serv->task($result);
67                                 });
68                         }
69                 }
70         }
71 
72         public function onMessage(swoole_redis $client, $result)
73         {
74                 $this->serv->task($result);
75         }
76 
77         public function onConnect()
78         {
79                 $this->client->connect("10.101.118.2", 6379, function(swoole_redis $client, $result){
80                         $client->subscribe('mailUserChannel');
81                 });
82         }
83 
84 
85 }
86 $server = new serverEmail();
87 ?>

猜你喜欢

转载自www.cnblogs.com/hnhycnlc888/p/9780839.html