国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

gateway中使用mysql

程序gg了

gateway使用mysql示例:http://doc2.workerman.net/mysql.html
我想在start_gateway.php中查詢數(shù)據(jù)庫,然后繼續(xù)在events.php中使用要怎么做 ?

start_gateway.php我現(xiàn)在是使用file_get_content去GET查詢數(shù)據(jù)庫。

<?php 
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link http://wtbis.cn/
 * @license http://www.opensource.org/licenses/mit-license.php MIT License
 */
use \Workerman\Worker;
use \Workerman\WebServer;
use \GatewayWorker\Gateway;
use \GatewayWorker\BusinessWorker;
use \Workerman\Autoloader;

// 自動加載類
require_once __DIR__ . '/../../vendor/autoload.php';

// gateway 進程,這里使用Text協(xié)議,可以用telnet測試
// $gateway = new Gateway("tcp://0.0.0.0:8282");
$gateway = new Gateway("websocket://0.0.0.0:2000");
// gateway名稱,status方便查看
$gateway->name = 'Device';
// gateway進程數(shù)
$gateway->count = 1;
// 本機ip,分布式部署時使用內(nèi)網(wǎng)ip
$gateway->lanIp = '127.0.0.1';
// 內(nèi)部通訊起始端口,假如$gateway->count=4,起始端口為4000
// 則一般會使用4000 4001 4002 4003 4個端口作為內(nèi)部通訊端口 
$gateway->startPort = 2900;
// 服務(wù)注冊地址
$gateway->registerAddress = '127.0.0.1:1238';

// 心跳間隔
$gateway->pingInterval = 10;
$gateway->pingNotResponseLimit = 1;
// 心跳數(shù)據(jù)
//$gateway->pingData = '{"type":"ping"}';
$gateway->pingData = '';

// 當(dāng)客戶端連接上來時,設(shè)置連接的onWebSocketConnect,即在websocket握手時的回調(diào)
$gateway->onConnect = function($connection)
{
    $connection->onWebSocketConnect = function($connection , $http_header)
    {
        // $_SERVER 可用這里判斷連接來源是否合法,不合法就關(guān)掉連接
        try
        {
            $url = 'http://ws.lzkdkeji.com/index/auth/index?data='.$_SERVER['REQUEST_URI'];
            $res = file_get_contents($url);
            if ( empty($res) ) throw new Exception('鑒權(quán)失敗');
            $res = json_decode($res, TRUE);
            if ($res['code'] != 'success') throw new Exception($res['msg']);

            $_SESSION['device_data'] = $res['device_data'];
        }
        catch (\Exception $e)
        {
            echo $e->getMessage();
            $connection->close();
        }
    };
}; 

// 如果不是在根目錄啟動,則運行runAll方法
if(!defined('GLOBAL_START'))
{
    Worker::runAll();
}

怎么在start_gateway.php中使用mysql 并且不影響Events.php

<?php
use \GatewayWorker\Lib\Gateway;
require_once '/your/path/of/mysql-master/src/Connection.php';

/**
 * 數(shù)據(jù)庫示例,假設(shè)有個your_db_name庫,里面有個user表
 */
class Events
{
    /**
     * 新建一個類的靜態(tài)成員,用來保存數(shù)據(jù)庫實例
     */
    public static $db = null;

    /**
     * 進程啟動后初始化數(shù)據(jù)庫連接
     */
    public static function onWorkerStart($worker)
    {
        self::$db = new \Workerman\MySQL\Connection('host', 'port', 'user', 'password', 'db_name');
    }

   /**
    * 有消息時觸發(fā)該方法,根據(jù)發(fā)來的命令打印2個用戶信息
    * @param int $client_id 發(fā)消息的client_id
    * @param mixed $message 消息
    * @return void
    */
   public static function onMessage($client_id, $message)
   {
        // 發(fā)來的消息
        $commend = trim($message);
        if($commend !== 'get_user_list')
        {
            Gateway::sendToClient($client_id, "unknown commend\n");
            return;
        }
        // 使用數(shù)據(jù)庫實例
        self::$db->select('*')->from('users')->where('uid>3')->offset(5)->limit(2)->query();
        // 打印結(jié)果
        return Gateway::sendToClient($client_id, var_export($ret, true));
   }

}
3835 2 0
2個回答

six

我覺得最好不要在onConnect里做外部訪問這些耗時的操作,連接量大了程序會越來越慢,萬一訪問外部資源卡了那么幾秒,所有客戶端跟著卡。你要鑒權(quán)不一定非要讀數(shù)據(jù)庫,客戶端傳遞個token,onConnect里根據(jù)一定算法驗證token是否合法就行了。

比如客戶端后臺生成token的算法

function token($uid) {
    $密鑰 = 's#8id%2*!';
    $time = time();
    return 'token='.md5($密鑰.$uid.$time)."&time=$time"&uid=$uid;
}

js連的時候 把token串帶上 ws = new WebSocket('ws://example.com?{$token}');

Gateway端驗證

$gateway->onConnect = function($connection)
{
    $connection->onWebSocketConnect = function($connection , $http_header)
    {
        $time = $_GET['time'];
        $uid = $_GET['uid'];
        $token = $_GET['token];
        $密鑰='s#8id%2*!';
        // token 過60秒就失效,防止被竊取重復(fù)使用
        if (abs(time() - $time) < 60) return $connection->close();
        // token不合法就關(guān)閉連接
        if ($token != md5($密鑰.$uid.$time)) {
            return $connection->close();
        }
        // 合法的話記錄個uid的$_SESSION標(biāo)記當(dāng)前是誰,在Events.php可能用到
        $_SESSION['uid'] = $uid;
    }
}; 

密鑰不要泄漏就行了

  • 程序gg了 2020-04-25

    我必須去數(shù)據(jù)庫里面查詢以下鏈接設(shè)備得密鑰

  • six 2020-04-25

    上面密鑰是一個公用的密鑰,沒必要去查。類似一個配置

程序gg了

求大佬解答下

  • 暫無評論
年代過于久遠,無法發(fā)表回答
??