|
|
@@ -9,6 +9,7 @@ namespace app\logic;
|
|
|
* Time: 11:22
|
|
|
*/
|
|
|
|
|
|
+use app\lib\DataPack;
|
|
|
use app\lib\GlobConfigs;
|
|
|
use Illuminate\Database\Capsule\Manager as Capsule;
|
|
|
use Illuminate\Database\Capsule\Manager as DB;
|
|
|
@@ -22,7 +23,7 @@ class MyServerV2
|
|
|
//线程连接实例
|
|
|
private $workRedis = null;
|
|
|
private $workMysql = null;
|
|
|
- private $workPgsql = null ;
|
|
|
+ private $workPgsql = null;
|
|
|
|
|
|
public function __construct()
|
|
|
{
|
|
|
@@ -78,17 +79,19 @@ class MyServerV2
|
|
|
MyPgsql::getInstance();
|
|
|
|
|
|
//消息订阅
|
|
|
- if ($worker_id == 0){
|
|
|
+ if ($worker_id == 0) {
|
|
|
|
|
|
- if ( !$worker_id ){ return ; }
|
|
|
+ if (!$worker_id) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- go(function() use ($serv,$worker_id){
|
|
|
+ go(function () use ($serv, $worker_id) {
|
|
|
$config = GlobConfigs::getKey('redis');
|
|
|
$redis = new \Swoole\Coroutine\Redis();
|
|
|
$redis->setOptions(['compatibility_mode' => true]);
|
|
|
$redis->connect($config['host'], $config['port']);
|
|
|
- $r = $redis->SUBSCRIBE([MSG_REDIS_SUBSCRIBE,'TEST']);
|
|
|
- if ($r){
|
|
|
+ $r = $redis->SUBSCRIBE([MSG_REDIS_SUBSCRIBE, 'TEST']);
|
|
|
+ if ($r) {
|
|
|
while ($msg = $redis->recv()) {
|
|
|
print_r($msg);
|
|
|
// msg是一个数组, 包含以下信息
|
|
|
@@ -99,12 +102,10 @@ class MyServerV2
|
|
|
if ($type == 'subscribe') // 或psubscribe
|
|
|
{
|
|
|
// 频道订阅成功消息,订阅几个频道就有几条
|
|
|
- }
|
|
|
- else if ($type == 'unsubscribe' && $info == 0) // 或punsubscribe
|
|
|
+ } else if ($type == 'unsubscribe' && $info == 0) // 或punsubscribe
|
|
|
{
|
|
|
break; // 收到取消订阅消息,并且剩余订阅的频道数为0,不再接收,结束循环
|
|
|
- }
|
|
|
- else if ($type == 'message') // 若为psubscribe,此处为pmessage
|
|
|
+ } else if ($type == 'message') // 若为psubscribe,此处为pmessage
|
|
|
{
|
|
|
/*
|
|
|
if ($need_unsubscribe) // 某个情况下需要退订
|
|
|
@@ -136,6 +137,10 @@ class MyServerV2
|
|
|
|
|
|
public function onMessage($serv, $frame)
|
|
|
{
|
|
|
+ if ( strtolower($frame->data->data) == '{"type":"ping"}'){
|
|
|
+ $serv->send($frame->fd,'{"type":"pong"}');
|
|
|
+ return ;
|
|
|
+ }
|
|
|
Wlog::getInstance()->WriteLog($frame);
|
|
|
$serv->task($frame);
|
|
|
}
|
|
|
@@ -144,28 +149,28 @@ class MyServerV2
|
|
|
{
|
|
|
|
|
|
$token = isset($request->get['token']) ? $request->get['token'] : '';
|
|
|
- $uid = isset($request->get['uid']) ? $request->get['uid'] : '';
|
|
|
+ //$uid = isset($request->get['uid']) ? $request->get['uid'] : '';
|
|
|
$fd = $request->fd;
|
|
|
$redis = $this->workRedis;
|
|
|
if (!$redis) {
|
|
|
return;
|
|
|
}
|
|
|
+ $token_uid = $uid = intval($this->workRedis->hget(MAPS_TOKEN_UID, md5($token)));
|
|
|
+ if (empty($token) || !$token_uid ) {
|
|
|
+ $serv->push($request->fd, DataPack::toJson(['mtype' => 'system_msg', 'stype' => 'invalid_token', 'data' => ['msg' => '无效的token']]));
|
|
|
+ $serv->disconnect($request->fd);
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- $redis->hset(ON_OPEN, $fd, json_encode(['fd' => $fd, 'token' => $token, 'uid' => $uid], JSON_UNESCAPED_UNICODE));
|
|
|
- $str = '你好 ! fd=' . $fd . ' ,ip=' . $request->server['remote_addr'] . ' ,port=' . $request->server['remote_port'] . ' ,time=' . date('Y-m-d H:i:s');
|
|
|
-
|
|
|
- echo $str ."\n" ;
|
|
|
-
|
|
|
- $serv->push($fd, $str);
|
|
|
+ $oldfid = $this->workRedis->hget(MAPS_UID_FID, $uid);
|
|
|
+ if ($oldfid != '' && $oldfid != $fd && $serv->exist($oldfid)) {
|
|
|
+ $serv->push($oldfid, DataPack::toJson(['mtype' => 'system_msg', 'stype' => 'force_logout', 'data' => [ 'msg' => '你已在其它地方登陆,本次退出!']]));
|
|
|
+ $serv->disconnect($oldfid);
|
|
|
+ }
|
|
|
|
|
|
- /*
|
|
|
- $serv->after(120000,function() use($serv,$fd,$redis){
|
|
|
- if ($serv->exist($fd)){
|
|
|
- $serv->disconnect($fd,1000,json_encode('未验证用户关闭',JSON_UNESCAPED_UNICODE));
|
|
|
- $redis->hdel('onOpen',$fd);
|
|
|
- }
|
|
|
- });
|
|
|
- */
|
|
|
+ $this->workRedis->hset(MAPS_UID_FID, $uid, $fd);
|
|
|
+ $this->workRedis->hset(MAPS_FID_UID, $fd, $uid);
|
|
|
+ $serv->push($fd, DataPack::toJson(['mtype' => 'system_msg', 'stype' => 'well_come', 'data' => [ 'msg' => '成功接入']]));
|
|
|
}
|
|
|
|
|
|
public function onTask($serv, \Swoole\Server\Task $task)
|
|
|
@@ -174,6 +179,7 @@ class MyServerV2
|
|
|
Wlog::getInstance()->WriteLog($task, 1, $serv->worker_id);
|
|
|
CmdProxy::getInstance()->ParaCMD($serv, $task);
|
|
|
} catch (\Exception $e) {
|
|
|
+ Wlog::getInstance()->WriteLog(['onTask error:',$task,$e->getCode() . ' ' . $e->getMessage()], 3, $serv->worker_id);
|
|
|
echo "发生异常." . $e->getCode() . ' ' . $e->getMessage() . "\n";
|
|
|
}
|
|
|
}
|
|
|
@@ -184,8 +190,10 @@ class MyServerV2
|
|
|
|
|
|
public function onClose($serv, $fd, $from_id)
|
|
|
{
|
|
|
- $this->workRedis->hdel(ON_OPEN, $fd);
|
|
|
- echo "Client {$fd} close connection!\n";
|
|
|
+ $uid = $this->workRedis->hget("MAPS_FID_UID", $fd);
|
|
|
+ $this->workRedis->hdel(MAPS_UID_FID, $uid);
|
|
|
+ $this->workRedis->hdel(MAPS_FID_UID, $fd);
|
|
|
+ echo "ClientFd:{$fd} -- uid:{$uid} close connection!\n";
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -269,7 +277,6 @@ class MyServerV2
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
//Mysql Ping检测
|
|
|
private function PingPgsql($serv, $worker_id)
|
|
|
{
|
|
|
@@ -311,14 +318,17 @@ class MyServerV2
|
|
|
private function startInit()
|
|
|
{
|
|
|
$conf = GlobConfigs::getKey('redis');
|
|
|
+ $adminconf = GlobConfigs::getKey('admin_conf');
|
|
|
$redis = new \Redis();
|
|
|
$ret = $redis->connect($conf['host'], $conf['port'], $conf['overtime']);
|
|
|
if ($ret) {
|
|
|
$redis->pipeline();
|
|
|
$redis->del(MAPS_UID_FID);
|
|
|
$redis->del(MAPS_FID_UID);
|
|
|
- $redis->del(MAPS_USER_TOKEN);
|
|
|
- $redis->del(ON_OPEN);
|
|
|
+ //$redis->del(MAPS_TOKEN_UID);
|
|
|
+ //$redis->del(MAPS_UID_TOKEN);
|
|
|
+ $redis->hset(MAPS_TOKEN_UID,$adminconf['admin_uid'],$adminconf['md5']);
|
|
|
+ $redis->hset(MAPS_UID_TOKEN,$adminconf['md5'],$adminconf['admin_uid']);
|
|
|
$redis->exec();
|
|
|
$redis->close();
|
|
|
}
|