|
|
|
@ -41,38 +41,39 @@ class Events |
|
|
|
'type' => 'init', |
|
|
|
'client_id' => $client_id |
|
|
|
))); |
|
|
|
self::onlineStatistics(); |
|
|
|
} |
|
|
|
/** |
|
|
|
* 有消息时 |
|
|
|
* @param int $client_id |
|
|
|
* @param mixed $message |
|
|
|
*/ |
|
|
|
public static function onMessage($client_id, $message) |
|
|
|
{ |
|
|
|
* 有消息时 |
|
|
|
* @param int $client_id |
|
|
|
* @param mixed $message |
|
|
|
*/ |
|
|
|
public static function onMessage($client_id, $message) |
|
|
|
{ |
|
|
|
// 客户端传递的是json数据 |
|
|
|
$message_data = json_decode($message, true); |
|
|
|
if(!$message_data) |
|
|
|
{ |
|
|
|
return ; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 根据类型执行不同的业务 |
|
|
|
switch($message_data['type']) |
|
|
|
{ |
|
|
|
// 客户端回应服务端的心跳 |
|
|
|
case 'pong': |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'ping': |
|
|
|
self::sendStatus($client_id); |
|
|
|
break; |
|
|
|
break; |
|
|
|
case 'bindUid': |
|
|
|
self::auth($client_id,$message_data); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
protected static function sendStatus($client_id){ |
|
|
|
protected static function sendStatus($client_id){ |
|
|
|
$uid=$_SESSION['user_id'] ?? 0; |
|
|
|
$multiport=false; |
|
|
|
if($uid){ |
|
|
|
@ -85,7 +86,7 @@ class Events |
|
|
|
'type' => 'pong', |
|
|
|
'multiport' => $multiport, |
|
|
|
))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//验证用户的真实性并绑定 |
|
|
|
protected static function auth($client_id, $msg){ |
|
|
|
@ -113,30 +114,47 @@ class Events |
|
|
|
self::closeClient($client_id); |
|
|
|
} |
|
|
|
$_SESSION['user_id']=$userInfo['user_id']; |
|
|
|
|
|
|
|
self::sendStatus($client_id); |
|
|
|
} |
|
|
|
|
|
|
|
//断开连接 |
|
|
|
protected static function closeClient($client_id){ |
|
|
|
self::onlineStatistics(); |
|
|
|
$_SESSION['user_id']=null; |
|
|
|
Gateway::closeClient($client_id); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 当断开连接时 |
|
|
|
* @param int $client_id |
|
|
|
*/ |
|
|
|
* 当断开连接时 |
|
|
|
* @param int $client_id |
|
|
|
*/ |
|
|
|
public static function onClose($client_id) |
|
|
|
{ |
|
|
|
$user_id=$_SESSION['user_id']; |
|
|
|
if($user_id){ |
|
|
|
Gateway::sendToAll(json_encode(array( |
|
|
|
'type' => 'isOnline', |
|
|
|
'time' => time(), |
|
|
|
'data' => ['id'=>$user_id,'is_online'=>0] |
|
|
|
))); |
|
|
|
'type' => 'isOnline', |
|
|
|
'time' => time(), |
|
|
|
'data' => ['id'=>$user_id,'is_online'=>0] |
|
|
|
))); |
|
|
|
} |
|
|
|
|
|
|
|
self::onlineStatistics(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static function onlineStatistics() |
|
|
|
{ |
|
|
|
// 通知后台在线用户数和在线设备数 |
|
|
|
$data=[ |
|
|
|
'type' => 'statistics', |
|
|
|
'time' => time(), |
|
|
|
'data' => [ |
|
|
|
'onlineCount'=>Gateway::getAllUidCount() ?? 0, |
|
|
|
'clientCount'=>Gateway::getAllClientCount() ?? 0, |
|
|
|
] |
|
|
|
]; |
|
|
|
Gateway::sendToGroup('admin-manage', json_encode($data)); |
|
|
|
Gateway::sendToUid(1, json_encode($data)); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|