• <bdo id='qRKTt'></bdo><ul id='qRKTt'></ul>

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

    1. <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 id='qRKTt'><tfoot id='qRKTt'></tfoot><dl id='qRKTt'><fieldset id='qRKTt'></fieldset></dl></div>

        如何使用 WebSocket 从 PHP 发送数据/文本进行处理

        时间:2023-09-25

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

                <tbody id='Fwe4L'></tbody>
              • <tfoot id='Fwe4L'></tfoot>
              • <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 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 发送数据/文本进行处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在充当 WebSocket 服务器的服务器上有进程(不是用 Ratchet 编写的).我希望能够使用 PHP(作为客户端)向该进程发送数据.

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

                  我发现了很多像这样作为 TCP 发送的示例:

                  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);
                  ?>
                  

                  我只需要向进程发送消息并关闭连接即可.我期望的结果是 webSocket 的结果稍后会打印或回显"到 PHP 页面.

                  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,因为我已经查看并尝试了很多)

                  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 个 php websocket 客户端中,这是我唯一能够使用的客户端.它不需要任何外部文件或框架.这是执行不需要持久连接到 webSocket 服务器的短命令的简单实现.

                  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.

                  希望能帮到你们,省时省力!!!

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

                  这篇关于如何使用 WebSocket 从 PHP 发送数据/文本进行处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:laravel 5 中的简单 websocket 实现 下一篇:用于实时聊天应用程序的 HTML5 Websockets?

                  相关文章

                  最新文章

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

                        <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 id='HQfRG'><tfoot id='HQfRG'></tfoot><dl id='HQfRG'><fieldset id='HQfRG'></fieldset></dl></div>
                      <tfoot id='HQfRG'></tfoot>