pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

        <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="dvtprvz" 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="fbn5tl5" 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="n5xrfdv" id='G0Sz5'><tfoot id='G0Sz5'></tfoot><dl id='G0Sz5'><fieldset id='G0Sz5'></fieldset></dl></div>
                            主站蜘蛛池模板: 电渗析,废酸回收,双极膜-山东天维膜技术有限公司 | 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | 砖机托板价格|免烧砖托板|空心砖托板厂家_山东宏升砖机托板厂 | 办公室家具公司_办公家具品牌厂家_森拉堡办公家具【官网】 | 山东集装箱活动房|济南集装箱活动房-济南利森集装箱有限公司 | 温州中研白癜风专科_温州治疗白癜风_温州治疗白癜风医院哪家好_温州哪里治疗白癜风 | 伶俐嫂培训学校_月嫂培训班在哪里报名学费是多少_月嫂免费政府培训中心推荐 | 山东锐智科电检测仪器有限公司_超声波测厚仪,涂层测厚仪,里氏硬度计,电火花检漏仪,地下管线探测仪 | 品牌策划-品牌设计-济南之式传媒广告有限公司官网-提供品牌整合丨影视创意丨公关活动丨数字营销丨自媒体运营丨数字营销 | 丝杆升降机-不锈钢丝杆升降机-非标定制丝杆升降机厂家-山东鑫光减速机有限公司 | 耐磨焊丝,堆焊焊丝,耐磨药芯焊丝,碳化钨焊丝-北京耐默公司 | 全自动贴标机-套标机-工业热风机-不干胶贴标机-上海厚冉机械 | 干粉砂浆设备_干混砂浆生产线_腻子粉加工设备_石膏抹灰砂浆生产成套设备厂家_干粉混合设备_砂子烘干机--郑州铭将机械设备有限公司 | 不锈钢螺丝,不锈钢螺栓,不锈钢标准件-江苏百德特种合金有限公司 交变/复合盐雾试验箱-高低温冲击试验箱_安奈设备产品供应杭州/江苏南京/安徽马鞍山合肥等全国各地 | IHDW_TOSOKU_NEMICON_EHDW系列电子手轮,HC1系列电子手轮-上海莆林电子设备有限公司 | 湿地保护| 盘煤仪,盘料仪,盘点仪,堆料测量仪,便携式激光盘煤仪-中科航宇(北京)自动化工程技术有限公司 | 中宏网-今日新闻-财经新闻 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 宿舍管理系统_智慧园区系统_房屋/房产管理系统_公寓管理系统 | 无锡不干胶标签,卷筒标签,无锡瑞彩包装材料有限公司 | 智慧食堂_食堂管理系统_食堂订餐_食堂消费系统—客易捷 | 东亚液氮罐-液氮生物容器-乐山市东亚机电工贸有限公司 | 康明斯发电机,上柴柴油发电机,玉柴柴油发电机组_海南重康电力官网 | CCC验厂-家用电器|服务器CCC认证咨询-奥测世纪 | 亚克隆,RNAi干扰检测,miRNA定量检测-上海基屹生物科技有限公司 | 理化生实验室设备,吊装实验室设备,顶装实验室设备,实验室成套设备厂家,校园功能室设备,智慧书法教室方案 - 东莞市惠森教学设备有限公司 | 油液红外光谱仪-油液监测系统-燃油嗅探仪-上海冉超光电科技有限公司 | 贵州自考_贵州自学考试网| Trimos测长机_测高仪_TESA_mahr,WYLER水平仪,PWB对刀仪-德瑞华测量技术(苏州)有限公司 | 筒瓦厂家-仿古瓦-寺庙-古建琉璃瓦-宜兴市古典园林建筑陶瓷厂有限公司 | 电缆接头_防水接头_电缆防水接头 - 乐清市新豪电气有限公司 | 天一线缆邯郸有限公司_煤矿用电缆厂家_矿用光缆厂家_矿用控制电缆_矿用通信电缆-天一线缆邯郸有限公司 | 拖链电缆_柔性电缆_伺服电缆_坦克链电缆-深圳市顺电工业电缆有限公司 | 深圳昂为官网-气体分析仪,沼气分析仪,动态配气仪,气体传感器厂家 | 振动台-振动试验台-振动冲击台-广东剑乔试验设备有限公司 | 北京开业庆典策划-年会活动策划公司-舞龙舞狮团大鼓表演-北京盛乾龙狮鼓乐礼仪庆典策划公司 | 彩信群发_群发彩信软件_视频短信营销平台-达信通 | 珠海网站建设_响应网站建设_珠海建站公司_珠海网站设计与制作_珠海网讯互联 | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 |