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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                Workerman\Protocols\Websocket ``` // BINARY_TYPE_BLOB Websocket blob type. // BINARY_TYPE_BLOB_DEFLATE Websocket blob type. // BINARY_TYPE_ARRAYBUFFER Websocket arraybuffer type. // BINARY_TYPE_ARRAYBUFFER_DEFLATE Websocket arraybuffer type. // ::input($buffer, ConnectionInterface $connection):int Check the integrity of the package. // ::encode($buffer, ConnectionInterface $connection):string Websocket encode. // ::decode($buffer, ConnectionInterface $connection):strng Websocket decode // ::inflate($connection, $buffer, $is_fin_frame):false|string Inflate // ::deflate($connection, $buffer):false|string Deflate // ::dealHandshake($buffer, $connection):int Websocket handshake. ``` ``` <?php //ini_set('display_errors', 'on'); //運行: php start.php start use Workerman\Worker; use Workerman\Connection\TcpConnection; if(strpos(strtolower(PHP_OS), 'win') === 0) { exit("start.php 入口不支持windows\n"); } // 檢查擴展 if(!extension_loaded('pcntl')) { exit("Please install pcntl extension. See http://doc3.workerman.net/appendices/install-extension.html\n"); } if(!extension_loaded('posix')) { exit("請安裝 posix 擴展. 查看http://doc3.workerman.net/appendices/install-extension.html\n"); } require_once __DIR__ . '/vendor/autoload.php'; $global_uid = 0; // 當客戶端連上來時分配uid,并保存連接,并通知所有客戶端 function handle_connection($connection) { global $text_worker, $global_uid; // 為這個連接分配一個uid $connection->uid = ++$global_uid; foreach($text_worker->connections as $conn) { $conn->send("user_{$connection->uid} connection,IP:".getIP()."!!\n"); } } // 當客戶端發送消息過來時,轉發給所有人 function handle_message(TcpConnection $connection, $data) { global $text_worker; foreach($text_worker->connections as $conn) { $conn->send("user_{$connection->uid} said: $data"); } } // 當客戶端斷開時,廣播給所有客戶端(群發) function handle_close($connection) { global $text_worker; foreach($text_worker->connections as $conn) { $conn->send("user_{$connection->uid} logout"); } } function getIP() { static $realip; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv("HTTP_X_FORWARDED_FOR")){ $realip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv("HTTP_CLIENT_IP")) { $realip = getenv("HTTP_CLIENT_IP"); } else { //如果 PHP 腳本不是通過 HTTP 請求運行的(例如在命令行中運行),$_SERVER['REMOTE_ADDR'] 將不存在。 $realip = getenv("REMOTE_ADDR")??'0.0.0.0'; } } // ip2long($ip_address); // long2ip($proper_address); return $realip; } // 創建一個文本協議的Worker主進程監聽2347接口websocket,如果時前端websockt則必須是websocket,如果telnet 127.0.0.1 2347則可以是其它協議 $text_worker = new Worker("websocket://0.0.0.0:2347"); // 只啟動多個子進程,每個子進程可以和多個客戶端鏈接,這樣方便客戶端之間傳輸數據 //此示例進程數只能選擇1,因為進程間的客戶端是隔離的,無法直接通訊,也就是A進程中無法直接操作B進程的客戶端connection對象發送數據。(要做到這點,需要進程間通訊,比如可以使用Channel組件,例如例子-集群發送、例子-分組發送)。 $text_worker->count = 1; $text_worker->onConnect = 'handle_connection'; $text_worker->onMessage = 'handle_message'; $text_worker->onClose = 'handle_close'; // 每個進程啟動后打印當前進程id編號即 $worker1->id $text_worker->onWorkerStart = function($text_worker) { echo $text_worker->id;//輸出0,1,2,3 }; Worker::runAll(); ``` <!doctype html> <html lang="en"> <head cache-control=''> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>websocket</title> </head> <body> <input id="text" value=""> <input type="submit" value="send" onclick="start()"> <input type="submit" value="close" onclick="close()"> <div id="msg"></div> <script> /** 0:未連接 1:連接成功,可通訊 2:正在關閉 3:連接已關閉或無法打開 */ //創建一個webSocket 實例 var webSocket = new WebSocket("ws://8.137.80.185:2347"); webSocket.onerror = function (event){ console.log(event) onError(event); }; // 打開websocket webSocket.onopen = function (event){ onOpen(event); }; //監聽消息 webSocket.onmessage = function (event){ onMessage(event); }; webSocket.onclose = function (event){ onClose(event); } //關閉監聽websocket function onError(event){ document.getElementById("msg").innerHTML = "<p>close</p>"; console.log("error"+event.data); }; function onOpen(event){ console.log("open:"+sockState()); document.getElementById("msg").innerHTML = "<p>Connect to Service</p>"; }; function onMessage(event){ console.log("onMessage"); console.log(event); document.getElementById("msg").innerHTML += "<p>response:"+event.data+"</p>" }; function onClose(event){ document.getElementById("msg").innerHTML = "<p>close</p>"; console.log("close:"+sockState()); webSocket.close(); } function sockState(){ var status = ['未連接','連接成功,可通訊','正在關閉','連接已關閉或無法打開']; return status[webSocket.readyState]; } function start(event){ console.log(webSocket); var msg = document.getElementById('text').value; document.getElementById('text').value = ''; console.log(sockState()); console.log("發送msg:"+msg); if(webSocket.readyState===0){ return "未連接"; }else if(webSocket.readyState===1){ webSocket.send("發送msg:"+msg); }else if(webSocket.readyState===2){ return "正在關閉"; }else{ return "連接已關閉或鏈接失敗"; } document.getElementById("msg").innerHTML += "<p>request:"+msg+"</p>" }; function close(event){ webSocket.close(); } </script> </body> </html> ``` ```
                  <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>

                              哎呀哎呀视频在线观看