개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.05.08 09:06

SwooleConnectionIterator에 대한 사용법을 알려주세요

목록
  • Lisp전문가 15시간 전 2025.05.08 09:06 새글
  • 2
    1
제가 SwooleConnectionIterator::next를 사용하여 연결을 순회하는 중에 발생하는 오류에 대해 도움을 요청하고 싶습니다.

제가 소스를 작성한 후에 SwooleConnectionIterator::next를 호출하면 "Resource ID #N/A" 오류가 발생합니다.

이러한 오류가 발생하는 이유를 알고 싶습니다.

소스 코드는 다음과 같습니다.

```php
$server = new swoole_server('0.0.0.0', 9501);

$serv->start();

$server->on('connect', function($ws, $request) {
$connection = new \Swoole\Coroutine\Connection();
$connection->connect('127.0.0.1', 9502);

$iterator = new SwooleConnectionIterator();
$iterator->add($connection);

$iterator->rewind();

while ($iterator->valid()) {
$nextConnection = $iterator->next();

if ($nextConnection) {
$this->log->debug('next connection');
} else {
$this->log->error('next connection is null');
}
}

$this->log->debug('connection count: ' . $this->clientList->count());
});

$serv->on('close', function ($ser, $fd) {
//
});

$serv->on('message', function ($ws, $data) {
$this->log->info('[{id}] : {data}', ['id' => $this->clientList->get($this->clientList->getActive() - 1), 'data' => $data]);

$this->server->push($this->clientList->get($this->clientList->getActive() - 1), $data);

$this->server->push($this->clientList->get($this->clientList->getActive() - 1), 'I received: [{}]', ['%data%' => $data]);

$this->server->push($this->clientList->get($this->clientList->getActive() - 1), 'I received: [{}]', ['%data%' => $data]);

$this->server->push($this->clientList->get($this->clientList->getActive() - 1),

    댓글목록

    profile_image
    나우호스팅  15시간 전



    SwooleConnectionIterator::next를 사용하여 연결을 순회하는 중에 발생하는 오류는 Resource ID #N/A 오류입니다.

    이러한 오류는 SwooleConnectionIterator::next를 호출하기 전에 rewind() 메소드를 호출하지 않아서 발생합니다.

    SwooleConnectionIterator는 연결을 순회할 때 Iterator를 사용하기 때문에 rewind() 메소드를 호출하여 Iterator의 포인터를 처음 위치로 이동해야 합니다.

    정확한 소스를 작성하기 위해서는 SwooleConnectionIterator::next를 호출하기 전에 rewind() 메소드를 호출해야 합니다.

    #hostingforum.kr
    php
    
    $iterator = new SwooleConnectionIterator();
    
    $iterator->add($connection);
    
    $iterator->rewind(); // rewind() 메소드를 호출하여 Iterator의 포인터를 처음 위치로 이동
    
    while ($iterator->valid()) {
    
        $nextConnection = $iterator->next();
    
        // ...
    
    }
    
    


    또한, SwooleConnectionIterator::next를 호출할 때 null을 반환할 수 있습니다. 이 경우는 연결이 더 이상 존재하지 않는 경우입니다.

    따라서, SwooleConnectionIterator::next를 호출할 때 null을 반환하는 경우를 처리해야 합니다.

    #hostingforum.kr
    php
    
    while ($iterator->valid()) {
    
        $nextConnection = $iterator->next();
    
        if ($nextConnection) {
    
            $this->log->debug('next connection');
    
        } else {
    
            $this->log->error('next connection is null');
    
        }
    
    }
    
    

    2025-05-08 09:07

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 17,698건 / 10 페이지

검색

게시물 검색