<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 一、業務代碼 ``` <?php include "SwooleMySQL.php"; use frontend\controllers\SwooleMySQL; use function Swoole\Coroutine\run; //創建WebSocket Server對象,監聽0.0.0.0:9502端口。 $ws = new Swoole\WebSocket\Server('0.0.0.0', 9502); //監聽WebSocket連接打開事件。 $ws->on('Open', function ($ws, $request) { $ws->push($request->fd, "websocket鏈接成功\n"); }); //監聽WebSocket消息事件。 $ws->on('Message', function ($ws, $frame) { $data = json_decode($frame->data, true); if (!empty($data["user_id"]) && !empty($data["shop_id"]) && $data["middlebox_id"] ){ $userId = $data["user_id"]; $shopId = $data["shop_id"]; $boxId = $data["middlebox_id"]; $swooleMysqlClient = new SwooleMySQL(); $sql = "SELECT * FROM shop_items_middlebox_queue WHERE user_id = $userId AND shop_id = $shopId AND middlebox_id = $boxId AND opened_at < time() AND closed_at > time()"; $shop = $swooleMysqlClient->query($sql); if ($shop) { $res =[ "code"=>200, "msg"=>"排隊時間已到,請您盡快前去抽盒" ]; $result = json_encode($res); $ws->push($frame->fd,$result); } } $ws->push($frame->fd,"ok"); }); //監聽WebSocket連接關閉事件。 $ws->on('Close', function ($ws, $fd) { echo "client-{$fd} is closed\n"; }); $ws->start(); ``` ## 二、鏈接數據庫 ``` <?php namespace frontend\controllers; use Swoole; class SwooleMySQL { private $pool; public function __construct() { $this->pool = new Swoole\Coroutine\MySQL(); $this->pool->connect([ 'host' => '101.200.179.32', 'port' => 3306, 'user' => 'mancool_test', 'password' => 'DGaJGyaG7ehisTx8', 'database' => 'mancool_test', ]); } public function query($sql) { return $this->pool->query($sql); } } ``` ## 三、執行 ``` php WebSocket.php ``` ## 四、默認訪問 默認訪問 127.0.0.1:9502 (域名和端口填寫自己的) ## 五、配置反向代理(nginx 反向代理 wss) ``` location /websocket { # WebSocket代理配置 proxy_pass http://127.0.0.1:9502; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $host; # 如果你希望保留客戶端的IP地址,可以傳遞以下頭部 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; # 禁用緩存和緩沖,這對于WebSocket連接很重要 proxy_buffering off; proxy_cache_bypass $http_upgrade; # 設置超時時間 proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; send_timeout 60s; } ``` 現在你的訪問為:域名/websocket ## 六、其它操作 [鏈接 ](http://www.hmoore.net/lengyueguang/linux/2027120)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看