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
| public static function request($data){ error_reporting(E_ALL); set_time_limit(0); $host = "127.0.0.1"; $port = 8090; $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 2000, "usec" => 0)); socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array("sec" => 6, "usec" => 0)); $connection = @socket_connect($socket, $host, $port); if(!$connection){ file_put_contents('socket.log', 'cannot connection '.$host.':'.$port.' at '.date('Y-m-d H:i:s')."\r\n",8); return false; } $_data = json_decode($data,true); isset($_SESSION['this_token']) && $_data['token'] = $_SESSION['this_token']; $data = json_encode($_data);
$data .= '^^^'; socket_write($socket, $data,strlen($data)); $res = ''; while ($buff = socket_read($socket,1024)) { $encoding = mb_detect_encoding($buff, array("ASCII",'UTF-8',"GB2312","GBK",'BIG5')); if($encoding=='EUC-CN'){ $buff = iconv('GBK', 'UTF-8', $buff); }
$res .= $buff; if(substr($res,-3)=='^^^'){ socket_close($socket); break; } }
$res = rtrim($res,'^^^'); if($res == 'ipdeny'){ xpexit(json_encode(array('code'=>403,'msg'=>'该IP被禁止访问'))); }
if($_data['command'] != 'login'){ $res_ = json_decode($res,true); if(isset($res_['result'])&&$res_['result']==-2){ distorySession(); xpexit(json_encode(array('code'=>1001,'msg'=>'您已经在其他地方登录过了,即将退出当前页面'))); } }
return $res; }
|