-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.php
82 lines (80 loc) · 1.65 KB
/
socket.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
<?php
/*
* Simple PHP Sockets client class
*/
class socket{
public $ip = "0.0.0.0";
public $port = 4000;
public $action = array();
public $log = array();
public $response = array();
public $socket;
public $clients = array();
public $pool = array();
function __construct($ip = "0.0.0.0", $port = 4000){
$this->ip = $ip;
$this->port = $port;
}
public function __call($method, $params) {
// check method
if (!is_scalar($method)){
throw new Exception('Method name ('.$method.') is not scalar value');
}
// check params
if(is_array($params)){
$params = array_values($params);
}else{
throw new Exception('Params must be given as array');
}
return @call_user_func_array($method, $params);
}
function __destruct(){
if(is_resource($this->socket)) $this->socket_close($this->socket);
}
function report(){
echo '
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>'.__CLASS__.' report</title>
<style type="text/css">
* {
padding:0;
margin:0;
}
div.log {
width:700px;
padding: 4px 10px 4px 10px;
background-color: #f88;
border: 1px solid #f00;
margin:25px auto;
}
div.log span {
font-family: monospace;
}
</style>
</head>
<body>
'.implode(PHP_EOL, $this->log).'
</body>
</html>';
}
protected function log($reason, $extra, $return = false){
$errMsg = $this->socket_strerror($this->socket_last_error($this->socket));
$log = '
<div class="log">
<h1>'.$reason.'</h1>
<p><strong>Message:</strong>
<span>'.$extra.'</span>
<br>
<span>'.$errMsg.'</span>
</p>
</div>'.PHP_EOL;
if($return == true){
return $log;
}else{
$this->log[] = $log;
}
}
}