示例#1
0
 public function open($timeOut = null)
 {
     /**
      * @var $that self
      */
     $that = new FullAccessWrapper($this);
     $uri = new Uri($this->url);
     $isSecured = 'wss' === $uri->getScheme();
     $defaultPort = $isSecured ? 443 : 80;
     $connector = new Connector($this->loop, $this->dns, $this->streamOptions);
     if ($isSecured) {
         $connector = new \React\SocketClient\SecureConnector($connector, $this->loop);
     }
     $deferred = new Deferred();
     $connector->create($uri->getHost(), $uri->getPort() ?: $defaultPort)->then(function (\React\Stream\Stream $stream) use($that, $uri, $deferred, $timeOut) {
         if ($timeOut) {
             $timeOutTimer = $that->loop->addTimer($timeOut, function () use($promise, $stream, $that) {
                 $stream->close();
                 $that->logger->notice("Timeout occured, closing connection");
                 $that->emit("error");
                 $promise->reject("Timeout occured");
             });
         } else {
             $timeOutTimer = null;
         }
         $transport = new WebSocketTransportHybi($stream);
         $transport->setLogger($that->logger);
         $that->transport = $transport;
         $that->stream = $stream;
         $stream->on("close", function () use($that) {
             $that->isClosing = false;
             $that->state = WebSocket::STATE_CLOSED;
         });
         // Give the chance to change request
         $transport->on("request", function (Request $handshake) use($that) {
             $that->emit("request", func_get_args());
         });
         $transport->on("handshake", function (Handshake $handshake) use($that) {
             $that->request = $handshake->getRequest();
             $that->response = $handshake->getRequest();
             $that->emit("handshake", array($handshake));
         });
         $transport->on("connect", function () use(&$state, $that, $transport, $timeOutTimer, $deferred) {
             if ($timeOutTimer) {
                 $timeOutTimer->cancel();
             }
             $deferred->resolve($transport);
             $that->state = WebSocket::STATE_CONNECTED;
             $that->emit("connect");
         });
         $transport->on('message', function ($message) use($that, $transport) {
             $that->emit("message", array("message" => $message));
         });
         $transport->initiateHandshake($uri);
         $that->state = WebSocket::STATE_HANDSHAKE_SENT;
     }, function ($reason) use($that) {
         $that->logger->err($reason);
     });
     return $deferred->promise();
 }
 private function createImapstream(&$imapaccount)
 {
     //$loop = \React\EventLoop\Factory::create();
     $dnsResolverFactory = new \React\Dns\Resolver\Factory();
     $dns = $dnsResolverFactory->createCached('8.8.8.8', $this->loop);
     $imapconnector = new \React\SocketClient\Connector($this->loop, $dns);
     $secureConnector = new \React\SocketClient\SecureConnector($imapconnector, $this->loop);
     $mailaccount = $imapaccount['mailaccount'];
     echo $mailaccount->getImapserver() . ($mailaccount->getImapport() ? $mailaccount->getImapport() : 993);
     $secureConnector->create($mailaccount->getImapserver(), $mailaccount->getImapport() ? $mailaccount->getImapport() : 993)->then(function (\React\Stream\Stream $imapstream) use(&$imapaccount) {
         $uid = uniqid();
         echo $imapaccount['mailaccount']->getImapserver() . ': ' . $uid;
         $imapaccount['imapstream'] = $imapstream;
         $login = $imapaccount['mailaccount']->getImapusername();
         $password = $imapaccount['mailaccount']->getImappassword();
         $imapstream->write($uid . " LOGIN {$login} {$password}\r\n");
         $status = 'LOGIN';
         $imapstream->on('data', function ($data) use($uid, &$status, &$imapstream, &$imapaccount) {
             echo $imapaccount['mailaccount']->getImapserver() . ': ' . $data;
             $dataexpl = explode("\r\n", $data);
             foreach ($dataexpl as $dexpl) {
                 if (preg_match("/^" . $uid . " OK/", $dexpl)) {
                     //login OK:
                     if ($status == 'LOGIN') {
                         $imapstream->write($uid . " SELECT " . $imapaccount['mailaccount']->getImappathprefix() . "\r\n");
                         $status = 'SELECT';
                         echo "SEND: SELECT {$status}\r\n";
                     } else {
                         if ($status == 'SELECT') {
                             $imapstream->write($uid . " IDLE\r\n");
                             $status = 'IDLE';
                             echo "SEND: IDLE {$status}\r\n";
                         }
                     }
                 }
                 if ($status == 'IDLE') {
                     if (preg_match("/^\\* (\\d+) RECENT/", $dexpl, $countrecent)) {
                         //login OK:
                         $countrecent = $countrecent[1];
                         echo 'RECENT:' . $countrecent;
                         $this->notifychanges($imapaccount);
                     }
                     if (preg_match("/^\\* (\\d+) EXISTS/", $dexpl, $countrecent)) {
                         //login OK:
                         $countrecent = $countrecent[1];
                         echo 'EXISTS' . $countrecent;
                         $this->notifychanges($imapaccount);
                     }
                 }
             }
         });
         $imapstream->on('end', function () use($uid, &$status, &$imapstream, &$imapaccount) {
             echo 'END!!!!!';
         });
     });
     return $imapaccount;
 }