|
|
@@ -53,7 +53,7 @@ class Gateway
|
|
|
* @var bool
|
|
|
*/
|
|
|
public static $persistentConnection = true;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 向所有客户端连接(或者 client_id_array 指定的客户端连接)广播消息
|
|
|
*
|
|
|
@@ -170,7 +170,7 @@ class Gateway
|
|
|
{
|
|
|
return (int)self::getClientIdByUid($uid);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 判断某个客户端连接是否在线
|
|
|
*
|
|
|
@@ -261,7 +261,7 @@ class Gateway
|
|
|
{
|
|
|
return self::getAllClientSessions($group);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取所有连接数
|
|
|
*
|
|
|
@@ -319,10 +319,10 @@ class Gateway
|
|
|
}
|
|
|
return $client_list;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 生成验证包,用于验证此客户端的合法性
|
|
|
- *
|
|
|
+ *
|
|
|
* @return string
|
|
|
*/
|
|
|
protected static function generateAuthBuffer()
|
|
|
@@ -407,16 +407,15 @@ class Gateway
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 踢掉某个客户端,并以$message通知被踢掉客户端
|
|
|
+ * 关闭某个客户端
|
|
|
*
|
|
|
* @param int $client_id
|
|
|
- * @param string $message
|
|
|
* @return bool
|
|
|
*/
|
|
|
- public static function closeClient($client_id, $message = null)
|
|
|
+ public static function closeClient($client_id)
|
|
|
{
|
|
|
if ($client_id === Context::$client_id) {
|
|
|
- return self::closeCurrentClient($message);
|
|
|
+ return self::closeCurrentClient();
|
|
|
} // 不是发给当前用户则使用存储中的地址
|
|
|
else {
|
|
|
$address_data = Context::clientIdToAddress($client_id);
|
|
|
@@ -424,60 +423,22 @@ class Gateway
|
|
|
return false;
|
|
|
}
|
|
|
$address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
|
|
|
- return self::kickAddress($address, $address_data['connection_id'], $message);
|
|
|
+ return self::kickAddress($address, $address_data['connection_id']);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 踢掉当前客户端,并以$message通知被踢掉客户端
|
|
|
+ * 踢掉当前客户端
|
|
|
*
|
|
|
- * @param string $message
|
|
|
* @return bool
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static function closeCurrentClient($message = null)
|
|
|
+ public static function closeCurrentClient()
|
|
|
{
|
|
|
if (!Context::$connection_id) {
|
|
|
throw new Exception('closeCurrentClient can not be called in async context');
|
|
|
}
|
|
|
- $address = long2ip(Context::$local_ip) . ':' . Context::$local_port;
|
|
|
- return self::kickAddress($address, Context::$connection_id, $message);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 踢掉某个客户端并直接立即销毁相关连接
|
|
|
- *
|
|
|
- * @param int $client_id
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- public static function destoryClient($client_id)
|
|
|
- {
|
|
|
- if ($client_id === Context::$client_id) {
|
|
|
- return self::destoryCurrentClient();
|
|
|
- } // 不是发给当前用户则使用存储中的地址
|
|
|
- else {
|
|
|
- $address_data = Context::clientIdToAddress($client_id);
|
|
|
- if (!$address_data) {
|
|
|
- return false;
|
|
|
- }
|
|
|
- $address = long2ip($address_data['local_ip']) . ":{$address_data['local_port']}";
|
|
|
- return self::destroyAddress($address, $address_data['connection_id']);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 踢掉当前客户端并直接立即销毁相关连接
|
|
|
- *
|
|
|
- * @return bool
|
|
|
- * @throws Exception
|
|
|
- */
|
|
|
- public static function destoryCurrentClient()
|
|
|
- {
|
|
|
- if (!Context::$connection_id) {
|
|
|
- throw new Exception('destoryCurrentClient can not be called in async context');
|
|
|
- }
|
|
|
- $address = long2ip(Context::$local_ip) . ':' . Context::$local_port;
|
|
|
- return self::destroyAddress($address, Context::$connection_id);
|
|
|
+ return self::kickAddress(long2ip(Context::$local_ip) . ':' . Context::$local_port, Context::$connection_id);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -634,7 +595,7 @@ class Gateway
|
|
|
}
|
|
|
return self::setSocketSession($client_id, Context::sessionEncode($session));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 更新 session,实际上是与老的session合并
|
|
|
*
|
|
|
@@ -649,12 +610,12 @@ class Gateway
|
|
|
}
|
|
|
return self::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_UPDATE_SESSION, '', Context::sessionEncode($session));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 获取某个client_id的session
|
|
|
*
|
|
|
* @param int $client_id
|
|
|
- * @return mixed false表示出错、null表示用户不存在、array表示具体的session信息
|
|
|
+ * @return mixed false表示出错、null表示用户不存在、array表示具体的session信息
|
|
|
*/
|
|
|
public static function getSession($client_id)
|
|
|
{
|
|
|
@@ -675,7 +636,7 @@ class Gateway
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 向某个用户网关发送命令和消息
|
|
|
+ * 想某个用户网关发送命令和消息
|
|
|
*
|
|
|
* @param int $client_id
|
|
|
* @param int $cmd
|
|
|
@@ -830,27 +791,11 @@ class Gateway
|
|
|
* @param int $connection_id
|
|
|
* @return bool
|
|
|
*/
|
|
|
- protected static function kickAddress($address, $connection_id, $message)
|
|
|
+ protected static function kickAddress($address, $connection_id)
|
|
|
{
|
|
|
$gateway_data = GatewayProtocol::$empty;
|
|
|
$gateway_data['cmd'] = GatewayProtocol::CMD_KICK;
|
|
|
$gateway_data['connection_id'] = $connection_id;
|
|
|
- $gateway_data['body'] = $message;
|
|
|
- return self::sendToGateway($address, $gateway_data);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 销毁某个网关的 socket
|
|
|
- *
|
|
|
- * @param string $address
|
|
|
- * @param int $connection_id
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- protected static function destroyAddress($address, $connection_id)
|
|
|
- {
|
|
|
- $gateway_data = GatewayProtocol::$empty;
|
|
|
- $gateway_data['cmd'] = GatewayProtocol::CMD_DESTROY;
|
|
|
- $gateway_data['connection_id'] = $connection_id;
|
|
|
return self::sendToGateway($address, $gateway_data);
|
|
|
}
|
|
|
|
|
|
@@ -912,6 +857,58 @@ class Gateway
|
|
|
}
|
|
|
return $addresses_cache;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得访客操作系统
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function get_os(){
|
|
|
+ if (!empty($_SESSION['userAgent'])) {
|
|
|
+ $os = $_SESSION['userAgent'];
|
|
|
+ if (preg_match('/win/i', $os)) {
|
|
|
+ $os = 'Windows';
|
|
|
+ } else if (preg_match('/mac/i', $os)) {
|
|
|
+ $os = 'MAC';
|
|
|
+ } else if (preg_match('/linux/i', $os)) {
|
|
|
+ $os = 'Linux';
|
|
|
+ } else if (preg_match('/unix/i', $os)) {
|
|
|
+ $os = 'Unix';
|
|
|
+ } else if (preg_match('/bsd/i', $os)) {
|
|
|
+ $os = 'BSD';
|
|
|
+ } else {
|
|
|
+ $os = 'Other';
|
|
|
+ }
|
|
|
+ return $os;
|
|
|
+ } else {
|
|
|
+ return 'unknow';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获得访问者浏览器
|
|
|
+ * @return string
|
|
|
+ */
|
|
|
+ public static function browse_info(){
|
|
|
+ if (!empty($_SESSION['userAgent'])) {
|
|
|
+ $br = $_SESSION['userAgent'];
|
|
|
+ if (preg_match('/MSIE/i', $br)) {
|
|
|
+ $br = 'MSIE';
|
|
|
+ } else if (preg_match('/Firefox/i', $br)) {
|
|
|
+ $br = 'Firefox';
|
|
|
+ } else if (preg_match('/Chrome/i', $br)) {
|
|
|
+ $br = 'Chrome';
|
|
|
+ } else if (preg_match('/Safari/i', $br)) {
|
|
|
+ $br = 'Safari';
|
|
|
+ } else if (preg_match('/Opera/i', $br)) {
|
|
|
+ $br = 'Opera';
|
|
|
+ } else {
|
|
|
+ $br = 'Other';
|
|
|
+ }
|
|
|
+ return $br;
|
|
|
+ } else {
|
|
|
+ return 'unknow';
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if (!class_exists('\Protocols\GatewayProtocol')) {
|