HttpMqServer.php
1.45 KB
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
<?php
require_once __DIR__.'/Sender.php';
$serv = new swoole_http_server("0.0.0.0", $Config->http_mq_port);
$serv->set(array(
'worker_num' => $Config->worker_num,
'daemonize' => $Config->daemonize, //是否作为守护进程
));
if ($Config->log_file){
$serv->set(array(
'log_file' => $Config->log_file,
));
}
$serv->on('start', function (swoole_server $serv)
{
echo "Client:Start.\n";
});
$serv->on('Request', function(swoole_http_request $request, swoole_http_response $response)
{
$response->end();
$ip = $request->server['remote_addr'];
$get = !empty($request->get) ? $request->get : array();
$post = !empty($request->post) ? $request->post : array();
$ouput = "HTTP MQ Request--\n";
$ouput .= sprintf("Receive request fd: %d, ip: %s, method: %s\n", $request->fd, $ip, $request->server['request_method']);
$ouput .= date('Y-m-d H:i:s')."\n";
$ouput .= "Request query : \n";
$ouput .= $get ? json_encode($get)."\n" : '';
$ouput .= $post ? json_encode($post)."\n" : '';
$ouput .= "\r\n\r\n";
Sender::log($ouput);
$data = $get + $post;
$tags = array(
'ip'=>$ip,
'hostname'=>!empty($data['hostname']) ? $data['hostname'] : gethostbyaddr($ip),
);
$type = !empty($data['type']) ? $data['type'] : 'info';
Sender::pushmq($type,$data,$tags);
});
$serv->on('close', function (swoole_server $serv, $fd)
{
echo "Client: Close.\n";
});
$serv->start();