public function sendFrame(IWebSocketFrame $frame) { if ($this->_socket->write($frame->encode()) === false) { return FALSE; } }
public function onDisconnect(WebSocketSocket $socket) { $con = $socket->getConnection(); try { if ($con) { $handler = $this->_connections[$con]; if ($handler) { $this->uriHandlers[$handler]->removeConnection($con); } $this->_connections->detach($socket->getConnection()); } } catch (Exception $e) { $this->say("Exception occurred while handling message:\r\n" . $e->getTraceAsString()); } if ($con instanceof IWebSocketConnection) { foreach ($this->_observers as $o) { /** * @var @o IWebSocketServerObserver */ $o->onDisconnect($con); } } $this->sockets->detach($socket); }
/** * TODO: Proper header generation! * TODO: Check server response! */ public function open() { $errno = $errstr = null; $protocol = $this->scheme == 'ws' ? "tcp" : "ssl"; $this->socket = stream_socket_client("{$protocol}://{$this->host}:{$this->port}", $errno, $errstr, $this->getTimeOut()); // socket_connect($this->socket, $this->host, $this->port); $buffer = $this->serializeHeaders(); fwrite($this->socket, $buffer, strlen($buffer)); // wait for response $buffer = WebSocketFunctions::readWholeBuffer($this->socket); $headers = WebSocketFunctions::parseHeaders($buffer); $s = new WebSocketSocket($this, $this->socket, $immediateWrite = true); if ($this->hybi) { $this->_connection = new WebSocketConnectionHybi($s, $headers); } else { $this->_connection = new WebSocketConnectionHixie($s, $headers, $buffer); } $s->setConnection($this->_connection); return true; }