改进Socket驱动

This commit is contained in:
thinkphp
2016-05-20 10:28:45 +08:00
parent 6c294b58fc
commit 97e12c1a8a

View File

@@ -40,7 +40,7 @@ class Socket
'big' => 'font-size:20px;color:red;', 'big' => 'font-size:20px;color:red;',
]; ];
protected $_allowForceClientIds = []; //配置强制推送且被授权的client_id protected $allowForceClientIds = []; //配置强制推送且被授权的client_id
/** /**
* 架构函数 * 架构函数
@@ -52,10 +52,6 @@ class Socket
if (!empty($config)) { if (!empty($config)) {
$this->config = array_merge($this->config, $config); $this->config = array_merge($this->config, $config);
} }
if (isset($this->config['allow_client_id'])) {
//兼容旧配置
$this->allow_client_ids = array_merge($this->allow_client_ids, [$this->config['allow_client_id']]);
}
} }
/** /**
@@ -120,9 +116,9 @@ class Socket
$client_id = ''; $client_id = '';
} }
if (!empty($this->_allowForceClientIds)) { if (!empty($this->allowForceClientIds)) {
//强制推送到多个client_id //强制推送到多个client_id
foreach ($this->_allowForceClientIds as $force_client_id) { foreach ($this->allowForceClientIds as $force_client_id) {
$client_id = $force_client_id; $client_id = $force_client_id;
$this->sendToClient($tabid, $client_id, $logs, $force_client_id); $this->sendToClient($tabid, $client_id, $logs, $force_client_id);
} }
@@ -142,12 +138,12 @@ class Socket
*/ */
protected function sendToClient($tabid, $client_id, $logs, $force_client_id) protected function sendToClient($tabid, $client_id, $logs, $force_client_id)
{ {
$logs = array( $logs = [
'tabid' => $tabid, 'tabid' => $tabid,
'client_id' => $client_id, 'client_id' => $client_id,
'logs' => $logs, 'logs' => $logs,
'force_client_id' => $force_client_id, 'force_client_id' => $force_client_id,
); ];
$msg = @json_encode($logs); $msg = @json_encode($logs);
$address = '/' . $client_id; //将client_id作为地址 server端通过地址判断将日志发布给谁 $address = '/' . $client_id; //将client_id作为地址 server端通过地址判断将日志发布给谁
$this->send($this->config['host'], $msg, $address); $this->send($this->config['host'], $msg, $address);
@@ -167,8 +163,8 @@ class Socket
$allow_client_ids = $this->config['allow_client_ids']; $allow_client_ids = $this->config['allow_client_ids'];
if (!empty($allow_client_ids)) { if (!empty($allow_client_ids)) {
//通过数组交集得出授权强制推送的client_id //通过数组交集得出授权强制推送的client_id
$this->_allowForceClientIds = array_intersect($allow_client_ids, $this->config['force_client_ids']); $this->allowForceClientIds = array_intersect($allow_client_ids, $this->config['force_client_ids']);
if (!$tabid && count($this->_allowForceClientIds)) { if (!$tabid && count($this->allowForceClientIds)) {
return true; return true;
} }
@@ -177,7 +173,7 @@ class Socket
return false; return false;
} }
} else { } else {
$this->_allowForceClientIds = $this->config['force_client_ids']; $this->allowForceClientIds = $this->config['force_client_ids'];
} }
return true; return true;
} }