/** * 发送数据 * * 通过socket连接发送数据 * * @param StreamBuf $content 对象或字符串 * @return bool */ public function send($content) { $buffer = is_string($content) ? $content : $content->data(); $length = strlen($buffer); $bytes = @socket_send($this->socket, $buffer, $length, 0); if ($bytes === false) { $errno = socket_last_error($this->socket); if ($errno === 0) { return true; } Exception::display($this->socket); return false; } return true; }
/** * 接受连接请求 * * @return void */ private function doAccept() { $client = @socket_accept($this->socket); if ($client === false) { return Exception::display($this->socket); } $sessid = $this->genid($client); if ($this->sessionProvider == null) { $this->setProvider('koogix\\net\\Session'); } $this->connections[$sessid] = $this->sessionProvider->newInstance($client, $sessid); if ($this->onConnected == null) { return; } $closure = $this->onConnected; $closure($this->connections[$sessid], $this->connections); }