-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths2.php
86 lines (76 loc) · 2.91 KB
/
s2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
class Server {
private $serv;
/*
$serv = new swoole_server("0.0.0.0", 9501);
$serv->on('connect', function ($serv, $fd){
echo "Client:Connect.\n";
});
$serv->on('receive', function ($serv, $fd, $from_id, $data) {
$serv->send($fd, 'Swoole: '.$data);
});
$serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$serv->start(); */
public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 1, //一般设置为服务器CPU数的1-4倍
'daemonize' => 0, //1以守护进程执行,0调试用,在命令行输出
'max_request' => 10000,
'dispatch_mode' => 2,
'task_worker_num' => 1, //task进程的数量
"task_ipc_mode " => 3, //使用消息队列通信,并设置为争抢模式
"heartbeat_check_interval"=>60,//心跳检查自动close掉长时间没有数据来往的连接
"heartbeat_idle_time"=>600,
"log_file" => "log/taskqueueu.log" ,//日志
));
$this->serv->on('Receive', array($this, 'onReceive'));
// bind callback
$this->serv->on('Task', array($this, 'onTask'));
$this->serv->on('Finish', array($this, 'onFinish'));
$this->serv->on('connect', function ($serv, $fd){
echo "Client:Connect.\n";
});
$this->serv->on('close', function ($serv, $fd) {
echo "Client: Close.\n";
});
$this->serv->start();
}
public function onReceive(swoole_server $serv, $fd, $from_id, $data)
{
//echo "Get Message From Client {$fd}:{$data}\n";
// send a task to task worker.
$serv->task($data);
}
public function onTask($serv, $task_id, $from_id, $data)
{
$array = json_decode($data, true);
if ($array['url']) {
echo "onTask----go run {$task_id} \n";
return $this->httpGet($array['url'], $array['param']);
}
}
public function onFinish($serv, $task_id, $data)
{
//echo "Task {$task_id} finish\n";
//echo "Result: {$data}\n";
}
protected function httpGet($url, $data)
{
if ($data) {
$url .= '?' . http_build_query($data);
}
$curlObj = curl_init(); //初始化curl,
curl_setopt($curlObj, CURLOPT_URL, $url); //设置网址
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1); //将curl_exec的结果返回
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curlObj, CURLOPT_HEADER, 0); //是否输出返回头信息
$response = curl_exec($curlObj); //执行
curl_close($curlObj); //关闭会话
return $response;
}
}
$server = new Server();