久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

      <small id='Fwe4L'></small><noframes id='Fwe4L'>

    1. <tfoot id='Fwe4L'></tfoot>
    2. <legend id='Fwe4L'><style id='Fwe4L'><dir id='Fwe4L'><q id='Fwe4L'></q></dir></style></legend>
      <i id='Fwe4L'><tr id='Fwe4L'><dt id='Fwe4L'><q id='Fwe4L'><span id='Fwe4L'><b id='Fwe4L'><form id='Fwe4L'><ins id='Fwe4L'></ins><ul id='Fwe4L'></ul><sub id='Fwe4L'></sub></form><legend id='Fwe4L'></legend><bdo id='Fwe4L'><pre id='Fwe4L'><center id='Fwe4L'></center></pre></bdo></b><th id='Fwe4L'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Fwe4L'><tfoot id='Fwe4L'></tfoot><dl id='Fwe4L'><fieldset id='Fwe4L'></fieldset></dl></div>

        <bdo id='Fwe4L'></bdo><ul id='Fwe4L'></ul>

      如何使用 WebSocket 從 PHP 發(fā)送數(shù)據(jù)/文本進(jìn)行處理

      How can I send data/text from PHP using WebSocket to process?(如何使用 WebSocket 從 PHP 發(fā)送數(shù)據(jù)/文本進(jìn)行處理?)
          <tbody id='qRKTt'></tbody>
        • <bdo id='qRKTt'></bdo><ul id='qRKTt'></ul>

              <small id='qRKTt'></small><noframes id='qRKTt'>

            • <legend id='qRKTt'><style id='qRKTt'><dir id='qRKTt'><q id='qRKTt'></q></dir></style></legend>

              1. <tfoot id='qRKTt'></tfoot>

              2. <i id='qRKTt'><tr id='qRKTt'><dt id='qRKTt'><q id='qRKTt'><span id='qRKTt'><b id='qRKTt'><form id='qRKTt'><ins id='qRKTt'></ins><ul id='qRKTt'></ul><sub id='qRKTt'></sub></form><legend id='qRKTt'></legend><bdo id='qRKTt'><pre id='qRKTt'><center id='qRKTt'></center></pre></bdo></b><th id='qRKTt'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qRKTt'><tfoot id='qRKTt'></tfoot><dl id='qRKTt'><fieldset id='qRKTt'></fieldset></dl></div>
                本文介紹了如何使用 WebSocket 從 PHP 發(fā)送數(shù)據(jù)/文本進(jìn)行處理?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我在充當(dāng) WebSocket 服務(wù)器的服務(wù)器上有進(jìn)程(不是用 Ratchet 編寫的).我希望能夠使用 PHP(作為客戶端)向該進(jìn)程發(fā)送數(shù)據(jù).

                I have process on server which acts as WebSocket server (not written in Ratchet). I want to be able to send data to this process using PHP (as client).

                我發(fā)現(xiàn)了很多像這樣作為 TCP 發(fā)送的示例:

                I found a lot of examples to send as TCP like this:

                <?php
                  $addr = gethostbyname("localhost");
                
                  $client = stream_socket_client("tcp://$addr:8887", $errno, $errorMessage);
                
                  if ($client === false) {
                      throw new UnexpectedValueException("Failed to connect: $errorMessage");
                  }
                
                  fwrite($client, "GET / HTTP/1.0
                Host: localhost
                Accept: */*
                
                ");
                  echo stream_get_contents($client);
                ?>
                

                我只需要向進(jìn)程發(fā)送消息并關(guān)閉連接即可.我期望的結(jié)果是 webSocket 的結(jié)果稍后會(huì)打印或回顯"到 PHP 頁(yè)面.

                All I need I to send message to the process and close the connection. The result that I expect is the result from the webSocket will be later printed or "echo" to the PHP page.

                有沒有辦法讓它在 php 中與 curl 一起工作?

                Is there a way to make it work with curl in php?

                推薦答案

                我在 github 上找到了這段代碼,(我找不到我從哪里得到它的確切 repo,因?yàn)槲乙呀?jīng)查看并嘗試了很多)

                I have found this code on github, (I can't find the exact repo where I got it from because I have looked and tried a lot of them)

                <?php
                class WebsocketClient {
                
                    private $_Socket = null;
                
                    public function __construct($host, $port) {
                        $this->_connect($host, $port);
                    }
                
                    public function __destruct() {
                        $this->_disconnect();
                    }
                
                    public function sendData($data) {
                        // send actual data:
                        fwrite($this->_Socket, "x00" . $data . "xff") or die('Error:' . $errno . ':' . $errstr);
                        $wsData = fread($this->_Socket, 2000);
                        $retData = trim($wsData, "x00xff");
                        return $retData;
                    }
                
                    private function _connect($host, $port) {
                        $key1 = $this->_generateRandomString(32);
                        $key2 = $this->_generateRandomString(32);
                        $key3 = $this->_generateRandomString(8, false, true);
                
                        $header = "GET /echo HTTP/1.1
                ";
                        $header.= "Upgrade: WebSocket
                ";
                        $header.= "Connection: Upgrade
                ";
                        $header.= "Host: " . $host . ":" . $port . "
                ";
                        $header.= "Origin: http://localhost
                ";
                        $header.= "Sec-WebSocket-Key1: " . $key1 . "
                ";
                        $header.= "Sec-WebSocket-Key2: " . $key2 . "
                ";
                        $header.= "
                ";
                        $header.= $key3;
                
                
                        $this->_Socket = fsockopen($host, $port, $errno, $errstr, 2);
                        fwrite($this->_Socket, $header) or die('Error: ' . $errno . ':' . $errstr);
                        $response = fread($this->_Socket, 2000);
                
                        /**
                         * @todo: check response here. Currently not implemented cause "2 key handshake" is already deprecated.
                         * See: http://en.wikipedia.org/wiki/WebSocket#WebSocket_Protocol_Handshake
                         */
                        return true;
                    }
                
                    private function _disconnect() {
                        fclose($this->_Socket);
                    }
                
                    private function _generateRandomString($length = 10, $addSpaces = true, $addNumbers = true) {
                        $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"§$%&/()=[]{}';
                        $useChars = array();
                        // select some random chars:    
                        for ($i = 0; $i < $length; $i++) {
                            $useChars[] = $characters[mt_rand(0, strlen($characters) - 1)];
                        }
                        // add spaces and numbers:
                        if ($addSpaces === true) {
                            array_push($useChars, ' ', ' ', ' ', ' ', ' ', ' ');
                        }
                        if ($addNumbers === true) {
                            array_push($useChars, rand(0, 9), rand(0, 9), rand(0, 9));
                        }
                        shuffle($useChars);
                        $randomString = trim(implode('', $useChars));
                        $randomString = substr($randomString, 0, $length);
                        return $randomString;
                    }
                
                }
                
                $WebSocketClient = new WebsocketClient('localhost', 8887);
                echo $WebSocketClient->sendData("MyUserNameFromPHP");
                unset($WebSocketClient);
                ?>
                

                在我嘗試過的 7 個(gè) php websocket 客戶端中,這是我唯一能夠使用的客戶端.它不需要任何外部文件或框架.這是執(zhí)行不需要持久連接到 webSocket 服務(wù)器的短命令的簡(jiǎn)單實(shí)現(xiàn).

                Out of 7 php websocket clients that I tried, this is the only one that I was able to work with. It doesn't requires any external files or frameworks. This is simple implementation for executing short command that doesn't require persistent connection to webSocket server.

                希望能幫到你們,省時(shí)省力?。?!

                I hope it helps you guys , and you will save some time !!!

                這篇關(guān)于如何使用 WebSocket 從 PHP 發(fā)送數(shù)據(jù)/文本進(jìn)行處理?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動(dòng)游標(biāo)不起作用)
                PHP PDO ODBC connection(PHP PDO ODBC 連接)
                Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術(shù)方法)
                php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個(gè)值;等于變量的值)
                MSSQL PDO could not find driver(MSSQL PDO 找不到驅(qū)動(dòng)程序)

                    <small id='HQfRG'></small><noframes id='HQfRG'>

                      <tbody id='HQfRG'></tbody>
                        <bdo id='HQfRG'></bdo><ul id='HQfRG'></ul>
                        <legend id='HQfRG'><style id='HQfRG'><dir id='HQfRG'><q id='HQfRG'></q></dir></style></legend>
                        <i id='HQfRG'><tr id='HQfRG'><dt id='HQfRG'><q id='HQfRG'><span id='HQfRG'><b id='HQfRG'><form id='HQfRG'><ins id='HQfRG'></ins><ul id='HQfRG'></ul><sub id='HQfRG'></sub></form><legend id='HQfRG'></legend><bdo id='HQfRG'><pre id='HQfRG'><center id='HQfRG'></center></pre></bdo></b><th id='HQfRG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='HQfRG'><tfoot id='HQfRG'></tfoot><dl id='HQfRG'><fieldset id='HQfRG'></fieldset></dl></div>
                          <tfoot id='HQfRG'></tfoot>

                        • 主站蜘蛛池模板: 国产福利网站 | 日韩在线视频观看 | 一级特黄网站 | 国产精品69毛片高清亚洲 | 成人毛片一区二区三区 | 奇米久久久 | 国产精品久久久久久吹潮 | 99久久精品免费看国产高清 | 性一区| 秋霞电影院午夜伦 | 精品成人av| 国产精品福利在线观看 | 日本一区二区三区四区 | 免费看黄色小视频 | 久久99精品久久久久久琪琪 | 久久天天躁狠狠躁夜夜躁2014 | 亚洲图片一区二区三区 | 亚洲三区视频 | 国产欧美日韩一区 | 欧美久久一区二区三区 | a级免费观看视频 | 在线观看国产精品一区二区 | 国产一区二区视频在线 | 日韩一区中文字幕 | av在线免费播放 | 国产一区二区免费 | 久久高清免费视频 | 亚洲精品国产电影 | 免费久久99精品国产婷婷六月 | 99久久久无码国产精品 | 欧美激情久久久 | 亚洲精品一区中文字幕乱码 | 国产精品入口 | 日韩欧美国产成人一区二区 | 国产偷久久一级精品60部 | 一级黄色毛片子 | 亚洲xxxxx | 亚洲精品一区二区三区四区高清 | 在线成人 | 国产视频久久 | 久久99精品久久久 |