create() публичный статический Метод

public static create ( $type, $data = null )
Пример #1
0
 function testMasking()
 {
     $input = "Hello World!";
     $frame = WebSocketFrame::create(WebSocketOpcode::TextFrame, $input);
     $frame->setMasked(true);
     $client = new WebSocket("wss://127.0.0.1:12345/echo/");
     $client->open();
     $client->sendFrame($frame);
     $msg = $client->readMessage();
     $client->close();
     $this->assertEquals($input, $frame->getData());
     $this->assertEquals(false, $msg->getFrames()[0]->isMasked());
 }
Пример #2
0
 public function close()
 {
     $f = WebSocketFrame::create(WebSocketOpcode::CloseFrame);
     $this->sendFrame($f);
     $this->_socket->close();
 }
Пример #3
0
 /**
  * Start the server
  */
 public function bind()
 {
     $err = $errno = 0;
     $this->FLASH_POLICY_FILE = str_replace('to-ports="*', 'to-ports="' . $this->uri->getPort() ?: 80, $this->FLASH_POLICY_FILE);
     $serverSocket = stream_socket_server($this->uri->toString(), $errno, $err, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $this->_context);
     $this->_logger->notice(sprintf("phpws listening on %s", $this->uri->toString()));
     if ($serverSocket == false) {
         $this->_logger->err("Error: {$err}");
         return;
     }
     $timeOut =& $this->purgeUserTimeOut;
     $sockets = $this->_streams;
     $that = $this;
     $logger = $this->_logger;
     $this->loop->addReadStream($serverSocket, function ($serverSocket) use($that, $logger, $sockets) {
         try {
             $newSocket = stream_socket_accept($serverSocket);
         } catch (\ErrorException $e) {
             $newSocket = false;
         }
         if (false === $newSocket) {
             return;
         }
         stream_set_blocking($newSocket, 0);
         $client = new WebSocketConnection($newSocket, $that->loop, $logger);
         $sockets->attach($client);
         $client->on("handshake", function (Handshake $request) use($that, $client) {
             $that->emit("handshake", array($client->getTransport(), $request));
         });
         $client->on("connect", function () use($that, $client, $logger) {
             $con = $client->getTransport();
             $that->getConnections()->attach($con);
             $that->emit("connect", array("client" => $con));
         });
         $client->on("message", function ($message) use($that, $client, $logger) {
             $connection = $client->getTransport();
             $that->emit("message", array("client" => $connection, "message" => $message));
         });
         $client->on("close", function () use($that, $client, $logger, &$sockets, $client) {
             $sockets->detach($client);
             $connection = $client->getTransport();
             if ($connection) {
                 $that->getConnections()->detach($connection);
                 $that->emit("disconnect", array("client" => $connection));
             }
         });
         $client->on("flashXmlRequest", function () use($that, $client) {
             $client->getTransport()->sendString($that->FLASH_POLICY_FILE);
             $client->close();
         });
     });
     $this->loop->addPeriodicTimer(5, function () use($timeOut, $sockets, $that) {
         # Lets send some pings
         foreach ($that->getConnections() as $c) {
             if ($c instanceof WebSocketTransportHybi) {
                 $c->sendFrame(WebSocketFrame::create(WebSocketOpcode::PingFrame));
             }
         }
         $currentTime = time();
         if ($timeOut == null) {
             return;
         }
         foreach ($sockets as $s) {
             if ($currentTime - $s->getLastChanged() > $timeOut) {
                 $s->close();
             }
         }
     });
 }
Пример #4
0
 public function close()
 {
     if ($this->isClosing) {
         return;
     }
     $this->isClosing = true;
     $this->sendFrame(WebSocketFrame::create(WebSocketOpcode::CloseFrame));
     $this->state = self::STATE_CLOSING;
     $stream = $this->stream;
     $closeTimer = $this->loop->addTimer(5, function () use($stream) {
         $stream->close();
     });
     $loop = $this->loop;
     $stream->once("close", function () use($closeTimer, $loop) {
         if ($closeTimer) {
             $loop->cancelTimer($closeTimer);
         }
     });
 }
Пример #5
0
 protected function createFrames()
 {
     $this->frames = [WebSocketFrame::create(WebSocketOpcode::TEXT_FRAME, $this->data)];
 }
Пример #6
0
 /**
  * @return void
  */
 public function close()
 {
     $f = WebSocketFrame::create(WebSocketOpcode::CLOSE_FRAME);
     $this->sendFrame($f);
     $this->socket->close();
 }
Пример #7
0
 protected function createFrames()
 {
     $this->frames = array(WebSocketFrame::create(WebSocketOpcode::TextFrame, $this->data));
 }
Пример #8
0
 public function clientSendPing()
 {
     if (!empty($this->client) && $this->client->getState() == WebSocketClient::STATE_CONNECTED) {
         $this->client->sendFrame(WebSocketFrame::create(WebSocketOpcode::PingFrame));
         return true;
     }
     return false;
 }