|
|
@@ -0,0 +1,114 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: Administrator
|
|
|
+ * Date: 2019/7/25
|
|
|
+ * Time: 14:24
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+use \GatewayWorker\Lib\Gateway;
|
|
|
+use Workerman\Lib\Timer;
|
|
|
+
|
|
|
+class Mlogic
|
|
|
+{
|
|
|
+ private static $Instance = null;
|
|
|
+ private static $db = null;
|
|
|
+ private static $redis = null;
|
|
|
+ private static $redisTime = null;
|
|
|
+ public static $global = null;
|
|
|
+
|
|
|
+ public static function GetInstance()
|
|
|
+ {
|
|
|
+ if (!empty(self::$Instance)) {
|
|
|
+ return self::$Instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ self::$Instance = new self();
|
|
|
+ self::$Instance->Init();
|
|
|
+
|
|
|
+ return self::$Instance;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static function Init()
|
|
|
+ {
|
|
|
+ self::getDb();
|
|
|
+ self::getRedis();
|
|
|
+ self::getGlbData();
|
|
|
+ }
|
|
|
+
|
|
|
+ //workman 的共享数据插件
|
|
|
+ public static function getGlbData()
|
|
|
+ {
|
|
|
+ if (empty(self::$global)) {
|
|
|
+ self::$global = new \GlobalData\Client('127.0.0.1:2207');
|
|
|
+ }
|
|
|
+ return self::$global;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function getDb()
|
|
|
+ {
|
|
|
+ if (empty(self::$db)) {
|
|
|
+ $mds = DIRECTORY_SEPARATOR;
|
|
|
+ if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
|
|
|
+ $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'database.php';
|
|
|
+ } else {
|
|
|
+ $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'database.php';
|
|
|
+ }
|
|
|
+ $conf = require($dbcfg);
|
|
|
+ self::$db = new \Workerman\MySQL\Connection($conf['hostname'], $conf['hostport'], $conf['username'], $conf['password'], $conf['database']);
|
|
|
+ }
|
|
|
+ return self::$db;
|
|
|
+ }
|
|
|
+
|
|
|
+ //实例化redis
|
|
|
+ public static function getRedis($force = 0)
|
|
|
+ {
|
|
|
+ if (self::$redis && !$force) {
|
|
|
+ return self::$redis;
|
|
|
+ }
|
|
|
+
|
|
|
+ $mds = DIRECTORY_SEPARATOR;
|
|
|
+ if (strtolower(substr(PHP_OS, 0, 3)) == 'win') {
|
|
|
+ $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'redis.php';
|
|
|
+ } else {
|
|
|
+ $dbcfg = realpath(dirname(__FILE__) . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..' . $mds . '..') . $mds . 'application' . $mds . 'redis.php';
|
|
|
+ }
|
|
|
+ $conf = require($dbcfg);
|
|
|
+ $redis = new \Redis();
|
|
|
+ $ret = $redis->connect($conf['host'], $conf['port']);
|
|
|
+ if (!$ret) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ if (empty($conf['passwd'])) {
|
|
|
+ $ret = $redis->auth($conf['passwd']);
|
|
|
+ if (!$ret) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $redis->select($conf['db']);
|
|
|
+ self::$redisTime = time();
|
|
|
+
|
|
|
+ self::$redis = $redis;
|
|
|
+ return self::$redis;
|
|
|
+ }
|
|
|
+
|
|
|
+ //redis ping ,看是否断线
|
|
|
+ public static function RedisPing()
|
|
|
+ {
|
|
|
+ $ret = self::$redis->ping();
|
|
|
+ if (strpos("PONG", strtolower($ret)) !== false) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //发送消息的代理
|
|
|
+ public static function MySendMsg($clientId, $msg)
|
|
|
+ {
|
|
|
+ Gateway::sendToClient($clientId, $msg);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|