Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No data returned from server written to socket before connection close #37

Closed
pitersky opened this issue Apr 27, 2016 · 2 comments
Closed

Comments

@pitersky
Copy link

pitersky commented Apr 27, 2016

Hi!

I have a following code

$socket->on('connection', function ($conn) 
{
  $conn->write("Hello there!\n");

  $conn->on('data', function ($data) use ($conn) {
    $conn->write("I have received a data!\n");
    $conn->close();
  });
});

The problem is I only receive "Hello there" string, but not "I have received a data!\n". I am not receiving second string even with sleep(5) or something before $conn->close() line. Is this a bug or I am doing something wrong?

Thank you in advance.

PS

comoser:

{
    "require": {
        "react/socket": "~0.4.0"
    }
}

php -v:

PHP 7.0.5 (cli) (built: Apr  2 2016 23:10:23) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies
@cboden
Copy link
Member

cboden commented Apr 27, 2016

close immediately closes the socket. Being an asynchronous environment that could happen before the write happens. Instead, use $conn->end(). That will close the connection after the write buffer has been drained.

@pitersky
Copy link
Author

->end() works great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants