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

        <tfoot id='4cksR'></tfoot><legend id='4cksR'><style id='4cksR'><dir id='4cksR'><q id='4cksR'></q></dir></style></legend>
      1. <small id='4cksR'></small><noframes id='4cksR'>

          <bdo id='4cksR'></bdo><ul id='4cksR'></ul>
      2. <i id='4cksR'><tr id='4cksR'><dt id='4cksR'><q id='4cksR'><span id='4cksR'><b id='4cksR'><form id='4cksR'><ins id='4cksR'></ins><ul id='4cksR'></ul><sub id='4cksR'></sub></form><legend id='4cksR'></legend><bdo id='4cksR'><pre id='4cksR'><center id='4cksR'></center></pre></bdo></b><th id='4cksR'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4cksR'><tfoot id='4cksR'></tfoot><dl id='4cksR'><fieldset id='4cksR'></fieldset></dl></div>
      3. 定期向 Ratchet 中的客戶端發送消息

        Periodically sending messages to clients in Ratchet(定期向 Ratchet 中的客戶端發送消息)
        • <bdo id='3aEH5'></bdo><ul id='3aEH5'></ul>

            <legend id='3aEH5'><style id='3aEH5'><dir id='3aEH5'><q id='3aEH5'></q></dir></style></legend>
            <tfoot id='3aEH5'></tfoot>

              <tbody id='3aEH5'></tbody>

            <small id='3aEH5'></small><noframes id='3aEH5'>

                • <i id='3aEH5'><tr id='3aEH5'><dt id='3aEH5'><q id='3aEH5'><span id='3aEH5'><b id='3aEH5'><form id='3aEH5'><ins id='3aEH5'></ins><ul id='3aEH5'></ul><sub id='3aEH5'></sub></form><legend id='3aEH5'></legend><bdo id='3aEH5'><pre id='3aEH5'><center id='3aEH5'></center></pre></bdo></b><th id='3aEH5'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='3aEH5'><tfoot id='3aEH5'></tfoot><dl id='3aEH5'><fieldset id='3aEH5'></fieldset></dl></div>
                  本文介紹了定期向 Ratchet 中的客戶端發送消息的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試定期發送hello world!"從 Ratchet 教程向所有連接到聊天服務器的客戶端發送消息

                  I'm trying to periodically send a "hello world!" message to all clients connected to the chat-server from the Ratchet tutorial

                  我將在這里發布所有代碼:聊天.php:

                  I will post all of the code here: Chat.php:

                  <?php
                  namespace MyApp;
                  use RatchetMessageComponentInterface;
                  use RatchetConnectionInterface;
                  
                  class Chat implements MessageComponentInterface {
                      public $clients;
                  
                      public function __construct() {
                          $this->clients = new SplObjectStorage;
                              }
                  
                      public function onOpen(ConnectionInterface $conn) {
                          // Store the new connection to send messages to later
                          $this->clients->attach($conn);
                  
                          echo "New connection! ({$conn->resourceId})
                  ";
                      }
                  
                      //this worked but I don't want this behaviour
                      public function onMessage(ConnectionInterface $from, $msg) {
                          /*$numRecv = count($this->clients) - 1;
                          echo sprintf('Connection %d sending message "%s" to %d other connection%s' . "
                  "
                              , $from->resourceId, $msg, $numRecv, $numRecv == 1 ? '' : 's');
                  
                          foreach ($this->clients as $client) {
                              if ($from !== $client) {
                                  // The sender is not the receiver, send to each client connected
                                  $client->send($msg);
                              }
                          }*/
                      }
                  
                      public function onClose(ConnectionInterface $conn) {
                          // The connection is closed, remove it, as we can no longer send it messages
                          $this->clients->detach($conn);
                  
                          echo "Connection {$conn->resourceId} has disconnected
                  ";
                      }
                  
                      public function onError(ConnectionInterface $conn, Exception $e) {
                          echo "An error has occurred: {$e->getMessage()}
                  ";
                  
                          $conn->close();
                      }
                  }
                  

                  聊天服務器.php:

                  <?php
                  use RatchetServerIoServer;
                  use MyAppChat;
                  
                      require dirname(__DIR__) . '/vendor/autoload.php';
                  
                      $server = IoServer::factory(
                          new Chat(),
                          8080
                      );
                  
                      $server->run();
                  

                  為了測試我理解了多少文檔,我在服務器的循環中添加了一個計時器

                  To test how much of the docs I understood , I added a timer to the server's loop

                      <?php
                      use RatchetServerIoServer;
                      use MyAppChat;
                  
                          require dirname(__DIR__) . '/vendor/autoload.php';
                  
                          $server = IoServer::factory(
                              new Chat(),
                              8080
                          );
                  
                  
                          // My code here
                          $server->loop->addPeriodicTimer(5, function () {
                            echo  "custom loop timer working !";        
                          });
                  
                  
                          $server->run();
                  

                  并且它在啟動服務器后每五秒輸出一次該字符串工作正常.

                  and it worked fine outputting that string every five seconds after starting the server.

                  現在我嘗試這樣做,嘗試向存儲在 MessageComponentInterface 中的客戶端發送一條消息,該接口稱為教程中的 Chat

                  Now I tried doing it like so, trying to send a message to clients stored in the MessageComponentInterface called Chat from the tutorial

                  $server->loop->addPeriodicTimer(5, function () {        
                      foreach ($server->app->clients as $client) {                  
                              $client->send("hello client");          
                      }
                  });
                  

                  但我得到 $server->app 為 NULL 這可能是因為我現在在 function() 塊中.當談到面向對象的 PHP 時,我不是專家,這個小項目將肯定對我有很大幫助.如何在計時器內訪問名為 app 的服務器的 MessageComponentInterface 屬性,然后將數據發送到存儲在其中的客戶端?

                  But I'm getting that $server->app is NULL which is probably because I'm now inside the function() block .I'm not an expert when it comes to Object oriented PHP, and this little project will sure help me a lot. How can I access the MessageComponentInterface called app property of the server inside the timer and then send data to the clients stored in there?

                  推薦答案

                  $server 沒有在函數作用域中定義,父作用域的變量不會被繼承到子作用域默認.閉包可以通過使用 use 語言結構從父作用域繼承變量.

                  $server isn't defined in the function scope and variables from the parent scope don't get inherited to the child scope by default. Closures can inherit variables from the parent scope by using the use language construct.

                  $server->loop->addPeriodicTimer(5, function () use ($server) {        
                      foreach ($server->app->clients as $client) {                  
                              $client->send("hello client");          
                      }
                  });
                  

                  關于匿名函數(閉包)的更多信息:https://secure.php.net/manual/en/functions.anonymous.php
                  有關變量范圍的更多信息:https://secure.php.net/manual/en/language.variables.scope.php

                  More information about anonymous functions (closures): https://secure.php.net/manual/en/functions.anonymous.php
                  More information about variables scope: https://secure.php.net/manual/en/language.variables.scope.php

                  這篇關于定期向 Ratchet 中的客戶端發送消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)
                  • <bdo id='G0Sz5'></bdo><ul id='G0Sz5'></ul>
                      <tbody id='G0Sz5'></tbody>

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

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

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

                            <i id='G0Sz5'><tr id='G0Sz5'><dt id='G0Sz5'><q id='G0Sz5'><span id='G0Sz5'><b id='G0Sz5'><form id='G0Sz5'><ins id='G0Sz5'></ins><ul id='G0Sz5'></ul><sub id='G0Sz5'></sub></form><legend id='G0Sz5'></legend><bdo id='G0Sz5'><pre id='G0Sz5'><center id='G0Sz5'></center></pre></bdo></b><th id='G0Sz5'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='G0Sz5'><tfoot id='G0Sz5'></tfoot><dl id='G0Sz5'><fieldset id='G0Sz5'></fieldset></dl></div>
                            主站蜘蛛池模板: 日韩欧美一区二区三区免费看 | 国产精品久久久久久久久久久久冷 | 欧美中文字幕一区二区 | 国产农村妇女精品一区 | 亚洲视频三 | 99精品一区 | 久久精品69 | 伊人激情网 | 综合久久av | 欧美在线a | 9久久婷婷国产综合精品性色 | 久久久久一区 | 精品久久久久久久久久久久久久 | 亚洲福利在线观看 | 日韩一级免费看 | 成人精品 | 成人毛片视频免费 | 国产色播av在线 | 四虎影院新地址 | 天天干视频 | 欧美日韩国产一区二区三区 | 久久久精品久久久 | 麻豆亚洲 | 久久高清国产视频 | 国产精品一区二区三区四区 | 亚洲一区二区三区免费观看 | 欧美激情综合 | 国产一区二区三区免费观看在线 | 天天久久 | 成人中文字幕在线观看 | 午夜视频免费在线观看 | 天堂av中文在线 | 国产一区二区三区 | 中文字幕 国产 | 2021狠狠天天天 | 欧美中文字幕一区 | 国产精品国产三级国产aⅴ中文 | 爱爱视频网 | 国产精品色 | 一区视频 | 精品在线一区 |