<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ``` 使用的寶塔 需要去掉的禁用函數可能有 pcntl_alarm pcntl_fork putenv pcntl_signal pcntl_wait stream_socket_server pcntl_signal_dispatch 開放端口 /sbin/iptables -I INPUT -p tcp --dport 2120 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 2121 -j ACCEPT /sbin/iptables -I INPUT -p tcp --dport 2123 -j ACCEPT ``` 只需要開啟 start_io.php ``` <?php use Workerman\Worker; use Workerman\Timer; use PHPSocketIO\SocketIO; use Workerman\Protocols\Http\Request; use Workerman\Connection\TcpConnection; include __DIR__ . '/vendor/autoload.php'; // 全局數組保存uid在線數據 $uidConnectionMap = array(); // 記錄最后一次廣播的在線用戶數 $last_online_count = 0; // 記錄最后一次廣播的在線頁面數 $last_online_page_count = 0; // 這個是設置https的 如果不用設置就這樣 //$context = array( // 'ssl' => array( // 'local_cert' => '/www/server/panel/vhost/ssl/test.erhas.cn/fullchain.pem', // pem 文件一樣的 // 'local_pk' => '/www/server/panel/vhost/ssl/test.erhas.cn/privkey.pem', // 'verify_peer' => false, // ) //); // PHPSocketIO服務 //$sender_io = new SocketIO(2120,$context); $sender_io = new SocketIO(2120); // 客戶端發起連接事件時,設置連接socket的各種事件回調 $sender_io->on('connection', function($socket){ // 當客戶端發來登錄事件時觸發 $socket->on('login', function ($uid)use($socket){ global $uidConnectionMap, $last_online_count, $last_online_page_count; // 已經登錄過了 if(isset($socket->uid)){ return; } // 更新對應uid的在線數據 $uid = (string)$uid; if(!isset($uidConnectionMap[$uid])) { $uidConnectionMap[$uid] = 0; } // 這個uid有++$uidConnectionMap[$uid]個socket連接 ++$uidConnectionMap[$uid]; // 將這個連接加入到uid分組,方便針對uid推送數據 $socket->join($uid); $socket->uid = $uid; // 更新這個socket對應頁面的在線數據 共打開<b>{$last_online_page_count}</b>個頁面 $socket->emit('update_online_count', "當前{$last_online_count}人在線"); }); // 當客戶端斷開連接是觸發(一般是關閉網頁或者跳轉刷新導致) $socket->on('disconnect', function () use($socket) { if(!isset($socket->uid)) { return; } global $uidConnectionMap, $sender_io; // 將uid的在線socket數減一 if(--$uidConnectionMap[$socket->uid] <= 0) { unset($uidConnectionMap[$socket->uid]); } }); }); // 當$sender_io啟動后監聽一個http端口,通過這個端口可以給任意uid或者所有uid推送數據 $sender_io->on('workerStart', function(){ // 監聽一個http端口 // $context = array( // 'ssl' => array( // 'local_cert' => '/www/server/panel/vhost/ssl/test.erhas.cn/fullchain.pem', // pem 文件一樣的 // 'local_pk' => '/www/server/panel/vhost/ssl/test.erhas.cn/privkey.pem', // 'verify_peer' => false, // ) // ); // $inner_http_worker = new Worker('https://0.0.0.0:2121',$context); $inner_http_worker = new Worker('http://192.168.1.185:2121'); // 當http客戶端發來數據時觸發 $inner_http_worker->onMessage = function(TcpConnection $http_connection, Request $request){ global $uidConnectionMap; $post = $request->post(); $post = $post ? $post : $request->get(); // 推送數據的url格式 type=publish&to=uid&content=xxxx switch(@$post['type']){ case 'publish': global $sender_io; $to = @$post['to']; $post['content'] = htmlspecialchars(@$post['content']); // 有指定uid則向uid所在socket組發送數據 if($to){ $sender_io->to($to)->emit('new_msg', $post['content']); // 否則向所有uid推送數據 }else{ $sender_io->emit('new_msg', @$post['content']); } // http接口返回,如果用戶離線socket返回fail if($to && !isset($uidConnectionMap[$to])){ return $http_connection->send('offline'); }else{ return $http_connection->send('ok'); } } return $http_connection->send('fail'); }; // 執行監聽 $inner_http_worker->listen(); // 一個定時器,定時向所有uid推送當前uid在線數及在線頁面數 Timer::add(1, function(){ global $uidConnectionMap, $sender_io, $last_online_count, $last_online_page_count; $online_count_now = count($uidConnectionMap); $online_page_count_now = array_sum($uidConnectionMap); // 只有在客戶端在線數變化了才廣播,減少不必要的客戶端通訊 if($last_online_count != $online_count_now || $last_online_page_count != $online_page_count_now) { $sender_io->emit('update_online_count', "當前<b>{$online_count_now}</b>人在線,共打開<b>{$online_page_count_now}</b>個頁面"); $last_online_count = $online_count_now; $last_online_page_count = $online_page_count_now; } }); }); if(!defined('GLOBAL_START')) { Worker::runAll(); } ``` ``` socket() { let that = this // const socket = io('https://127.0.0.1:2120') // 這里當然填寫真實的地址了 const socket = io('http://127.0.0.1:2120') // 這里當然填寫真實的地址了 // uid可以是自己網站的用戶id,以便針對uid推送以及統計在線人數 let userInfoId=JSON.parse(window.localStorage.getItem('userInfoId')).data; let id=userInfoId ? userInfoId : that.$store.getters.userInfo.id // 用戶ID const uid = id // socket連接后以uid登錄 socket.on('connect', function() { socket.emit('login', uid) console.log('已連接,用戶00' + uid) }) // 后端推送來消息時 socket.on('new_msg', function(msg) { console.log('收到消息:' + msg) that.$message.success({ message: msg, showClose:true }); }) // 后端推送來在線數據時 socket.on('update_online_count', function(online_stat) { // console.log(online_stat) }) }, ```
                  <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>

                              哎呀哎呀视频在线观看